2012-07-16 37 views
1

我正在开发一个带有ListActivity的Android 2.3.3应用程序。ListView项目:将背景颜色更改为上次选择的项目(或单击)

我想做一些事情,向用户展示他/她选择的项目是什么。我做了以下,但它没有(我在搜索stackoverflow和他们是矛盾的答案)。

这是layout_item.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/txtName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/selector" 
     android:text="" /> 

</LinearLayout> 

这是对资源的selector.xml文件/绘制

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:color="#00FF00" /> 
    <item android:state_pressed="true" 
     android:color="#555555" /> 
    <item android:color="#000000" /> 
</selector> 

但它不工作,因为它说,我有在selector.xml中输入@drawable

如何将背景颜色设置为蓝色,以点击最后列表项目?与此列表中使用

UPDATE

阵列适配器:

public class GatesAdapter extends ArrayAdapter<Gate> 
{ 
    /** 
    * Application context. 
    */ 
    private Context context; 
    /** 
    * 
    */ 
    private int itemLayoutId; 
    /** 
    * 
    */ 
    private ArrayList<Gate> gates; 
    private int selectedGateIndex; 

    public int getSelectedGateIndex() { 
     return selectedGateIndex; 
    } 

    public void setSelectedGateIndex(int selectedGateIndex) { 
     this.selectedGateIndex = selectedGateIndex; 
    } 

    public Gate getSelectedGate() 
    { 
     return gates.get(selectedGateIndex); 
    } 

    public void removeSelectedGate() 
    { 
     this.gates.remove(selectedGateIndex); 
    } 

    public ArrayList<Gate> getGates() 
    { 
     return this.gates; 
    } 

    public GatesAdapter(Context context, int listItemResourceId, 
      ArrayList<Gate> objects) 
    { 
     super(context, listItemResourceId, objects); 

     this.context = context; 
     this.itemLayoutId = listItemResourceId; 
     this.gates = objects; 
     this.selectedGateIndex = -1; 

     this.setNotifyOnChange(true); 
    } 

    @Override 
    public int getCount() 
    { 
     return gates.size(); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     Log.v("GatesAdapter", "getView.postion: " + position); 
     View row = convertView; 
     if (row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(itemLayoutId, parent, false); 
     } 

     Gate gate = gates.get(position); 
     if (gate != null) 
     { 
      TextView itemText = (TextView)row.findViewById(android.R.id.text1); 
      if (itemText != null) 
      { 
       itemText.setText(gate.getName()); 
       //selectedGateIndex = position; 
       if (selectedGateIndex == position) 
       { 
        row.setBackgroundColor(Color.BLUE); 
       } 
      } 
     } 

     return row; 
    } 
} 
+0

你想为当前选择的颜色i项目或最后一个项目点击? – 2012-07-16 14:14:47

+0

我想要选择最后一个项目的颜色。我不知道列表项是否具有选定的状态。 – VansFannel 2012-07-16 14:25:51

回答

1

我自定义阵列适配器做到了,只是检查那里getView()方法:

if (position == lastClicked) { 
    v.setBackgroundColor(0x...) 
} else { 
    v.setBackgroundColor(0x...) 
} 

由于特定的行为,else块很重要大列表视图

更新:这是我自定义阵列适配器工作正常:

public class AbcArrayAdapter extends ArrayAdapter<UniversalListItem> { 

private Context c; 
private int id; 
private List<UniversalListItem>items; 

public AbcArrayAdapter(Context context, int viewResourceId, List<UniversalListItem> objects){ 
    super(context,viewResourceId,objects); 
    c=context; 
    id=viewResourceId; 
    items=objects; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(id, null); 
    } 

    final UniversalListItem o = items.get(position); 
    if (o != null) { 
     if (o.isInList()) { 
      v.setBackgroundColor(0x00ffffff); 
     } else { 
      v.setBackgroundColor(0x4d0099cc); 
     } 
     /*....*/ 
    } 
    return v; 
} 

}

+0

是的,我认为这是解决您的问题的最佳解决方案。比较位置并设置背景颜色。 – pixelscreen 2012-07-16 14:19:30

+0

其他很重要,因为每一行都被重绘! – Carnal 2012-07-16 14:55:58

+0

对不起,但它不起作用。 – VansFannel 2012-07-16 19:43:16

0

是,在选择项目只接受可绘制。但是,您可以用颜色创建绘图资源: 添加在您的colors.xml你的颜色:

<drawable name="color1">#00FF00</drawable> 
<drawable name="color2">#00FF00</drawable> 
<drawable name="color3">#000000</drawable> 

然后,把它们作为可绘制:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/color1" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/color2" /> 
    <item android:drawable="@drawable/color3" /> 
</selector> 

希望这将帮助你=)

+0

对不起,但它不起作用。 – VansFannel 2012-07-16 14:36:19

+0

如果你使用这种方法,你应该使用'android:state_activated =“true”'而不是'item android:state_selected =“true”'(http://stackoverflow.com/a/6207213/1506048) – kromuchi 2012-12-06 21:00:19

0

我更喜欢这个更简单的解决方案:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) { 
      statusText.setText("Redes:" + lv.getCount()+"\n Posición/First:" +position + "/" + lv.getFirstVisiblePosition()); 

      NetsArrayAdapter aA = (NetsArrayAdapter) parent.getAdapter(); 

      for(int a = 0; a < parent.getChildCount(); a++) { 
       parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT); 
      } 

      view.setBackgroundColor(Color.BLUE); 
      aA.setLastSelected(position); 
     } 
    }); 

private class NetsArrayAdapter extends ArrayAdapter<String> { 
    private ArrayList<String> mData; 
    private int id; 
    private Context c; 

    private int lastSelected = -1; 

    // declaring our ArrayList of items 
    private ArrayList<String> objects; 

    /* here we must override the constructor for ArrayAdapter 
    * the only variable we care about now is ArrayList<Item> objects, 
    * because it is the list of objects we want to display. 
    */ 
    public NetsArrayAdapter(Context context, int textViewResourceId, ArrayList<String> objects) { 
     super(context, textViewResourceId, objects); 
     c = context; 
     id = textViewResourceId; 
     mData = objects; 
    } 

    @Override 
    public View getView(final int position, View view, ViewGroup parent) { 
     View currView = super.getView(position, view, parent); 

     System.out.println("Paint/selected:"+position+"/"+lastSelected); 

     if(lastSelected != position) 
      currView.setBackgroundColor(Color.BLACK); 
     else 
      currView.setBackgroundColor(Color.BLUE); 

     return currView; 
    } 

    public int getLastSelected() { 
     return lastSelected; 
    } 

    public void setLastSelected(int lastSelected) { 
     this.lastSelected = lastSelected; 
    } 


} 
相关问题