2012-02-15 26 views
3

私は自分のアクティビティでListViewを作成し、もう2つのボタン(前、次)を使用してListViewでハイライトを移動します。 2つのボタンをクリックすると、setSelection(pos)が呼び出されますが、リストビューにハイライトは表示されません。残念ながら、期待通りに動作しない http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.htmlListView、選択したアイテムを強調表示する

:で説明したように

は、私はまた、カスタムレイアウトファイルとリスト項目を試みたが、それにセレクタを登録しています。このメソッドはリストアイテムに触れると色が変わりましたが、setSelection()を呼び出すとハイライトは表示されません。

レイアウト/ main.xml(メインレイアウト):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="4" 
    > 
    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
    <LinearLayout 
     android:orientation="vertical" 
     android:weightSum="2" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="3"> 
     <Button 
      android:id="@+id/btn_prev" 
      android:text="prev" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
     <Button 
      android:id="@+id/btn_next" 
      android:text="next" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

List.java(活性):

package com.android.list; 

import android.app.Activity; 
import android.os.Bundle; 

import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.TableRow; 
import android.widget.SimpleAdapter; 
import android.widget.SimpleAdapter.ViewBinder; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.graphics.Point; 
import android.graphics.Color; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class List extends Activity 
    implements View.OnClickListener 
{ 
    private ArrayList<HashMap<String, String>> mList; 
    private Button mPrev; 
    private Button mNext; 
    private ListView mListView; 
    private int mPosition; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mList = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map; 
     String[] ent = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"}; 
     for (int i=0; i<ent.length; i++) 
     { 
      map = new HashMap<String, String>(); 
      map.put("content", ent[i]); 
      mList.add(map); 
     } 
     SimpleAdapter adapter = new SimpleAdapter(this, 
      mList, R.layout.list_item, 
      new String[] {"content"}, 
      new int[] {R.id.list_text}); 
     mListView = (ListView) findViewById(R.id.list_view); 
     mListView.setAdapter(adapter); 
     mPosition = 0; 
     mListView.setSelection(mPosition); 

     mPrev = (Button) findViewById(R.id.btn_prev); 
     mPrev.setOnClickListener(this); 
     mNext = (Button) findViewById(R.id.btn_next); 
     mNext.setOnClickListener(this); 
    } 

    public void onClick(View view) 
    { 
     if (view == mPrev && mPosition >= 0) 
     { 
      mListView.setSelection(mPosition); 
      mPosition--; 
     } 
     else if (view == mNext && mPosition < mList.size()) 
     { 
      mListView.setSelection(mPosition); 
      mPosition++; 
     } 
    } 
} 

レイアウト/ list_item.xml(リスト項目のレイアウト) :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:chess="http://schemas.android.com/apk/res/org.pengguang.chess" 
    android:orientation="vertical" 
    android:background="@color/list_bg" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/list_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
</LinearLayout> 

カラー/ list_bg.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/grey" /> 
<item 
    android:state_pressed="true" 
    android:drawable="@color/blue" /> 
<item 
    android:state_selected="true" 
    android:state_pressed="false" 
    android:drawable="@color/red" /> 

</selector> 

値/ colors.xml(カラーファイル):

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
    <color name="blue">#0303ff</color> 
    <color name="grey">#f7f7f7</color> 
    <color name="red">#ff0000</color> 
</resources> 

答えて

2

私はそれを試していません。

listView.setOnItemSelectedListener(new OnItemSelectedListener() { 

       public void onItemSelected(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 
        arg1.setSelected(true); 
       } 

       public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 

       } 

      }) 
+0

を設定するまで、私はちょうど今それを試してみましたが、とonItemSelectedがトリガされていません。 sdk docから: "onItemSelected():現在選択されている項目を設定します。タッチモードでは項目は選択されませんが、適切な位置に配置されます。"リストビューのタッチモードをオフにしますか? – pengguang001

+0

http://developer.android.com/resources/articles/touch-mode.html 慎重に検討した結果、ユーザーがタッチスクリーンでUIを操作したときに、選択を完全に削除することにしました。 タッチモードでは、フォーカスがないため選択できません。グリッド内のリスト内の選択された項目は、ユーザがタッチモードに入るとすぐに選択解除されます。同様に、フォーカスされたウィジェットは、ユーザーがタッチ・モードに入ると焦点が合わなくなります。下の画像は、ユーザーがトラックボールでアイテムを選択した後にリストに触れるとどうなるかを示しています。 – jsaye

+0

新しいAndroid開発者にとって非常によくある問題は、ListView.getSelectedItemPosition()に依存することです。タッチモードでは、このメソッドはINVALID_POSITIONを返します。代わりに、クリックリスナーを使用する必要があります(setOnItemClickListener(android.widget.AdapterView。OnItemClickListener))または選択モード(setChoiceMode(int)を参照してください)。 – jsaye

0

これは意図した動作です。なぜ選択を変更するためにボタンを使用しているのですか?

異なる選択肢を示す単一の選択肢ラジオグループを使用することをお勧めします。

0

項目を強調表示することは非常に簡単な方法:

lv.setItemChecked(ItemIndex,true); 

が、このアイテムは、選択し続ける(強調表示)あなたは

lv.setItemChecked(ItemIndex,false); 
関連する問題