1

我正在开发一个带有 ListActivity 的 Android 2.3.3 应用程序。

我想做一些事情来向用户展示他/她选择的项目。我已经完成了以下操作,但它没有(我在 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>

这是 res/drawable 上的 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>

但它不起作用,因为它说我必须@drawable放入selector.xml.

如何将背景颜色设置为蓝色以最后单击列表项?

更新

与此列表一起使用的阵列适配器:

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;
    }
}
4

3 回答 3

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

}

于 2012-07-16T14:15:08.490 回答
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>

希望这会帮助你=)

于 2012-07-16T14:16:15.623 回答
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;
    }


}
于 2016-07-18T21:54:13.343 回答