PopupMenu 单击导致 RecyclerView 滚动
PopupMenu click causing RecyclerView to scroll
我在 Recyclerview
中的 CardView
中有一个溢出按钮。每当我单击按钮时,我都会显示一个弹出菜单,但 RecyclerView
也会向下滚动一个项目。谁能帮我阻止这种不需要的滚动?
基本上我正在尝试复制与 Playstore 中相同的溢出按钮行为。
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<android.support.v7.widget.CardView
android:id="@+id/cv_tracks"
android:layout_width="match_parent"
android:layout_height="65dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/ivTracks"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/unknown"
/>
<Button
android:id="@+id/imgbtn_overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableTop="@drawable/ic_overflow"
android:paddingLeft="25dp"
android:paddingRight="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
适配器:
@Override
public void onBindViewHolder(final TracksViewHolder tracksViewHolder, int i) {
tracksViewHolder.imgBtnOverflow.setTag(i);
tracksViewHolder.imgBtnOverflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
final int position = (Integer) v.getTag();
PopupMenu popup = new PopupMenu(mContext,tracksViewHolder.imgBtnOverflow);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu_overflow, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(mPopUpClick!=null)
mPopUpClick.actionOnPopUpItemClick(position,item,songs.get(position));
return true;
}
});
popup.show(); //showing popup menu
}
});
}
更新:
明白了。当弹出菜单显示时,它向下滑动列表以显示整个下拉菜单。如何根据可用的 space 调整弹出窗口以显示 up/down?
尝试使用 android.widget.PopupMenu 而不是 android.support。v7.widget.PopupMenu 瞧,它有效。那么它是支持库中的错误吗?伟大的开发人员可以证实这一点吗?
您可以让 AnchorView 覆盖 requestRectangleOnScreen()
和 return false。这将阻止任何父 ScrollView
滚动。
因此,在您的情况下,imgBtnOverflow
将是自定义 ImageButton
,如下所示:
/**
* A custom {@link ImageButton} which prevents parent ScrollView scrolling when used as the
* anchor for a {@link android.support.v7.widget.PopupMenu}
*/
public class NonScrollImageButton extends ImageButton {
public NonScrollImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
return false;
}
}
支持 jankovd (https://gist.github.com/jankovd/19ef35efd1f00e9213fa)
已针对此处的 Support PopupMenu 提出问题:
https://code.google.com/p/android/issues/detail?id=135439
requestRectangleOnScreen
对我不起作用,就我而言,requestRectangleOnScreen
显示弹出菜单时未调用。
我的解决方案是覆盖 requestChildRectangleOnScreen
,并返回 false,效果很好。
public class PopupAncorRecyclerViewPager extends RecyclerView {
@Override
public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
return false;
}
}
我也有同样的问题,我是这样使用的:
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
};
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView 用户 layoutmanager 来管理布局,所以我认为更改 layoutmanger 行为是更好的解决方案。
我在 Recyclerview
中的 CardView
中有一个溢出按钮。每当我单击按钮时,我都会显示一个弹出菜单,但 RecyclerView
也会向下滚动一个项目。谁能帮我阻止这种不需要的滚动?
基本上我正在尝试复制与 Playstore 中相同的溢出按钮行为。
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<android.support.v7.widget.CardView
android:id="@+id/cv_tracks"
android:layout_width="match_parent"
android:layout_height="65dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/ivTracks"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/unknown"
/>
<Button
android:id="@+id/imgbtn_overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableTop="@drawable/ic_overflow"
android:paddingLeft="25dp"
android:paddingRight="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
适配器:
@Override
public void onBindViewHolder(final TracksViewHolder tracksViewHolder, int i) {
tracksViewHolder.imgBtnOverflow.setTag(i);
tracksViewHolder.imgBtnOverflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
final int position = (Integer) v.getTag();
PopupMenu popup = new PopupMenu(mContext,tracksViewHolder.imgBtnOverflow);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu_overflow, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(mPopUpClick!=null)
mPopUpClick.actionOnPopUpItemClick(position,item,songs.get(position));
return true;
}
});
popup.show(); //showing popup menu
}
});
}
更新: 明白了。当弹出菜单显示时,它向下滑动列表以显示整个下拉菜单。如何根据可用的 space 调整弹出窗口以显示 up/down?
尝试使用 android.widget.PopupMenu 而不是 android.support。v7.widget.PopupMenu 瞧,它有效。那么它是支持库中的错误吗?伟大的开发人员可以证实这一点吗?
您可以让 AnchorView 覆盖 requestRectangleOnScreen()
和 return false。这将阻止任何父 ScrollView
滚动。
因此,在您的情况下,imgBtnOverflow
将是自定义 ImageButton
,如下所示:
/**
* A custom {@link ImageButton} which prevents parent ScrollView scrolling when used as the
* anchor for a {@link android.support.v7.widget.PopupMenu}
*/
public class NonScrollImageButton extends ImageButton {
public NonScrollImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
return false;
}
}
支持 jankovd (https://gist.github.com/jankovd/19ef35efd1f00e9213fa)
已针对此处的 Support PopupMenu 提出问题: https://code.google.com/p/android/issues/detail?id=135439
requestRectangleOnScreen
对我不起作用,就我而言,requestRectangleOnScreen
显示弹出菜单时未调用。
我的解决方案是覆盖 requestChildRectangleOnScreen
,并返回 false,效果很好。
public class PopupAncorRecyclerViewPager extends RecyclerView {
@Override
public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
return false;
}
}
我也有同样的问题,我是这样使用的:
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
};
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView 用户 layoutmanager 来管理布局,所以我认为更改 layoutmanger 行为是更好的解决方案。