繁体   English   中英

ListView项:将背景色更改为上一个选定(或单击)的项

[英]ListView item: change background color to last item selected (or clicked)

我正在开发一个带有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>

但是,这是行不通的,因为它说,我必须把@drawableselector.xml

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

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

我已经完成了使用自定义数组适配器的操作,只需在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;
}

}

是的,选择器中的项目仅接受可绘制对象。 但是,您可以使用颜色创建可绘制对象:将颜色添加到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>

希望这对您有帮助=)

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

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


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM