0

I am Parsing Json data in fragment. To get a view of this Json data I used RecycleView. In this fragment I showed short detail of some post. I want that when user click the recycleView it will go to another activity and show details of this post. How to make my RecycleView clickable?

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recent_post, container, false);
        recentpostRecycleView = (RecyclerView) view.findViewById(R.id.recent_posts);
        recentpostRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recentPostAdapter = new RecentPostAdapter(getActivity());
        recentpostRecycleView.setAdapter(recentPostAdapter);
        sendJsonRequest();
        return  view;

    }  

My RecycleView ScreenShoot:

enter image description here

Yeahia2508
  • 7,526
  • 14
  • 42
  • 71

2 Answers2

2

You should not make the RecyclerView clickable, but its cards. Just add android:clickable="true" to the card layout and then add a ClickListener to the ChildViews. If you struggle with that, slidenerd has a nice tutorial about RecyclerViews: https://www.youtube.com/watch?v=zE1E1HOK_E4

StG
  • 257
  • 2
  • 11
1

Have a look at this answer, I think it best describes: https://stackoverflow.com/a/24471410/5243853

I am currently using a similar solution to handle clicks on items of RecycleView, but I set listener in onBindViewHolder method of the adapter. Also, you can not only set onClickListener to whole item view, but also to its elements, if needed.

In your adapter implementation you can have:

@Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Good good = getItem(position);
        Picasso.with(mContext)
                .load(good.mainImages)
                .placeholder(R.drawable.picasso_placeholder_normal)
                .into(viewHolder.mImage, null);
        viewHolder.mTitle.setText(good.name);
        viewHolder.mCount.setText(good.count);
        viewHolder.parent.setOnClickListener(viewHolder); }

And inner class of the adapter looks like this:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private View parent;
        private ImageView mImage;
        private TextView mTitle;
        private TextView mCount;

        public ViewHolder(View parentView, ImageView image, TextView title, TextView count) {
            super(parentView);
            parent = parentView;
            mImage = image;
            mTitle = title;
            mCount = count;
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            Good good = getItem(position);
            if (good != null) {
                //requestProductCard(good.id, v);   //do your stuff here
            }
        }
    }
Community
  • 1
  • 1
Daelinn
  • 26
  • 4
  • I found one solution from slidenard with addOnItemTouchListener. It's allowing to add onLongTouch. Nice one. – Yeahia2508 Aug 19 '15 at 17:54