【问题标题】:Click through a RecyclerView list item单击 RecyclerView 列表项
【发布时间】:2015-03-15 21:50:58
【问题描述】:

我有一个RecyclerView 和一个LinearLayoutManager 和一个Adapter

@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"

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

【问题讨论】:

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


    【解决方案1】:

    您可以将所有“洞”的OnClickListener/OnTouchListener 设置为RecyclerView 后面的视图的父级,并将任何MotionEvent 和触摸事件委托给该父级ViewGroup' s 触摸处理。

    由 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 并且长方法会阻止它在将手指放在“孔”上时开始滚动手势。

    我在列表中的“洞”视图也有一个半透明的背景,但这并不重要,因为我们是手动进行触摸的。

    【讨论】:

    • 嗯,有趣 :) 我想要复杂的手势工作(例如滑动),所以只有触摸监听器值得尝试。
    • 另外,您可以允许RecyclerView 识别您想要处理的任何触摸处理的任何漏洞,如果事件是神圣的,则委派到后面;)
    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多