1

I have an RecyclerView Adapter

public class CardViewDataMainAdapter extends RecyclerView.Adapter<CardViewDataMainAdapter.ViewHolder> {
        public String[] mColorData;

        public CardViewDataMainAdapter(String[] colorData) {
            mColorData = colorData;
        }

        @Override
        public CardViewDataMainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_row, null);
            ViewHolder viewHolder = new ViewHolder(itemLayoutView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {

            viewHolder.card_text.setText(mColorData[position].toString());

        }

        @Override
        public int getItemCount() {
            return mColorData.length;
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {

            public TextView card_text;

            public ViewHolder(View itemLayoutView) {
                super(itemLayoutView);
                card_text = (TextView) itemLayoutView.findViewById(R.id.info_text);
            }

            @Override
            public void onClick(View v) {
            }
        }
    }

And I have a String Array which defines the data for the cards. There are around 20 card items and I want to start new intents which start another RecyclerView activities for every elements using either if else loop or switch case loop

I've tried searching how to implement OnClickListener for RecyclerView but every solution either starts the same intent for all the Card items, or gives some or the other error

I can post my entire project if needed.

https://i.stack.imgur.com/POcqA.jpg

Chinmay
  • 119
  • 2
  • 11
  • The Intent that you want to launch. What data do you want to place in it? Am I correct in saying you want to pass the color data you bind to the viewholder? – asadmshah Nov 30 '15 at 15:20
  • @asadmshah I guess that's correct. I have my color data string array that makes up various cards and for every card, I wish to start a different intent. Do you want me to post a screenshot for reference? – Chinmay Nov 30 '15 at 15:29
  • 1
    I understand you want a different Intent but what about the Intent do you want to be different? The extras inside it or do you plan on calling an entirely different activity based on the view selected? Yes, a screenshot might be helpful. – asadmshah Nov 30 '15 at 15:32
  • Oh, I'm sorry. I want to start an entirely new activity. I want to make an expandable view of new set of cards in future but I want to learn this right now. P.S. I'm making a static app of material design color swatches. – Chinmay Nov 30 '15 at 15:37
  • I know. What I'm asking is if you have something like Activity1, Activity2, Activity3 that you plan on launching depending on the View clicked or whether you want to launch a new ActivityWhatever but just change the Intent Extras data inside it? – asadmshah Nov 30 '15 at 15:40
  • I'm sorry, but I didn't know about 'just changing data'. Is it also possible to change card styles after doing so (like change the corner radius, etc)? I'd love go the other way if this is possible. But i'd also like to know the first way of starting Activity1, Activity2 and so on. – Chinmay Nov 30 '15 at 15:46

1 Answers1

1

Nice question. Follow this or this.

Basically you can add click listener in onCreateViewHolder() and have your adapter imeplemt OnClickListener so you have single onClick() in which you cna have a switch or if() based on position.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • Yes, I've seen this before, but as I'm still learning, I find it confusing. `single onClick() in which you can have a switch or if() ` Can you explain more or show an example? – Chinmay Nov 30 '15 at 17:55
  • I figured it out myself. Thank you! – Chinmay Nov 30 '15 at 20:43