1

I have created one horizontal recycle view with card view. I have created single card and shown it multiple times in the recycle view. The data has been shown from the web services. Now what here is the UI of app

enter image description here

Now what i have to do is when i click on any card then one activity should open. I have created one activity, that activity should get open when i click on any card. And base on the card data, the data should be change in the activity.

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {

private List<Data> dataList;
private Context mContext;

public RecyclerViewDataAdapter(Context context, List<Data> dataList) {
    this.dataList = dataList;
    this.mContext = context;
}

@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
    ItemRowHolder mh = new ItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {

    final String sectionName = dataList.get(i).getTitle();

    List singleSectionItems = dataList.get(i).getSection();

    itemRowHolder.itemTitle.setText(sectionName);

    SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);

    itemRowHolder.recycler_view_list.setHasFixedSize(true);
    itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);


     itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);


   /*  itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                    //Allow ScrollView to intercept touch events once again.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            // Handle RecyclerView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });*/

    itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Toast.makeText(v.getContext(), "click event on more, "+sectionName , Toast.LENGTH_SHORT).show();



        }
    });







   /* Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/
}

@Override
public int getItemCount() {
    return (null != dataList ? dataList.size() : 0);
}

public class ItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitle;

    protected RecyclerView recycler_view_list;

    protected Button btnMore;



    public ItemRowHolder(View view) {
        super(view);

        this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
        this.btnMore= (Button) view.findViewById(R.id.btnMore);


    }

}

}

another class

public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {

private List<Section> itemsList;
private Context mContext;



public SectionListDataAdapter(Context context, List<Section> itemsList) {
    this.itemsList = itemsList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    Section singleItem = itemsList.get(i);

    holder.tvTitle.setText(singleItem.getName());



}

@Override
public int getItemCount() {
    return (null != itemsList ? itemsList.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView tvTitle;

    protected ImageView itemImage;


    public SingleItemRowHolder(View view) {
        super(view);

        this.tvTitle = (TextView) view.findViewById(R.id.tvTitle1);
        this.itemImage = (ImageView) view.findViewById(R.id.itemImage);


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(v.getContext(), BooksDescription.class);
                mContext.startActivity(intent);

            }
        });


    `}

}}`
OmiK
  • 2,606
  • 1
  • 19
  • 35
Fazeel Qureshi
  • 727
  • 8
  • 16

4 Answers4

1

1- you can pass your desired data(for example :id or whole object) to your activity and then handle that in your activity: Intent.putExtra("YOUR_KEY","YOUR_SERIALIZED_OBJECT_OR_STRING");

2-please do not setOnClickListener in your onBindViewHolder(in your first adapter) , it is better to put it in your viewHolder.

Reza.Abedini
  • 1,797
  • 1
  • 13
  • 17
1

You can pass data using intent or Bundle:

e.g.

Using Intent:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

Using Bundle:

Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
Naitik
  • 960
  • 11
  • 31
0

U need to implement item click listener . to implement recyclerview item clicklistner follow this link

And then Pass your data through intent , this link

will help you in passing data to activity and then getting it on the other side

Adeel Turk
  • 877
  • 8
  • 21
  • you are using clicklistner of cardview in viewholder which will work but its good to implement itemclikclistner if you hae a single action in whole item – Adeel Turk Jul 18 '17 at 06:12
0

Just add below code in your adapter OnClick Listener

Intent intent = new Intent(v.getContext(), BooksDescription.class);
intent.putExtra("YOUR KEY", yourData);
v.getContext().startActivity(intent);