我使用视图分页器在父片段中包含3个子片段。在第一个子片段上,我有一个回收者视图列表。我的自定义适配器中有一个on Click和onLongClick,第一个片段中有一个click侦听器。适配器似乎可以正常工作,因为在onBind内,我可以更改列表位置的背景,但是在我的第一个片段上,如果我在单击侦听器内不进行Toast,则什么也没发生。但是,如果我滑动到最后一个片段并返回到第一个片段,它将开始工作。我不明白发生了什么。
我的父母片段:
public class EventFragment extends Fragment implements FragmentInterface{
View view;
ViewPager viewPager;
TabLayout tabLayout;
private String idDiscipline;
public EventFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_event,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager=(ViewPager)view.findViewById(R.id.event_view_pager);
tabLayout=view.findViewById(R.id.event_tab_layout);
tabLayout.setupWithViewPager(viewPager);
SectionPagerAdapter adapter=new SectionPagerAdapter(getChildFragmentManager());
adapter.addFrag(new Child1Fragment();,"Classes");
adapter.addFrag(new Child2Fragment(),"Specials");
adapter.addFrag(new Child3Fragment(),"Events");
viewPager.setAdapter(adapter);
}
}
我的PagerAdapter
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
我的孩子1片段
public class Child1Fragment extends Fragment {
private Realm realm;
private RealmResults<Classe> classList;
private RecyclerView rvNormalClass;
private ClassAdapter classAdapter;
public Child1Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_child1, container, false);
realm = Realm.getDefaultInstance();
classList = realm.where(MyClass.class).findAll();
rvNormalClass = view.findViewById(R.id.rv__parent_class);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
rvNormalClass.setLayoutManager(layoutManager);
classAdapter = new ClassAdapter(getContext(), classList);
rvNormalClass.setAdapter(classAdapter);
classAdapter.setOnItemClickListener(new ClassAdapter.ClickListener() {
@Override
public void onItemClick(ClassAdapter adapter, View v, int position) {
}
@Override
public void onItemLongClick(ClassAdapter adapter, View v, int position) {
}
});
return view;
}
}
我的RecyclerView列表的ClassAdapter
public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ParentHolder> {
private List<Classe> dataset;
private static ClickListener clickListener;
private Context mContext;
static int myIndex = -1;
public ClassAdapter(Context mContext, List<Classe> dataset) {
this.dataset = dataset;
this.mContext = mContext;
}
public List<Classe> getClassDataSet() {
return dataset;
}
public static class ParentHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private TextView classType;
private ClassAdapter adapterRef;
public ParentHolder(@NonNull View containingView, ClassAdapter ref) {
super(containingView);
classType=containingView.findViewById(R.id.tv_class_title);
adapterRef = ref;
containingView.setOnLongClickListener(this);
containingView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(adapterRef, v, getAdapterPosition());
}
@Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(adapterRef, v, getAdapterPosition());
myIndex = getAdapterPosition();
adapterRef.notifyDataSetChanged();
return true;
}
}
@NonNull
@Override
public ParentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_class, parent, false);
ParentHolder pvh = new ParentHolder(v, this);
return pvh;
}
@Override
public void onBindViewHolder(@NonNull ParentHolder holder, int position) {
holder.classType.setVisibility(View.VISIBLE);
if (myIndex==position) {
holder.classType.setBackgroundColor(Color.parseColor("#EF9A9A"));
myIndex = -1;
} else {
holder.classType.setBackgroundColor(Color.parseColor("#FFFFFF"));;
}
}
@Override
public int getItemCount() {
return this.dataset.size();
}
public void setOnItemClickListener(ClickListener clickListener) {
ClassAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(ClassAdapter adapter, View v, int position);
void onItemLongClick(ClassAdapter adapter, View v, int position);
}
}
我的父母片段xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.EventFragment">
<LinearLayout
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/event_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<android.support.design.widget.TabLayout
android:id="@+id/event_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/event_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
</android.support.constraint.ConstraintLayout>