2013-03-20 75 views
0

我有以下列表使用充氣器的適配器,我試過在這裏添加顏色變化,但它改變了每個項目不只是單擊一個。Android的listview項顏色變化

private class ListAdapter extends ArrayAdapter<jsonData>{ 

    private List<jsonData> jList; 

    public ListAdapter(Context context, int resource,List<jsonData> jList) { 
     super(context, resource, jList); 
     this.jList = jList; 
    } 
     public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 

       if (v == null) { 
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v = vi.inflate(R.layout.listviewact_layout, null); 
          v.setBackgroundResource(R.layout.list_selector); 
       } 

       jsonData jd = jList.get(position); 

       if (jd != null) {    
        TextView name = (TextView) v.findViewById(R.id.memberName); 
        TextView dateJoined = (TextView) v.findViewById(R.id.dateJoined); 
        if (name != null) { 
         name.setText("Member Name: " + jd.name); 
        } 
        if (dateJoined != null) { 
         dateJoined.setText("Joined: " + getNewDate(jd.joined)); 
        } 
       } 

       return v; 
      } 

我也能夠得到物品位置,除了顏色以外,它大部分工作正常。我也嘗試添加一個資源文件的選擇器,但我得到了相同的結果。

更新:這似乎工作。我有一個小故障,但當我滾動項目的顏色變得瘋狂。

  public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) {   

      if(selectedItemPosition != position){ 
       //Resets old item to original color 
       parent.getChildAt(selectedItemPosition).setBackgroundColor(Color.BLACK); 
       view.setBackgroundColor(Color.BLUE);      
       selectedItemPosition = position; 
      }    
     } 
+0

寫的顏色onclickListn更改代碼er – Abx 2013-03-20 14:02:43

+0

爲什麼不在設計資源本身中設置可繪製背景?此外,您正在使用另一個佈局資源作爲背景可繪製? – Karakuri 2013-03-20 14:03:17

+0

這只是我用過的一個例子,沒有奏效。我將顏色更改添加到OnItemClickListener並且它可以工作,但顏色不會重置。我不得不再看看那個,但我正走在正確的軌道上。 – Jason 2013-03-20 14:12:31

回答

0

使用android:listSelectorandroid:background添加選擇XML文件烏爾列表視圖

  <ListView 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:listSelector="@drawable/list_selector"> 
     </ListView> 

試試這個選擇XML

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_enabled="true" 
android:state_pressed="true" android:drawable="@color/android:blue" /> 
<item android:state_enabled="true" 
android:state_focused="true" android:drawable="@color/android:transparent" /> 
<item 
android:drawable="@color/android:black" /> 

+0

我也會嘗試這種方法。 – Jason 2013-03-20 14:19:25

+0

我有這在列表選擇器。 <?xml version =「1.0」encoding =「utf-8」?> Jason 2013-03-20 14:23:25

+0

make state_pressed真如此: 2013-03-20 14:27:16

0
view.setbackground(); 

Changes the background of entire list .since your view points to that list not a field in that list. 
try this 

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

同樣的結果,並不保留選中的項目。我試圖在onItemClick視圖中將它設置爲BackgroundResource,它工作「不同」,但不正確。即使使用onItemClick視圖,它也會使整個列表變成藍色。 – Jason 2013-03-20 15:35:53