ListViewの項目の背景色を変更しようとしていますが、実際にはstate_pressed
のみが機能しています。セレクタを使用してListViewの背景色を変更する
の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>
SOLUTION: 私は正しいがstate_activated
ない、セレクタを間違って入力しましたstate_active
uは、リスト項目またはリスト項目名の背景色を変更したいのですか? – prat