SjN*_*erd 13 android popupmenu android-recyclerview
我有一个内部的溢出按钮CardView在Recyclerview.每当我点击按钮时,我会显示一个弹出菜单,但也会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>
Run Code Online (Sandbox Code Playgroud)
适配器:
@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
}
});
}
Run Code Online (Sandbox Code Playgroud)
更新: 遇到问题.当弹出菜单显示时,它会向下滑动列表以显示整个下拉列表.如何根据可用空间调整弹出窗口上/下显示?
SjN*_*erd 31
尝试使用android.widget.PopupMenu而不是android.support.v7.widget.PopupMenu和Voila,它的工作原理.这是支持库中的一个错误.伟大的开发人员可以在这里确认一样吗?
您可以让您的AnchorView覆盖requestRectangleOnScreen()并返回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;
}
}
Run Code Online (Sandbox Code Playgroud)
jankovd的道具(https://gist.github.com/jankovd/19ef35efd1f00e9213fa)
此处针对支持PopupMenu提出了一个问题:https://code.google.com/p/android/issues/detail? id = 135439
| 归档时间: |
|
| 查看次数: |
2558 次 |
| 最近记录: |