美文网首页Android开发Android技术知识Android知识
RecyclerView添加ContextMenu易错细节

RecyclerView添加ContextMenu易错细节

作者: dooze | 来源:发表于2017-12-31 23:12 被阅读245次

    RecyclerView是默认不能想ListView一样支持ContextMenu的,不过可以通过重写以下两个方法来实现ContextMenu:

    protected ContextMenu.ContextMenuInfo getContextMenuInfo(){...}
    
     @Override
        public boolean showContextMenuForChild(View originalView) {
            int pos = getChildAdapterPosition(originalView);
            long itemId= getChildItemId(originalView);
            contextMenuInfo = new AdapterView.AdapterContextMenuInfo(originalView,pos,itemId);
            return super.showContextMenuForChild(originalView);
        }
    

    然后在Activity或者Fragment中重写为RecyclerView设置:

     listRv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                //这里添加菜单内容
                    menu.add(0,1,1,"xxxx");
                }
            });
    
    //重写
    @Override
        public boolean onContextItemSelected(MenuItem item) {
            swithc(item.getItemId()){
            // .... 业务逻辑
    }
            return super.onContextItemSelected(item);
        }
    

    具体重写的代码细节可以参考文章: 为RecycleView添加ContextMenu支持

    以上基本流程就完成了正常情况长按RecyclerView中的条目就会弹出选项菜单了。

    最容易忽略的细节就是为RecuclerView的item设置允许长按

    //代码设置
    rl.setLongClickable(true);
    
    //xml中设置
    android:longClickable="true"
    

    而且设置长按必须而且仅能能在最外层布局设置允许长按
    意外时正确设置(假设这是RecyclerView的item不的布局文件)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- 就是这一句话-->
        android:longClickable="true"
        
        android:orientation="vertical">
    
        <!--其他view -->
        <RelativeLayout
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="100dp" >
            <!--其他view -->
            </RelativeLayout>
    </LinearLayout>
    

    假如设置了多个 longClickable 就像下面这样:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- 就是这一句话-->
        android:longClickable="true"
        
        android:orientation="vertical">
    
        <!--其他view -->
        <RelativeLayout
            android:id="@+id/parent"
            <!-- 就是这一句话-->
            android:longClickable="true"
            android:layout_width="match_parent"
            android:layout_height="100dp" >
            <!--其他view -->
            </RelativeLayout>
    </LinearLayout>
    

    这时RecyclerView中的条目时就会发生如下类似的异常 ,主要是xxx.LayoutParams 不能转换成RecyclerView$LayoutParams的ClassCastException

      java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
                                                                       at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:4314)
                                                                       at android.support.v7.widget.RecyclerView.getChildAdapterPosition(RecyclerView.java:4333)
                                                                       at dong.lan.test.ContextMenuRecyclerView.showContextMenuForChild(ContextMenuRecyclerView.java:41)
    

    而最外层布局不设置长按允许,而在内层View或者ViewGroup设置长按允许,也会发生如上类似的 ClassCastException
    例如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- 这里不设置了,这是步行的!! android:longClickable="true"-->
        
        
        android:orientation="vertical">
    
        <!--其他view -->
        <RelativeLayout
            android:id="@+id/parent"
            <!-- 就是这一句话-->
            android:longClickable="true"
            android:layout_width="match_parent"
            android:layout_height="100dp" >
            <!--其他view -->
            </RelativeLayout>
    </LinearLayout>
    

    最后,设置长按必须而且仅能能在最外层布局设置允许长按

    相关文章

      网友评论

        本文标题:RecyclerView添加ContextMenu易错细节

        本文链接:https://www.haomeiwen.com/subject/zykzgxtx.html