2014-02-19 2 views
0

다음은 Android 4.1 이상을 타겟팅하는 앱입니다. listview에서 활성 상태를 원합니다. 이 앱이 제대로 작동하는 다른 앱에서 코드를 사용하고 있습니다. 차이가 무엇인지 여기에 있습니다 (API 버전이 더 높음).Android ListView CHOICE_MODE_SINGLE이 (가) 설정되지 않음

DrawerAdapter lAdapter = new DrawerAdapter(this, modeList); 
    mDrawerList.setAdapter(lAdapter); 
    mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    mDrawerList.setItemChecked(0, true); 

    mDrawerList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View v, int pos, 
       long id) { 

      mDrawerList.setItemChecked(pos, true); 
      } 

서랍 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/main_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/drawer_list" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:cacheColorHint="#ececec" 
     android:background="@drawable/tile_repeat" 
     android:fadingEdge="none" 
     android:listSelector="@drawable/nav_selector" 
     android:scrollbars="none" /> 

</android.support.v4.widget.DrawerLayout> 

선택기 :

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 


    <!-- pressed --> 
    <item android:drawable="@drawable/nav_press" android:state_activated="false" android:state_pressed="true"/> 

    <!-- CHECKED --> 
    <item android:drawable="@drawable/nav_active" android:state_activated="true" android:state_pressed="false" /> 

</selector> 

nav_active :

나는이 자바를

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 
    <solid android:color="#cecece" /> 
</shape> 

단계가 빠졌습니까? 프레스 상태가 잘 작동, 그냥 활성화되지 않습니다. DrawerAdapterrow_drawer_layout.xml

<?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:orientation="vertical" > 

     <TextView 
      android:id="@+id/tvMenuItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" 
      android:textColor="#005348"/> 

    </LinearLayout> 

루트보기 DrawerAdapter :

public class DrawerAdapter extends ArrayAdapter<String> { 
private final Context context; 

public DrawerAdapter(Context context, String[] sizeList) { 
    super(context, 0, sizeList); 
    this.context = context; 
} 


static class ViewHolder { 
    public TextView tv1; 
    public TextView tv2; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    View rowView = convertView; 

    if (rowView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.row_drawer_layout, null, true); 

     holder = new ViewHolder(); 
     holder.tv1 = (TextView) rowView.findViewById(R.id.tvMenuItem); 
     rowView.setTag(holder); 
    } else { 
     holder = (ViewHolder) rowView.getTag(); 
    } 

    String s = getItem(position); 
    holder.tv1.setText(s); 

    return rowView; 

} 

}

+0

'DrawerAdapter.getView' 메소드의 기본보기는 무엇입니까? – Blaz

+0

@blazsolar 위 코드를 ..,. – KickingLettuce

답변

2

ListView 항목의 루트보기 Checkable 인터페이스를 구현해야합니다. 귀하의 경우에는 LinearLayout 마녀는 참고 Checkable 구현 노트 않습니다. 레이아웃에 CheckedTextView을 사용해야합니다.

다음과 같이 보일 것입니다.

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tvMenuItem" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView" 
     android:textColor="#005348" 
     android:background="@drawable/checkable_item"/> 

당신이 당신의 품목 내부의 더 복잡한 레이아웃을 가지고 싶다면 당신은 안드로이드 레이아웃 중 하나를 확장하고 그것에 Checkable 인터페이스를 추가 할 수 있습니다. Here 당신이 선택에 checked 상태를 사용해야 검사 항목에 대한 배경을 변경하려면 RelativeLayout``

`의 예

입니다 :

나는 android:background="@drawable/nav_selector" 추가 :

checkable_item.xml 
<selector> 
    ... 
    <item android:state_checked="true" android:drawable="@drawable/some_drawable" /> 
    ... 
</selector> 
+0

글쎄,'TextView'하지만 행의 배경색을 변경하려고하는 것이 아니므로 적용 할 수 없습니까? – KickingLettuce

+1

체크 된 항목의 배경을 변경하려면'android : state_checked = "true"'를 원하는 색상으로 설정 한'selector'에'android : background'를 설정해야합니다. – Blaz

0

를 해결하려면,이게 내가 무슨 짓을 루트에 LinearLayoutrow_drawer_layout.xml; 이제 사용자가 선택할 때 행은 활성 상태로 유지됩니다.

@blazsolar에게 감사드립니다. 그의 두 번째 코멘트는 나를 올바른 방향으로 이끌었다.

0

너무 늦게 답변을 드려 죄송합니다. 당신이해야 할 일은 android:background="?android:attr/activatedBackgroundIndicator" 속성을 row_drawer_layout.xml 파일에 추가하기 만하면됩니다.마지막으로 , 그것은 다음과 같아야합니다

<?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:orientation="vertical" 
    android:background="?android:attr/activatedBackgroundIndicator"> 

    <TextView 
     android:id="@+id/tvMenuItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:textColor="#005348"/> 

</LinearLayout> 

이것은 당신이 사용자 정의 스타일을 설정할 때 영향을 미치는 것을 의미 스타일에 의해 정의 된 activatedBackgroundIndicator를 사용하기 때문에이 작업을하고있는 올바른 방법입니다 모든 listviews (달리 지정되지 않은 경우).

관련 문제