2014-11-30 4 views
1

사용자 지정 listSelector를 listView에 설정하는 방법을 알아 내려고하고 있습니다. 코드는 더 많은 또는 덜 복사 & stackoverflow에서 다른 사람들을 위해 일한 솔루션에서 붙여 넣기지만, 나를 위해 작동하지 않습니다.사용자 지정 listView에서 사용자 지정 listSelector를 만드는 방법

CAB 모드를 켜면 항목을 선택하거나 선택 취소 할 수 있으며 CAB 탐색 모음에서 선택된 항목의 수가 표시되지만 목록에 아무런 표시가 표시되지 않습니다. 내가 선택한 항목을보고 싶습니다. 왜 그것이 작동하지 않는지?

레이아웃/category_list.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/categories_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
      android:id="@+id/category_browser" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:dividerHeight="1dp" 
        android:listSelector="@drawable/list_selector" 
        android:drawSelectorOnTop="true" 
      tools:listitem="@layout/category_list_item" > 
    </ListView> 
    <include layout="@layout/ad"/> 
</LinearLayout> 

레이아웃/category_list_item.xml :

<?xml version="1.0" encoding="utf-8"?> 
<com.fletech.smartbaby.api.CheckableLinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/category_browser_list_item" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:clickable="true" 
     android:gravity="fill" 
     android:orientation="horizontal"> 
     <ImageView 
       android:id="@+id/category_item_icon" 
       android:layout_width="90dp" 
       android:layout_height="90dp" 
       android:layout_gravity="fill" 
       android:layout_weight="0" 
         android:background="#ffffff" 
       android:src="@drawable/ic_launcher" /> 
     <TextView 
       android:id="@+id/category_browser_list_title" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="center_vertical|start" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
         tools:text="Category name" 
       android:textColor="#000000" 
       android:textSize="24sp" /> 

     <include layout="@layout/category_features" 
      android:layout_centerInParent="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      /> 

     <include layout="@layout/category_status" 
      android:layout_centerInParent="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      /> 
</com.fletech.smartbaby.api.CheckableLinearLayout> 

그리기/list_selector.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#40777777"/> 
     </shape> 
    </item> 
    <item android:state_long_pressable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#400000ff"/> 
     </shape> 
    </item> 
    <item android:state_checkable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#400000ff"/> 
     </shape> 
    </item> 
    <item android:state_checked="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#800000ff"/> 
     </shape> 
    </item> 
</selector> 

및 CheckableLinearLayout.java :

package com.fletech.smartbaby.api; 

import android.annotation.TargetApi; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.os.Build; 
import android.os.Parcel; 
import android.os.Parcelable; 
//import android.support.v7.widget.LinearLayoutCompat; // do I need this? I can't see any difference 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.Checkable; 
import android.widget.LinearLayout; 

public class CheckableLinearLayout extends LinearLayout implements Checkable { 
    private static final String TAG = CheckableLinearLayout.class.getSimpleName(); 
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; 

    private boolean mChecked; 

    public CheckableLinearLayout(Context context) { 
     super(context); 
    } 

    public CheckableLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void setChecked(boolean checked) { 
     Log.v(TAG, "setChecked: " + checked); 
     if (mChecked != checked) { 
      mChecked = checked; 
      refreshDrawableState(); 
     } 
    } 

    @Override 
    public boolean isChecked() { 
     return mChecked; 
    } 

    @Override 
    public void toggle() { 
     Log.v(TAG, "toggle: " + mChecked + " -> " + (!mChecked)); 
     setChecked(!mChecked); 
    } 

    @Override 
    // not sure if needed, as it seems to work without this as well 
    public boolean performClick() { 
     Log.v(TAG, "performClick"); 
     toggle(); 
     return super.performClick(); 
    } 

    @Override 
    // this might be unnecessary 
    protected void drawableStateChanged() { 
     Log.v(TAG, "drawableStateChanged"); 
     super.drawableStateChanged(); 
     final Drawable drawable=getBackground(); 
     if(drawable!=null) 
     { 
      final int[] myDrawableState=getDrawableState(); 
      drawable.setState(myDrawableState); 
      invalidate(); 
     } 
    } 

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
     Log.v(TAG, "onCreateDrawableState: " + extraSpace); 
     final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 
     if (isChecked()) { 
      mergeDrawableStates(drawableState, CHECKED_STATE_SET); 
     } 

     return drawableState; 
    } 

    @Override 
    protected Parcelable onSaveInstanceState() { 
     SavedState result = new SavedState(super.onSaveInstanceState()); 
     result.checked = mChecked; 
     return result; 
    } 

    @Override 
    protected void onRestoreInstanceState(Parcelable state) { 
     if (!(state instanceof SavedState)) { 
      super.onRestoreInstanceState(state); 
      return; 
     } 

     SavedState ss = (SavedState) state; 
     super.onRestoreInstanceState(ss.getSuperState()); 

     setChecked(ss.checked); 
    } 

    protected static class SavedState extends BaseSavedState { 
     protected boolean checked; 

     protected SavedState(Parcelable superState) { 
      super(superState); 
     } 

     @Override 
     public void writeToParcel(Parcel out, int flags) { 
      super.writeToParcel(out, flags); 
      out.writeInt(checked ? 1 : 0); 
     } 

     public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { 
      public SavedState createFromParcel(Parcel in) { 
       return new SavedState(in); 
      } 

      public SavedState[] newArray(int size) { 
       return new SavedState[size]; 
      } 
     }; 

     private SavedState(Parcel in) { 
      super(in); 
      checked = in.readInt() == 1; 
     } 
    } 
} 
+0

CAB 모드 ??? CAB navbar ?? – greenapps

+0

CAB = Contextual Action Bar : http://developer.android.com/design/patterns/actionbar.html – Gavriel

답변

1

나는 모든 사람에게 효과가있는이 일이 왜 나를 위해 일하지 않는지 알기 위해 일주일 이상을 보냈다. 대답은 간단합니다. 다른 사람들에게 도움이되기를 바랍니다.

해결책은 선택기 XML의 항목 순서를 변경하는 것이 었습니다. 어떤 상태가없는 항목은 마지막이어야한다 :

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_long_pressable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#400000ff"/> 
     </shape> 
    </item> 
    <item android:state_checkable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#400000ff"/> 
     </shape> 
    </item> 
    <item android:state_checked="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#800000ff"/> 
     </shape> 
    </item> 

    <!-- the item without any states has to be the last: --> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#40777777"/> 
     </shape> 
    </item> 
</selector> 

이유는 파서가 일치하는 XML 파일의 첫째 항목을 찾습니다 것으로 보인다는 것이다. 즉, 상태 수가 다른 항목이있는 경우 xml 상단에 상태가 더 많은 항목이 있어야합니다. 체크 할 수 있지만 long_pressable - - 체크 가능하고 long_pressable 모두 - long_pressable하지만 체크 가능하지를 예를 들어 :

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checkable="true" android:state_long_pressable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#ffff00"/> 
     </shape> 
    </item> 
    <item android:state_long_pressable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#ff0000"/> 
     </shape> 
    </item> 
    <item android:state_checkable="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#00ff00"/> 
     </shape> 
    </item> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#40777777"/> 
     </shape> 
    </item> 
</selector> 

이 방법 만이 방법, 당신은 항목에 다른 색을 볼 수 있습니다 - 그 외 모든 것

+0

덕분에 많은 도움이되었습니다. 항목 순서가 잘못되었습니다. –

관련 문제