单击RecyclerView列表项

时间:2015-03-15 21:50:58

标签: android android-layout android-listview android-recyclerview recycler-adapter

我有RecyclerView LinearLayoutManagerAdapter

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

此处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"
    />

我想要实现的是在RecyclerView中有一个漏洞,我可以点击RecyclerView后面的任何内容。我尝试了很多组合以下属性无济于事:

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

如何让列表中的第一个项目透明(允许触摸事件到其后面的任何内容),但让它仍然占据空间?

1 个答案:

答案 0 :(得分:10)

您可以设置所有&#34;漏洞的OnClickListener / OnTouchListener&#34;到RecyclerView后面的观看者的父级,并将MotionEvent和任何触摸事件委托给该父ViewGroup的触摸处理。

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
            }
        });

XML中没有特别需要的东西,只是普通的视图(问题中没有属性)。 v.getParent()RecyclerView,这种长方法可以阻止它在将手指放在&#34;&#34;上时开始滚动手势。

我的&#34;洞&#34;列表中的视图也具有半透明背景,但这并不重要,因为我们会手动传递触摸。

相关问题