简体   繁体   English

保持按下列表项

[英]Keep list item pressed

How I can set selected a certain items of a list when clicked? 单击后如何设置列表中的某些项目?

Example from Google Maps App: Google Maps App中的示例:

在此处输入图片说明

try this help you Method 1: get the current selected position 尝试此帮助您方法1:获取当前选定的位置

 OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

@Override
 public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
          mSelectedItem = position;
          mAdapter.notifyDataSetChanged();
 }
 };

And override the getView method of your adapter:

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
 final View view = View.inflate(context, R.layout.item_list, null);

if (position == mSelectedItem) {
     // set your color
}

  return view;

} }

Method 2:


first put this in your listview

android:listSelector="@drawable/list_selector"
Then create xml files in drawable to control the diferent states

@drawable/list_selector @绘制/ list_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>

@drawable/list_item_bg_normal: @绘制/ list_item_bg_normal:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="@color/list_background"
  android:endColor="@color/list_background"
  android:angle="90" />
</shape>

@drawable/list_item_bg_pressed: @绘制/ list_item_bg_pressed:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />
</shape>

In your ListView Selection : 在您的ListView选择中:

listView.setOnItemClickListener(new OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
             view.setSelected(true);
             ...
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM