我在我的活動中創建了一個ListView,並使用另一個兩個按鈕(prev,next)在ListView上移動突出顯示。當點擊兩個按鈕時,我打電話給setSelection(pos),但列表視圖中沒有突出顯示。ListView,突出顯示所選項目
我也定製試圖與佈局文件列表項,並在其上註冊選擇,如描述: http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html
不幸的是預期它不工作。當我觸摸列表項時,此方法確實改變了顏色,但是當我調用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>
我已經嘗試過了剛纔和onItemSelected不會被觸發。從sdk文檔:「onItemSelected():設置當前選擇的項目。如果在觸摸模式下,該項目不會被選中,但它仍然會被正確定位。」那麼我們應該關閉列表視圖觸摸模式嗎? – pengguang001 2012-02-16 01:55:04
http://developer.android.com/resources/articles/touch-mode.html 經過仔細考慮,我們決定完全刪除選擇,當用戶通過觸摸屏操縱UI時。 在觸摸模式下,沒有焦點並且沒有選擇。只要用戶進入觸摸模式,網格列表中的任何選定項目都會被取消選中。類似地,當用戶進入觸摸模式時,任何關注的小部件都會變得不重點。下圖說明了在用軌跡球選擇項目後用戶觸摸列表時發生的情況。 – jsaye 2012-02-16 08:39:25
新Android開發人員常見的問題是依賴於ListView.getSelectedItemPosition()。在觸摸模式下,此方法將返回INVALID_POSITION。您應該改爲使用點擊偵聽器(請參閱setOnItemClickListener(android.widget.AdapterView。OnItemClickListener))或選擇模式(請參閱setChoiceMode(int))。 – jsaye 2012-02-16 08:39:38