我似乎无法找到使用具有不同布局的相同recyclerview适配器的正确方法。
因为我试图对不同的片段使用不同的布局,所以我尝试用相同的方法制作另一个适配器..它确实有效,但是存在滞后(延迟响应),我敢肯定有办法,但是我甚至都不知道怎么做……
这是我的recyclerAdapter类
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<featuresItemList> mfeaturesArray;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView mImageAvater;
TextView mTextViewAvater;
public ViewHolder(View itemView){
super(itemView);
context = itemView.getContext();
mImageAvater = itemView.findViewById(R.id.avatar_picture);
mTextViewAvater = itemView.findViewById(R.id.avatar_text);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Intent intent;
switch (getPosition()) {
case 0:
intent = new Intent(context, Activity1.class);
break;
case 1:
intent = new Intent(context, Activity2.class);
break;
case 2:
intent = new Intent(context, Activity3.class);
break;
case 3:
intent = new Intent(context, Activity4.class);
break;
case 4:
intent = new Intent(context, Activity5.class);
break;
default:
intent = new Intent(context,something.class);
break;
}
context.startActivity(intent);
}
}
public recyclerAdapter(Context context, ArrayList<featuresItemList> featuresArray){
this.context = context;
inflater = LayoutInflater.from(context);
mfeaturesArray = featuresArray ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.features_list_layout , parent , false );
ViewHolder holder = new ViewHolder (view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
featuresItemList currentItem = mfeaturesArray.get(position);
holder.mImageAvater.setImageResource(currentItem.getImageResource());
holder.mTextViewAvater.setText(currentItem.getImageText());
}
@Override
public int getItemCount() {
return mfeaturesArray.size();
} }
这是我现在正在使用的片段..
public class FragmentThree extends Fragment {
private RecyclerView.Adapter mAdapter;
//
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<featuresItemList> featuresitems ;
public FragmentThree() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
featuresitems = new ArrayList<>();
featuresitems.add(new featuresItemList(R.drawable.ic , "one"));
featuresitems.add(new featuresItemList(R.drawable.ic_ , "two"));
featuresitems.add(new featuresItemList(R.drawable.ic , "three "));
featuresitems.add(new featuresItemList(R.drawable.ic , "four"));
featuresitems.add(new featuresItemList(R.drawable.ic_, "five"));
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.myrecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager( requireContext() );
// ToDo made a change here
mAdapter = new recyclerAdapter(getContext(), featuresitems);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view ; } }
基本上,上面的代码可以帮助我建立一个列表,并且单击的每个项目都可以按计划将我带到新的活动。
,并且我确实想使用相同的代码对我制作的第二个片段执行相同的操作。具有新布局的每个项目也应打开新活动...
那怎么办呢?
注意:对不起,我的编码能力很差,只是在做我的毕业设计。