2012-07-05 3 views
4

저는 ListView를 가지고 처음으로 항목을 표시하고 싶습니다. 일부 항목을 비활성화하려고합니다.android : state_enabled = "false"일 때 색상을 설정할 수 없습니다.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="20dp" 
    android:gravity="left|center" 
    android:layout_width="wrap_content" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:background="@color/list_bg"> 

    <ImageView 
     android:id="@+id/avatar" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:layout_marginTop="10dp" 
     android:layout_alignParentLeft="true" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="48dp" 
     android:layout_toRightOf="@+id/avatar" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp"> 


      <TextView android:id="@+id/listitem1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:textColor="@drawable/textblack_selected" 
       android:textSize="18dp" 
       android:textStyle="bold" /> 

      <TextView android:id="@+id/listitem2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:textColor="@drawable/textorange_selected" /> 

    </LinearLayout> 

    <ImageView 
     android:id="@+id/arrowImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25dp" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/arrow_selector" /> 

</RelativeLayout> 

문제는이 (android:state_enabled="false"을 체결하지 않을 것입니다 :이를 위해

나는이 textorange_selected.xml했다 :

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" 
      android:color="@color/medium_gray" /> 

    <item android:state_selected="true" 
     android:color="@android:color/white" /> 

    <item android:state_focused="true" 
     android:color="@android:color/white" /> 

    <item android:state_pressed="true" 
     android:color="@android:color/white" /> 

    <item android:color="@color/lighter_orange" /> 
</selector> 

을 나는 행의 레이아웃에 사용 항목이 비활성화되고 텍스트의 색상을 회색으로 설정하는 순간이어야합니다).

@Override 
public boolean areAllItemsEnabled() {      
    return false; 
} 

@Override 
public boolean isEnabled(int position) { 
    return position < 1; 
} 

어디에 내 실수 : 내 어댑터에서이 코드를 사용하는 항목을 비활성화에 대한

?

+0

Java에서 TextView를 사용 중지 하시겠습니까? –

+0

내 편집을 참조하십시오. ListView의 항목을 비활성화하는 코드를 작성했습니다. 나는 안드로이드에서 작동합니다. – Gabrielle

+0

relativelayout이 비활성화되어 있으며, textview가 확실하지 않습니다. duplicateParentState를 사용하여 비활성화 된 상태를 textview로 전달할 수 있습니다. – njzk2

답변

2

셀렉터는 할당 된보기의 값 isEnabled()을 사용합니다.이 경우 RelativeLayout입니다. Adapter에서 isEnabled()으로 전화하지 않습니다.

당신과 같은 코드가 필요합니다

public View getView(int position, View convertView, ViewGroup parent) { 
    RelativeLayout v = (RelativeLayout)LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.listitem_relativelayout, null); 
    if(postion < 1) v.setEnabled(false); 
1

문제 isEnabled()에 의해 반환되는 활성화 된 상태가 자동으로 목록 항목의 모든 자식 뷰에 전달 하지 것입니다. 어댑터의 getView() 메소드 끝에서 view.setEnabled()으로 전화를 걸어 선택기를 기능하도록하십시오.

또한 TextView 자체 및 LinearLayout 래퍼에 android:duplicateParentState="true"을 지정하여 상태를 자손에게 전달해야합니다.

관련 문제