NestedScrollView& RecyclerView,onLongPress事件

时间:2016-02-02 04:41:49

标签: android android-recyclerview nestedscrollview

我有布局:          

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#99000000"
            android:layout_marginRight="3dp"
            android:layout_marginEnd="3dp"
            android:layout_marginBottom="15dp"
            android:layout_gravity="start|bottom"
            android:padding="2dp">
            <Spinner
                android:id="@+id/spinnerPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </Spinner>
        </LinearLayout>
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text=""
            android:textStyle="bold"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">
        </android.support.v7.widget.RecyclerView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.CardContent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/title1"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text=""
                    android:textColor="@color/black"/>
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.CardContent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/title2"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text=""
                    android:textColor="@color/black"/>

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.CardContent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title5"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"
                    android:layout_marginBottom="16dp" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/contacts_recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </android.support.v7.widget.RecyclerView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/title3"/>
            </LinearLayout>

        </android.support.v7.widget.CardView>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:gravity="end"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@mipmap/ic_add_white_18dp"
    app:layout_anchor="@id/collapsing_toolbar"
    app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>

在.java文件中:

RecyclerView mRecyclerView = (RecyclerView) mActivity.findViewById(R.id.recyclerview);
mContactsRecyclerView.setNestedScrollingEnabled(false);
mContactsRecyclerView.setHasFixedSize(true);
mContactsRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(mActivity, mContactsRecyclerView,
    new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(mActivity, "OnItemClick", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onItemLongClick(View view, int position) {
            Toast.makeText(mActivity, "onItemLongClick", Toast.LENGTH_SHORT).show();
        }
}));
mContactsRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                Math.round(recyclerData.size() * mActivity.getResources().getDimension(R.dimen.recycler_row))));

RecyclerItemClickListener.java:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
{
    public interface OnItemClickListener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
    {
        mListener = listener;

        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
        {
            @Override
            public boolean onSingleTapUp(MotionEvent e)
            {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e)
            {
                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

                if(childView != null && mListener != null)
                {
                    mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
    {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
        {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
}

它正常工作,但是OnLongClick事件有问题,如果开始滚动项目RecyclerView,它会调用onItemLongClick
如何阻止事件RecyclerView onItemLongClick滚动NestedScrollView?

1 个答案:

答案 0 :(得分:1)

找到解决方案,需要在Adapter ViewHolder中实现OnClickListener,OnLongClickListener,并从RecyclerView中删除侦听器。

public class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener, AdapterView.OnLongClickListener {
    CardView cv;

    ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);

        cv.setOnClickListener(this);
        cv.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view != null) {
            int itemPosition = getAdapterPosition();
            switch (view.getId()) {
                case R.id.cv:
                    //
                    break;
            }
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (view != null) {
            int itemPosition = getAdapterPosition();
            switch (view.getId()) {
                case R.id.cv:
                    //
                    break;

            }
        }
        return false;
    }
}