2016-05-31 24 views
0

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

+0

uは、リスト項目またはリスト項目名の背景色を変更したいのですか? – prat

答えて

0

state_pressedからコードを削除し、state_seleteで追加してください。

0

このようstate_selected追加してください:例えば

<?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> 

用とリストビューで:

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) { 
     view.setSelected(true); 
     ... //Anything 
    } 
}) 
+0

それは1つのアイテムに対してのみ機能しますが、複数のアイテムを選択する必要があります – Mateus

関連する問題