我创建了一个RecyclerView并在片段中添加了卡片,我想通过在CardView中单击不同的卡片来打开不同的活动,但是我只能为所有卡片打开相同的活动。我在StackOverFlow中搜索了不同的答案。但是我不能。 你能告诉我如何解决这个问题吗?如果您能提供完整的代码,我很高兴。
这是我的密码...
PDFFragment.java
public class PDFFragment extends Fragment {
View v;
List<Pdf> listBook;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v= inflater.inflate(R.layout.fragment_pdf,container,false);
listBook = new ArrayList<>();
listBook.add(new Pdf("1-pdf name",R.drawable.pdf1image));
listBook.add(new Pdf("2-pdf name",R.drawable.pdf2image));
listBook.add(new Pdf("3-pdf name",R.drawable.pdf3image));
listBook.add(new Pdf("4-pdf name",R.drawable.pdf4image);
listBook.add(new Pdf("5-pdf name",R.drawable.pdf5image));
listBook.add(new Pdf("6-pdf name",R.drawable.pdf6image));
listBook.add(new Pdf("7-pdf name",R.drawable.pdf7image));
RecyclerView myTV= (RecyclerView) v.findViewById(R.id.recyclerview_id);
RecyclerViewAdapterPDF myAdapter = new RecyclerViewAdapterPDF(getActivity(),listBook);
//Part 1 -Start Changing number of columns
myTV.setLayoutManager(new GridLayoutManager(getActivity(),calculateNoOfColumns(getActivity())));
//Part 1 -End Changing number of columns
myTV.setAdapter(myAdapter);
return v;
}
//Part 2 -Start Changing number of columns
public int calculateNoOfColumns(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
int noOfColumns = (int) (dpWidth / 120);
return noOfColumns;
}
//Part 2 -End Changing number of columns
}
RecyclerViewAdapterPDF.java
public class RecyclerViewAdapterPDF extends RecyclerView.Adapter<RecyclerViewAdapterPDF.MyViewHolder> {
private Context mContext;
private List<Pdf> mData;
public RecyclerViewAdapterPDF(Context mContext, List<Pdf> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_item_pdf,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder( MyViewHolder holder, int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getPhoto());
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_book_title;
ImageView img_book_thumbnail;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id);
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_image_id);
}
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return FragmentListTitles.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return FragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment,String Title){
fragmentList.add(fragment);
FragmentListTitles.add(Title);
}
}
答案 0 :(得分:2)
您需要在视图持有者上引用您的卡:
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_book_title;
ImageView img_book_thumbnail;
CardView cardview;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id);
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_image_id);
cardview = itemView.findViewById(R.id.yourCardId);
}
}
然后在onBindViewHolder()上设置一个负责打开活动的点击侦听器:
@Override
public void onBindViewHolder( MyViewHolder holder, int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getPhoto());
holder.cardView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(mContext, pdfone.class);
mContext.startActivity(intent);
break;
case 1:
intent = new Intent(mContext, pdftwo.class);
mContext.startActivity(intent);
break;
...
//do the same for every card possible
}
}
}
答案 1 :(得分:0)
使用像this
这样的反射因此,在您的情况下,类似:
String activityToStart = "com.pkg.pdf"+position;
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException e) {
// print exception
}
要更改页面时将其放入适配器。