ListView的项目选择Android的 [英] ListView Item selection Android
问题描述
我有一个列表视图中,我想我的活动开始时,则第一个项目将具有背景和其他项目没有任何background.After,如果用户选择的任何其他项目则该特定项目的背景将现在是蓝色的,其余没有任何background.Please帮助我在此。
I have a listview in that i want when my activity starts then the first item will have background and the other items don't have any background.After that if the user selects any other item then the background of that particular item will now be blue and the rest don't have any background.Please help me on this.
我的 layout_effect.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape>
<solid android:color="#00a7eb" />
</shape>
</item>
</selector>
我的GetView code我在哪里设置默认背景
My GetView Code where i am setting the default Background
public View getView(int position, View convertView, ViewGroup parent) {
if(position == 0){
vi.setBackgroundResource(R.drawable.selection_effect);
vi.setSelected(true);
}
}
我的任何onclickListner列表视图的
Any my onclickListner of the listview
onewayListView .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Clicked", 1000).show();
}
});
我的列表视图XML
My Listview xml
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:choiceMode="singleChoice"
android:layout_height="wrap_content"
android:listSelector="@drawable/selection_effect"
android:layout_width="wrap_content"/>
请帮助我,我怎么能解决这个问题,我是stucked它从去年1天。
Please help me how could i solve this problem i am stucked in it from last 1day.
推荐答案
有关这样做你可以做的就是创建一个选择器,并将其应用到你的 row.xml $ C $的父视图C>
For doing this what you can do is create a selector and apply it to parent view of your row.xml
-
中定义颜色的strings.xml
&LT;颜色名称=蓝色&GT;#009FE3&LT; /彩色&GT;
创建一个选择器
<item android:drawable="@color/blue" android:state_activated="true" />
<item android:drawable="@color/blue" android:state_focused="true" />
现在把它应用为安卓背景=@可绘制/ row_selector
为您排父视图
Now apply it as android:background="@drawable/row_selector"
for parent view of your row.
-
设置的ListView为CHOICE_MODE_SINGLE在Java code
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
最后setAdapter到您的ListView。
listview.setAdapter(适配器);
对于ListView中选择使用默认项,
listview.setItemChecked(位置,真);
这篇关于ListView的项目选择Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!