2010-11-22 20 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> 

मेरी सूची देखें का प्रत्येक आइटम दो TextView से बना है:

<?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 से मेरी listView पॉप्युलेट कर रहा हूँ, इस तरह से:

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); 

मेरा लक्ष्य पाठ Colo बदलने के लिए सक्षम होने के लिए है जब भी माता-पिता का चयन किया जाता है या दबाया जाता है तो इन बच्चों के विचारों का बैकगोरंड।

मैं यह कैसे कर सकता हूं?

+0

कैसे आप उपयोगकर्ता क्लिक किए बिना फोकस स्थापित करेगा पाने में कामयाब रहे? –

उत्तर

36

जब भी मूल पंक्ति चयनित होती है तो आपकी सूची पंक्ति में बच्चे के विचारों को चुना जाना चाहिए, इसलिए आप उन दृश्यों पर सामान्य स्थिति खींचने योग्य/रंग-सूची सेट करने में सक्षम होना चाहिए, कोई गन्दा जावा कोड नहीं ज़रूरी। this SO post देखें।

विशेष रूप से, आप इस तरह एक XML संसाधन के लिए अपने textViews की textColor सेट करेंगे:

<?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

अरे योनी, 'सेट इटम्स कैनफोकस (बूलियन)' का क्या अर्थ है? –

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

हे धीरल, 'सेट इटम्स कैनफोकस (बूलियन)' का क्या अर्थ है? –

31

अपने main.xml अपने ListView में निम्नलिखित शामिल हैं:

android:drawSelectorOnTop="false" 

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

टच जारी करने के बाद केंद्रित सूची सेल हाइलाइट रहता है, अगर आप इसे इस तरह करते हैं। – Tsimmi

4

यहां लिस के साथ चयनकर्ताओं का उपयोग करने के तरीके पर good article है ts।

इसके बजाय यह स्थापित करने एंड्रॉयड होने के लिए की

: ListView की पृष्ठभूमि, मेरा मानना ​​है कि आप एंड्रॉयड सेट करना चाहते हैं: listSelector जिन्हें आप नीचे देख:

<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

सर कई जगह नल सूचक अपवाद बनाएँ। –

0

बहुत पुराने लेकिन मैं सिर्फ इस के साथ संघर्ष किया है , इस तरह मैंने इसे शुद्ध एक्सएमएल में हल किया। res/values ​​/ colors.xml में मैंने तीन रंग जोड़े (रंग _...);

<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> 

रेस/drawable फ़ोल्डर में मैं listview_colours.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> 

main_activity.xml सूची दृश्य खोजने के लिए और listSelector को drawable जोड़ने में; राज्य _...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> 

प्ले प्रभाव आप चाहते हैं पाने के लिए।

वहाँ भी एक तरीका है जहाँ आप सूची दृश्य की शैली निर्धारित कर सकते हैं लेकिन मैं इसे कभी काम करने के लिए

संबंधित मुद्दे

 संबंधित मुद्दे