我有一个实现ListView的相当简单的程序。当我在列表中选择一个项目时,它会短暂地变为橙色,然后恢复为黑色。但是,我希望在选择项目后保持橙色(直到我清除项目或选择其他项目)。我尝试编码但失败了。我认为它必须接近正确。任何人都可以在下面的代码中告诉我需要修改什么来使它工作吗?
文件:./ res /layout / main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center">
<ListView
android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@drawable/list_selector"/>
</LinearLayout>
文件:./ are / drawable / list_selector.xml
<?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/orange" />
<item android:state_selected="false" android:drawable="@android:color/black" />
</selector>
文件:./ res / values / colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <color name="orange">#b0e0e6</color> </resources>
文件:./ src / com / commware / android / linearpct / LinearLayoutDemo.java
public class LinearLayoutDemo extends ListActivity implements OnItemClickListener {
private static final String[] items={"1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"};
ListView myLV;
ArrayAdapter myAdapter;
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Log.d("LinearLayoutDemo:", "********: onCreate() begin");
getListView().setOnItemClickListener(this);
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
myLV = (ListView) findViewById(android.R.id.list);
myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
myLV.setAdapter(myAdapter);
}
@Override public void onItemClick(AdapterView<?> arg0, View view, int position, long lng) {
view.setSelected(true);
String selectedFromList =(String) (myLV.getItemAtPosition(position));
Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList);
}
@Override public void onListItemClick(ListView parent, View view, int position, long id) {
super.onListItemClick(parent, view, position, id);
view.setSelected(true);
String selectedFromList =(String) (myLV.getItemAtPosition(position));
Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList);
}
}
注意:当我运行此代码时,来自onListItemClick()的日志消息永远不会显示在日志中。那里有些不对劲。
另外,我无法弄清楚如何在onItemClick()中调用super.onItemClick()。这可能是一个线索。
答案 0 :(得分:2)
:
<item android:state_selected="true" android:drawable="@color/orange" />
当对象使用D-PAD(跟踪板)聚焦时调用
<item android:state_activated="true" android:drawable="@color/orange" />
在上下文操作栏中选择对象时调用。 (仅限API 11 +)
API 11中的- 实现多选的最佳方法是自定义适配器,以更改视图bakground颜色。
class CPGAdapter extends ArrayAdapter<Pack> {
public void UpdateItem() {
notifyDataSetChanged();
}
@Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;
if (mView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(R.layout.cpga_xml, null);
} // inflate
TextView text = (TextView) mView.findViewById(R.id.tv_cpgatitle);
// get views from ids
if (mView != null) {
if (getItem(position).Selected) {
mView.setBackgroundResource(R.color.pressed);
} else {
mView.setBackgroundResource(R.color.default);
}
text.setText(getItem(position).Name);
}
return mView;
}
}
并在您选择View时的代码中:
public void onItemClick(AdapterView<?> AV, View v, int POS, long Id) {
((Pack) AV.getItemAtPosition(POS)).Selected = true;
v.setBackgroundResource(R.color.pressed);
// or (CPGAdapter(AV.getAdapter)).UpdateItem()
}
答案 1 :(得分:2)
将simple_list_item_1
更改为simple_list_item_activated_1
。颜色非常难看,如果你想要漂亮的颜色,你必须扩展ArrayAdapter。
public class LinearLayoutDemo extends ListActivity implements OnItemClickListener {
private static final String[] items={"1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"};
ListView myLV;
ArrayAdapter myAdapter;
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Log.d("LinearLayoutDemo:", "********: onCreate() begin");
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, items);
myLV = (ListView) findViewById(android.R.id.list);
myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
myLV.setAdapter(myAdapter);
myLV.setOnItemClickListener(this);
}
@Override public void onItemClick(AdapterView<?> arg0, View view, int position, long lng) {
String selectedFromList =(String) (myLV.getItemAtPosition(position));
Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList);
}
}
您可以移除list_selector.xml
并更改布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center">
<ListView
android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 2 :(得分:1)
查看State List,尤其是android:state_activated="true"