2010-11-22 2 views
27

에있는 ListView 항목의 색상을 변경 (이것은 XML 레이아웃입니다) :방법 초점 그리고 난 내 응용 프로그램에서 목록보기를 클릭

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/arrayList" 
     android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
     android:textFilterEnabled="true" 
     android:scrollbars="vertical" 
     android:drawSelectorOnTop="true"> 
</ListView> 
내 목록보기의 각 항목은 두 개의 텍스트 뷰 구성되어

:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout android:layout_width="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/row_container" 
     android:padding="5px" android:layout_height="wrap_content" 
     android:background="@color/white" android:shrinkColumns="0"> 
       <TableRow> 
       <TextView android:layout_height="wrap_content" 
         android:layout_width="wrap_content" android:layout_below="@+id/ 
description" 
         android:id="@+id/description" 
         android:textColor="@color/black" 
         android:scrollHorizontally="true" 
         android:singleLine="true"></TextView> 
     </TableRow> 
     <TableRow> 
       <TextView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" android:id="@+id/result" 
         android:textColor="@color/grey" 
         android:maxLines="1" 
         android:scrollHorizontally="true"></TextView> 
     </TableRow> 

</TableLayout> 

나는 이런 식으로, ArrayAdapter와에서 내 목록보기를 채우는 해요 :

public class Matches extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    //set layout 

    setContentView(R.layout.list_layout); 
    // obtain reference to listview 
    ListView listView = (ListView) findViewById(R.id.arrayList); 

    ArrayAdapter<Match> arrayAdapter = new ArrayAdapter<Match>(
     this, R.layout.custom_row, R.id.description, createItems()) { 

    @Override 
    public View getView (int position, View convertView, ViewGroup parent){ 
     Match item = getItem (position); 
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.custom_row, null); 
     TextView description = (TextView)rowView.findViewById(R.id.description); 
     TextView result = (TextView)rowView.findViewById(R.id.result); 
     description.setText(item.description + " Risultato: " + item.result); 
     result.setText(item.date + " " + item.hour); 
     return rowView; 
    } 
    }; 

    listView.setAdapter(arrayAdapter); 

내 목표는 텍스트 콜로을 변경 할 수있다 부모가 선택되거나 누를 때마다 이러한 하위 뷰의 백글 런드 및 r.

어떻게 할 수 있습니까?

+0

가 어떻게 사용자의 클릭없이 초점을 설정합니다 작동하도록 관리하지? –

답변

36

부모 행을 선택할 때마다 목록 행의 하위 뷰를 선택해야하므로 변경하려는 뷰에 정상 상태 드로어 블/컬러 목록을 설정할 수 있어야하며 지저분한 Java 코드는 필요하지 않습니다. 필요한. this SO post을 참조하십시오.은 당신의 ListView에 다음을 포함하여 main.xml에에서

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused --> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed--> 
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed --> 
    <item android:drawable="@color/black" /> <!-- default --> 
</selector> 
+0

안녕하세요, 'setItemsCanFocus (부울)'의 의미는 무엇입니까? –

5
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_pressed="true" android:drawable="@drawable/YOUR DRAWABLE XML" /> 
    <item android:drawable="@drawable/YOUR DRAWABLE XML" /> 
</selector> 
+0

Hey Dhiral,'setItemsCanFocus (boolean)'의 의미는 무엇입니까? –

31

:

특히,이 하나의 같은 XML 자원에 textViews의 textColor을 설정할 것

android:drawSelectorOnTop="false" 

android:listSelector="@android:color/darker_gray" 
+5

이렇게하면 집중된 목록 셀이 ​​터치를 놓은 후에 강조 표시됩니다. – Tsimmi

4

lis와 함께 셀렉터를 사용하는 방법은 여기 good article입니다. ts. 아래 그림과 같이 listSelector을 : 대신 안드로이드로를 설정

: ListView에의 배경은, 당신이 안드로이드 설정하려는 생각

<ListView android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:divider="@null" 
    android:dividerHeight="0dip" 
    android:listSelector="@drawable/list_selector" /> 
1
listview.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(final AdapterView<?> parent, View view, 
       final int position, long id) { 
      // TODO Auto-generated method stub 

      parent.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.listlongclick_selection)); 

      return false; 
     } 
    }); 
+0

Sir Many Places Null 포인터 예외를 생성합니다. –

0

아주 오래된하지만 난 그냥이 함께 분투 , 이것은 내가 어떻게 순수한 XML에서 그것을 해결했는지입니다. res/values ​​/ colors.xml 3 색 (색 _...)을 추가했습니다. 입술/그리기 폴더에서

<resources> 

    <color name="black_overlay">#66000000</color> 

    <color name="colour_highlight_grey">#ff666666</color> 
    <color name="colour_dark_blue">#ff000066</color> 
    <color name="colour_dark_green">#ff006600</color> 

</resources> 

제가 포함 listview_colours.xml 생성; 목록보기를 찾아 listSelector에 드로어 블을 추가main_activity.xml에서

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/colour_highlight_grey" android:state_pressed="true"/> 
    <item android:drawable="@color/colour_dark_blue" android:state_selected="true"/> 
    <item android:drawable="@color/colour_dark_green" android:state_activated="true"/> 
    <item android:drawable="@color/colour_dark_blue" android:state_focused="true"/> 
</selector> 

; listview_colours.xml에서 상태 _... 항목

<ListView 
    android:id="@+id/menuList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:listSelector="@drawable/listview_colours" 
    android:background="#ff222222"/> 
</LinearLayout> 

플레이는 원하는 효과를 얻을 수 있습니다.

이 당신이 목록보기의 스타일을 설정할 수있는 방법도 있지만, 나는 그것이

관련 문제