简体   繁体   中英

Extend RecyclerView and get clicked item's position

I'm trying to extend RecyclerView to make certain modifications of my own. This is what I currently have:

public class FeedRecyclerView extends RecyclerView {

    private int currentClickedItem = -1;

    public FeedRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public FeedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FeedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    // Check if item in RecyclerView is clicked
    // If so, update "currentClickedItem" to the correct position

}

When a user clicks on an item in the RecyclerView, I want to update the value of currentClickedItem in the above code. What method would I use to do this?

Thanks!

You can try something like this:

rv_list.addOnItemTouchListener(
            new RecyclerItemClickListener(activity, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {

                    Toast.makeText(activity, "" + position, Toast.LENGTH_SHORT).show();
                }
            })
    );

This gets you the position of the clicked item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM