2017-08-28 3 views
-1

私はこれをしたい:私は2つのアイテムの色が違っていて、クリックされたアイテムを選択したいリストを持っている。選択された項目がハイライト表示されますが、それはJava android listViewクリック後、選択が消える

をリリースされた古い色が戻った後、私はこれでしている: artists_list_background_alternate.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:state_selected="false" 
    android:state_pressed="false" 
    android:drawable="@color/sign_out_color" /> 

<item android:state_pressed="true" 
    android:drawable="@color/survey_toplist_item" /> 

<item android:state_selected="true" 
    android:state_pressed="false" 
    android:drawable="@color/survey_toplist_item" /> 

</selector> 

artists_list_backgroundcolor.xml:

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

Javaファイルのコードを::

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      view = lInflater.inflate(R.layout.listitem, parent, false); 
     } 

     if (position % 2 == 0) { 
      view.setBackgroundResource(R.drawable.artists_list_backgroundcolor); 
     } else { 
      view.setBackgroundResource(R.drawable.artists_list_background_alternate); 
     } 

     ((TextView) view.findViewById(R.id.heading)).setText(data.get(position)); 

     return view; 
    } 


    ListView lvMain = (ListView) findViewById(R.id.list); 
     lvMain.setAdapter(adapter); 

レイアウトxml ::

<ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#00000000" 
     android:dividerHeight="1dp" /> 

そして私は、私は項目がハイライト表示させるために間違った

答えて

0

をやったかわからない、あなたのListViewためchoiceModeを提供する必要があります。例:

android:choiceMode="singleChoice" 

また、状態android:state_activated="true"の色をセレクタに追加します。

+0

私はこれを追加しましたが、アイテムアイテムにプッシュを止めたときにこの色が付きました。グレー、not this survey_toplist_item: –

+0

セレクタを修正する必要があります。 'android:state_activated =" true "'も導入してください。詳細については、[こちらを読む](https://stackoverflow.com/a/25322334/966550)を参照してください。 – waqaslam

+0

私はこれを修正しますが、私は速いclikに遅延があり、時には2つまたは選択された項目があります –

関連する問題