gpt4 book ai didi

android - NestedScrollView & RecyclerView, onLongPress 事件

转载 作者:行者123 更新时间:2023-11-30 01:35:38 27 4
gpt4 key购买 nike

我有布局:

<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
如何防止滚动 NestedScrollView 上的事件 RecyclerView onItemLongClick

最佳答案

找到解决方案,需要在 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;
}
}

关于android - NestedScrollView & RecyclerView, onLongPress 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35145188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com