Open different activities by clicking on different cards in CardView ,Recyclerview Fragment in Android studio

自古美人都是妖i 提交于 2020-08-10 22:53:37

问题


I have created a RecyclerView and added cards in fragment.I want to open different activities by clicking on different cards in CardView.But I am only able to open the same activity for all the cards.I searched different answers in StackOverFlow.But I couldn't. could you tell me how to solve this issue...? If you can give full code,I'm happy.

Here are my codes...

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);
    }
}

回答1:


You need a reference to your card on the view holder:

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);
        }
     }

And on the onBindViewHolder() you set a click listener responsible for opening the activity:

@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
                   } 
                 }

}



回答2:


Use reflection like this

So in your case, something like:

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
    }

Put it in adapter when page is to be changed.



来源:https://stackoverflow.com/questions/52096024/open-different-activities-by-clicking-on-different-cards-in-cardview-recyclervi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!