Я раздуваю мой вид карты через свой пользовательский адаптер recyclerview. Теперь я хочу знать, как я могу настроить прослушиватель кликов из моего класса adpater на su, mo, tu, we, th, fr, sa position wise.Как установить onclicklistener на карте?
Я хочу, чтобы массив, как если бы пользователь нажал вс, пн, вт на 0 позиции cardview то массив должен быть следующим образом [1,2,3]
и на 1-й позиции пользователь нажал мы, е, fr, тогда я должен получить следующий массив следующим образом: [4,5,6]
В настоящее время из моего адаптера я взял ссылки на это текстовое представление и добавил прослушиватель, но я получаю такой же массив для обоих поведений, даже если выбор отличается.
Мой класс адаптера a следует.
public class Adapter1 extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<String> myDataSet;
private static String KEY1;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private static String TAG = "DATA_ADAPTER";
private static ArrayList<String> mSavedTime = new ArrayList<String>();
private static ArrayList<String> mTimeUnit = new ArrayList<>();
private static String mCurrentHour;
private static String mCurrentMinute;
private static Context context;
public AlarmAdapter(Context context, RecyclerView recyclerView, ArrayList<String> mTime, ArrayList<String> mTimeUnit, String mCurrentHour, String mCurrentMinute) {
this.mSavedTime = mTime;
this.mTimeUnit = mTimeUnit;
this.mCurrentHour = mCurrentHour;
this.mCurrentMinute = mCurrentMinute;
this.context = context;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView
.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}
@Override
public int getItemViewType(int position) {
return mSavedTime.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.saved_alarm_card, parent, false);
vh = new StudentViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.more_progress_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
((StudentViewHolder) holder).mTimeDigitText.setText(mSavedTime.get(position));
((StudentViewHolder) holder).mTimeUnitText.setText(mTimeUnit.get(position));
// ((StudentViewHolder) holder).dateField.setText(mFileDate.get(position));
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return mSavedTime.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//
public class StudentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mTimeDigitText;
public TextView mTimeUnitText;
private SwitchCompat mAlarmButtonPower;
private ImageView mAlarmBellImage;
private TextView mSundayButton;
private TextView mMondayButton;
private TextView mTuesdayButton;
private TextView mWednesdayButton;
private TextView mThursdayButton;
private TextView mFridayButton;
private TextView mSaturdayButton;
public CardView cardView1;
public StudentViewHolder(View v) {
super(v);
mTimeDigitText = (TextView) v.findViewById(R.id.timeDigit);
mTimeUnitText = (TextView) v.findViewById(R.id.timeUnit);
mAlarmBellImage = (ImageView) v.findViewById(R.id.alarmBell);
mAlarmButtonPower = (SwitchCompat) v.findViewById(R.id.alarmPowerSwitch);
mSundayButton = (TextView) v.findViewById(R.id.sunday);
mMondayButton = (TextView) v.findViewById(R.id.monday);
mTuesdayButton = (TextView) v.findViewById(R.id.tuesday);
mWednesdayButton = (TextView) v.findViewById(R.id.wednesday);
mThursdayButton = (TextView) v.findViewById(R.id.thursday);
mFridayButton = (TextView) v.findViewById(R.id.friday);
mSaturdayButton = (TextView) v.findViewById(R.id.saturday);
v.findViewById(R.id.alarmBell).setOnClickListener(this);
v.findViewById(R.id.sunday).setOnClickListener(this);
v.findViewById(R.id.monday).setOnClickListener(this);
v.findViewById(R.id.tuesday).setOnClickListener(this);
v.findViewById(R.id.wednesday).setOnClickListener(this);
v.findViewById(R.id.thursday).setOnClickListener(this);
v.findViewById(R.id.friday).setOnClickListener(this);
v.findViewById(R.id.saturday).setOnClickListener(this);
mAlarmButtonPower.setChecked(true);
if (mAlarmButtonPower.isChecked()) {
mBellCount = 2;
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell_touch));
Toast.makeText(context, "Alarm Activated!!", Toast.LENGTH_SHORT).show();
} else {
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell));
Toast.makeText(context, "First Activate Your Alarm", Toast.LENGTH_SHORT).show();
}
mAlarmButtonPower.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.alarmPowerSwitch:
if (!isChecked) {
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell));
Toast.makeText(context, "Alarm is off!!", Toast.LENGTH_SHORT).show();
} else {
// AlarmReceiver.stopRingtone();
// alarmManager.cancel(pendingIntent);
Log.d("MyActivity", "Alarm Off");
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell_touch));
Toast.makeText(context, "Alarm is on!!", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alarmBell:
mBellCount = mBellCount + 1;
if (mBellCount % 2 == 0) {
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell_touch));
} else {
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell));
}
break;
case R.id.sunday:
mSunCount = mSunCount + 1;
// mDaysArrayList.add(1);
if (mSunCount % 2 == 0) {
mSundayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mSundayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
// mDaysArrayList.clear();
}
break;
case R.id.monday:
mMonCount = mMonCount + 1;
// mDaysArrayList.add(2);
if (mMonCount % 2 == 0) {
mMondayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mMondayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
//mDaysArrayList.remove(1);
// mDaysArrayList.clear();
}
break;
case R.id.tuesday:
mTueCount = mTueCount + 1;
// mDaysArrayList.add(3);
if (mTueCount % 2 == 0) {
mTuesdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mTuesdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
// mDaysArrayList.remove(2);
//mDaysArrayList.clear();
}
break;
case R.id.wednesday:
mWedCount = mWedCount + 1;
// mDaysArrayList.add(4);
if (mWedCount % 2 == 0) {
mWednesdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mWednesdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
// mDaysArrayList.remove(3);
//mDaysArrayList.clear();
}
break;
case R.id.thursday:
mThuCount = mThuCount + 1;
// mDaysArrayList.add(5);
if (mThuCount % 2 == 0) {
mThursdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mThursdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
// mDaysArrayList.remove(4);
//mDaysArrayList.clear();
}
break;
case R.id.friday:
mFriCount = mFriCount + 1;
// mDaysArrayList.add(6);
if (mFriCount % 2 == 0) {
mFridayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mFridayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
//mDaysArrayList.remove(5);
// mDaysArrayList.clear();
}
break;
case R.id.saturday:
mSatCount = mSatCount + 1;
// mDaysArrayList.add(7);
if (mSatCount % 2 == 0) {
mSaturdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorProfilePrimary));
} else {
mSaturdayButton.setTextColor(ContextCompat.getColor(context, R.color.colorSecondary));
//mDaysArrayList.remove(6);
// mDaysArrayList.clear();
}
break;
}
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
Любая помощь большинство welcome.Thanks
вы можете поместить свой код здесь? –
Какой массив вы хотите, если пользователь нажимает на su, mo и tu в позиции 1? –
@NavinGupta - да, я помещаю свой класс адаптера. – Arora