RecyclerView實現list,item分兩段的分區域按下問題記錄

  1. list顯示效果如圖,list item分左右兩個部分,左邊部分按下會變色,右邊部分點擊無效果。一些item只有左邊部分
  2. list item佈局如下,左右部分都爲ImageView實現

  3. 一個item包括左右兩個ImageView,無圖預覽:
  4. 左邊部分想通過ImageButton顯示,但沒找到在list中如何監聽左邊ImageButton的方法。只能監聽整個item(剛學習Android沒多久。。)在item右邊沒有ImageView的情況下,點擊item右側空白區域,左側也會有按下變色。通過判斷右側按下的x座標(程序中爲大於525)時,在OnTouch中返回true,就可以實現點擊item右側而左側沒有按下變色。過程中發現,如果只監聽OnTouch,item設置爲ImageView,按下不會變色。只監聽OnClick就得不到按下的x座標,所以兩個都進行了監聽。
  5. item的佈局代碼
    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.constraint.ConstraintLayout
    	xmlns:android="http://schemas.android.com/apk/res/android"
    	xmlns:app="http://schemas.android.com/apk/res-auto"
    	xmlns:tools="http://schemas.android.com/tools"
    	android:layout_width="711px"
    	android:layout_height="54px"
    	tools:ignore="MissingPrefix"
    	>
    
    	<ImageView
    		android:id="@+id/list_item_leftpart"
    		android:layout_width="528px"
    		android:layout_height="54px"
    		android:layout_marginLeft="0px"
    		android:layout_marginTop="0px"
    		app:layout_constraintStart_toStartOf="parent"
    		app:layout_constraintTop_toTopOf="parent" />
    
    	<ImageView
    		android:id="@+id/list_item_rightpart"
    		android:layout_width="184px"
    		android:layout_height="54px"
    		android:layout_marginLeft="527px"
    		android:layout_marginTop="0px"
    		app:layout_constraintStart_toStartOf="parent"
    		app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    
  6. RecyclerView實現list,Adapter中的
    onBindViewHolder函數如下:
    public void onBindViewHolder(VH holder, final int position) {
    
            /*設置為不複用,對應圖片錯亂問題*/
            holder.setIsRecyclable(false);
    
            holder.list_left.setImageResource(mDatas[position]);
            LogPrint.i(TAG,"left " + (position + 1));
    
            if(mDatas[position + 12] != 0)
            {
                holder.list_right.setImageResource(mDatas[position + 12]);
            }
    
            /*如果不監聽OnClick,只監聽OnTouch會有ImageView押下不變色問題*/
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                }
            });
    
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    /*點右側區域按鍵不變色*/
                    if(event.getX() > 525)
                    {
                        LogPrint.i(TAG,"Touch out of range.");
                        return true;
                    }
                    
                    /*item 點擊事件*/
                    if(event.getAction() == MotionEvent.ACTION_UP) {
                        switch (position) {
                            case 0:
                                break;
                            case 1:
                                break;
                            case 2:
                                break;
                            case 3:
                                break;
                        }
                    }
                    return false;
                }
            });
        }
  7. item按下變色對應xml代碼
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/menu_lay3_setup_nor_language_03"/>
        <item android:state_pressed="true" android:drawable="@drawable/menu_lay3_setup_nor_language_02"/>
        <item android:state_enabled="false" android:drawable="@drawable/menu_lay3_setup_nor_language_04"/>
        <item android:drawable="@drawable/menu_lay3_setup_nor_language_01"/>
    </selector>

     

  8. 僅作爲個人學習記錄。

 

發佈了15 篇原創文章 · 獲贊 10 · 訪問量 5856
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章