簡體   English   中英

保持按下列表項

[英]Keep list item pressed

單擊后如何設置列表中的某些項目?

Google Maps App中的示例:

在此處輸入圖片說明

嘗試此幫助您方法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

@繪制/ 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>

@繪制/ 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>

@繪制/ 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>

在您的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