使用选择器更改ListView背景颜色

时间:2016-05-31 19:06:23

标签: android android-layout listview

我正在尝试在选中时更改ListView项目的背景颜色,但只有state_pressed实际上正在工作。

ListView代码:

listView = (ListView) findViewById(R.id.listView);
chaptersAdapter = new ChaptersAdapter();
listView.setAdapter(chaptersAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        onListItemCheck(i, view);
        return true;
    }
});

ChaptersAdapter:

private class ChaptersAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<Chapter> data;

    public ChaptersAdapter(ArrayList<Chapter> data) {
        inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

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

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.activity_chapters_item, null);
        }
        TextView chapterText = (TextView) view.findViewById(R.id.chapterText);
        chapterText.setText("Chapter: " + data.get(i).number);
        TextView pagesText = (TextView) view.findViewById(R.id.pagesText);
        pagesText.setText("Pages: " + data.get(i).pagesRead + "/" + data.get(i).pagesTotal);
        return view;
    }
}

我尝试了setItemChecked

private void onListItemCheck(int position, View view)
{
    listView.setItemChecked(position, !listView.isItemChecked(position));
    Toast.makeText(getApplicationContext(), "Checked: " + listView.isItemChecked(position), Toast.LENGTH_SHORT).show();
}

使用setActivated

private void onListItemCheck(int position, View view)
{
    view.setActivated(!view.isActivated());
    Toast.makeText(getApplicationContext(), "Activated: " + view.isActivated(), Toast.LENGTH_SHORT).show();
}

ListView布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.library.Chapters"
android:orientation="vertical"
android:weightSum="1">

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listView"
    android:layout_weight="0.75"
    android:fastScrollEnabled="true" />
</LinearLayout>

ListView项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:library="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/chapters_listview_selector">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mankin="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout">

    <TextView
        android:id="@+id/chapterText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItem"
        android:text="Text"
        android:textColor="#000000"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp" />
</LinearLayout>
<TextView
    android:id="@+id/pagesText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:text="Page: 0/0"
    android:gravity="center_vertical"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:textSize="16sp"
    android:textColor="@android:color/secondary_text_light" />
</LinearLayout>

选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_red_dark"
      android:state_checked="true" />
<item android:drawable="@android:color/holo_red_dark"
    android:state_active="true" />
<item android:drawable="@android:color/holo_red_dark"
    android:state_selected="true" />
<item android:drawable="@android:color/holo_blue_light"
      android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />
</selector>

解: 我输入了错误的选择器,正确的是state_activated而不是state_active

2 个答案:

答案 0 :(得分:0)

请从state_pressed中删除代码并添加state_selete。

答案 1 :(得分:0)

请添加如下所示的state_selected:例如

class final:
    def __init__(self,app):
        self.savelist=[]

        self.time= StringVar()
        self.timecombo = ttk.Combobox(app,textvariable=self.time)
        self.timecombo.grid(row=0,column=1)
        self.timecombo.config(value =('1:00','2:00','3:00','4:00','5:00','6:00','7:00','8:00','9:00','10:00','11:00','12:00'))

        self.button1=Button(app,text='confirmed',command=self.save)
        self.button1.grid(row=3,column=2)


    ***def save(self):
        savetext = self.time.get()
        self.savelist.append(savetext)
        self.deletebutton_list = []
        self.savelabel_list = []
        for i in range(len(self.savelist)):
            savelabel = Label(app, text=self.savelist[i])
            savelabel.grid(row=4 + i, column=0)
            self.savelabel_list.append((savelabel))
            deletebutton = Button(app, text='delete' , command=functools.partial(self.deletelabel,idx=i))
            deletebutton.grid(row=4 + i, column=1)
            self.deletebutton_list.append(deletebutton)
    def deletelabel(self, idx):
        self.savelabel_list[idx].destroy()
        self.deletebutton_list[idx].destroy()
        self.savelist.remove(self.savelist[idx])
        self.savelabel_list.remove(self.savelabel_list[idx])
        self.deletebutton_list.remove(self.deletebutton_list[idx])***

app = Tk()
a = final(app)
app.title('things to do')
app.geometry("500x300+200+200")

app.mainloop()

并在Listview中:

    <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_selected="true"
        android:drawable="@color/pressed_color"/>
    <item
        android:drawable="@color/default_color" />
</selector>