Click a RecyclerView List Item

I have a RecyclerView with LinearLayoutManager and Adapter :

 @Override public int getItemViewType(int position) { return position == 0? R.layout.header : R.layout.item; } 

Here header.xml :

 <View xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/header" android:layout_width="match_parent" android:layout_height="@dimen/header_height" /> 

I want to reach a hole in RecyclerView where I can go to something behind RecyclerView . I tried many combinations for the following attributes:

  android:background="@android:color/transparent" android:background="@null" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:longClickable="false" 

How can I make the first element transparent (allow touch events to something behind) in the list, but let it still take up space?

+6
source share
1 answer

You can set the OnClickListener / OnTouchListener all your "holes" to the parent view element behind the RecyclerView and delegate any of the MotionEvent and touch events to this parent ViewGroup touch processing.

Update using TWiStErRob:

 class HeaderViewHolder extends RecyclerView.ViewHolder { public HeaderViewHolder(View view) { super(view); view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // http://stackoverflow.com/questions/8121491/is-it-possible-to-add-a-scrollable-textview-to-a-listview v.getParent().requestDisallowInterceptTouchEvent(true); // needed for complex gestures // simple tap works without the above line as well return behindView.dispatchTouchEvent(event); // onTouchEvent won't work } }); 

There is nothing special, simple in XML (none of the attributes in the question). v.getParent() is a RecyclerView , and this long method stops it from triggering scroll gestures by holding its finger on the hole.

In my view, the β€œhole” in the list also has a translucent background, but it does not matter, because we deliver our orders by hand.

+8
source

Source: https://habr.com/ru/post/983834/


All Articles