PopupMenu单击导致RecyclerView滚动 [英] PopupMenu click causing RecyclerView to scroll

查看:278
本文介绍了PopupMenu单击导致RecyclerView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RecyclerviewCardView内有一个溢出按钮.每当我单击按钮时,都会显示一个弹出菜单,但RecyclerView也会向下滚动一个项目.谁能帮我阻止这种不必要的滚动?

I have an overflow button inside a CardView in Recyclerview. Whenever I am clicking the button,I show a popup menu but also RecyclerView is scrolling down one item. Can anyone please help me stop this unwanted scrolling?

基本上,我正在尝试复制与Playstore中相同的溢出按钮行为.

Basically I am trying to replicate the same overflow button behavior as in 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
        }
    });

}

更新: 遇到了问题.当弹出菜单显示时,它会将列表向下滑动,以显示整个下拉列表.如何根据可用空间将弹出窗口调整为向上/向下显示?

UPDATE: Got the issue .When the popup menu displays,it slides the list down so as to display the whole dropdown. How to adjust popup to display up/down depending on the space available?

推荐答案

尝试使用android.widget.PopupMenu而不是android.support.v7.widget.PopupMenu和Voila,它可以正常工作.支持库中的错误也是如此.优秀的开发人员可以在这里确认吗?

Tried using android.widget.PopupMenu instead of android.support.v7.widget.PopupMenu and Voila, it works. So is it a bug in Support library. Can great developers out here confirm the same?

这篇关于PopupMenu单击导致RecyclerView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆