我有一个片段分页器适配器实现了tablayout ...每个选项卡都有一个带有一些项目的回收器视图(所有recylerviews都有相同的recyler视图适配器)...通过实现OnItemClick监听器来点击每个项目调用一个活动,使用fragmentStatePagerAdapter实现一个viewpager,以显示回收器视图项...基于tablayout的滑动viewpager显示上一个或下一个选项卡项... 每个项目的数据存储在一个arraylist中。 当我在当前标签中克隆一个项目时,它喜欢我点击同一位置的下一个标签项 tablayput片段:
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
//setupViewPager(viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getChildFragmentManager(),getActivity());
adapter.addFrag(new OneFragment(), "title1");
adapter.addFrag(new TwoFragment(), "title2");
adapter.addFrag(new ThreeFragment(), "title3");
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
OneFragment和TwoFragment和......相似:
rv=(RecyclerView) view.findViewById(R.id.rv);
StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(sglm);
rv.setHasFixedSize(true);
persons = new ArrayList<>();
persons.add(new Person("name", "age","email","phone","resume", R.drawable.photo));
persons.add(new Person("name", "age","email","phone","resume", R.drawable.photo));
.
.
.
for (int j=0; j < persons.size() ;j++){
PersonItem personItem = new PersonItem();
personItem.setName(persons.get(j).name);
personItem.setAge(persons.get(j).age);
personItem.setEmail(persons.get(j).email);
personItem.setPhone(persons.get(j).phone);
personItem.setResume(persons.get(j).resume);
personItem.setImageId(persons.get(j).photoId);
personList1.addItem(personItem);
RVAdapter adapter = new RVAdapter(getContext(),persons);
adapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
int pos = position;
Bundle bundle = new Bundle();
bundle.putSerializable("person", personList1);
Intent intent = new Intent(getContext(),
MyActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
rv.setAdapter(adapter);
recycleler view adapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private Context mContext;
private static OnItemClickListener listener;
//AdapterView.OnItemClickListener listener;
// Define the listener interface
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
// Define the method that allows the parent activity or fragment to define the listener
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
PersonViewHolder(final View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
// Setup the click listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(itemView, getLayoutPosition());
}
});
}
}
List<Person> persons;
public RVAdapter(Context context , List<Person> persons){
this.persons = persons;
mContext = context;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return persons.size();
}
}
答案 0 :(得分:0)
为了解决这个问题,我把RvAdapter类作为每个标签片段中的内部和私有类:
private static class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
//...and keep remaning