我有一个TabLayout
和ViewPager
只有Fragments
。第一个片段显示所有项目,第二个片段仅显示最喜欢的项目。在每个Fragment
我已经实现recyclerview
项目列表,每个项目都有一个chechbox。当用户选中FirstFragment中的复选框时,必须将选择的项添加到SecondFragment中的收藏夹列表中。问题是我不明白我应该在哪里实现checkBox项的clickListener?我的意思是我已经这样做了:
我的自定义适配器,我在其中设置了一个侦听器:
@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, int position) {
final Card card = cardList.get(position);
holder.imageView.setImageDrawable(holder.imageView.getResources().getDrawable(card.getImage()));
holder.textViewHeading.setText(card.getHeading());
holder.textViewDescription.setText(card.getDescription());
holder.checkBox.setChecked(card.isEnabled());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
card.setEnabled(b);
//here probably something gets wrong because
//by logic here i want to set to the card
//that item is favorite
}
});
}
但它不起作用。
并更新我的片段中显示所有喜爱项目的数据:
private void initTabs() {
viewPager = findViewById(R.id.viewPager);
tabLayout = findViewById(R.id.tabLayout);
//set fragments
adapter.addFragments(new FragmentOne(), "Chosen");
adapter.addFragments(new FragmentTwo(), "List");
adapter.addFragments(new FragmentThree(), "Three");
//adapter setup
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0){
DataUtil.refreshChosenList();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
我的实体卡:
public class Card {
private int id;
private String heading, description;
private int image;
private boolean isEnabled;
public Card(int id, String heading, String description, int image, boolean isEnabled) {
this.id = id;
this.heading = heading;
this.description = description;
this.image = image;
this.isEnabled = isEnabled;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean bol){
this.isEnabled = bol;
}
}