2015-01-21 66 views
0

我需要在列表視圖中突出顯示選定行。例如:如果我選擇第一個項目,則選擇第三個項目,然後刪除突出顯示的第一個項目,然後突出顯示第三個項目。列表視圖突出顯示所選行

我見過很多在Stackoverflow上發佈的例子和問題,但沒有找到合適的解決方案。

下面是我的代碼,它的工作原理,但我需要選擇項目兩次突出顯示,我該如何修改這個工作順利?

ListView mainListView = (ListView) findViewById(R.id.mainListView); 

     // Create and populate a List of planet names. 
     planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
              "Jupiter", "Saturn", "Uranus", "Neptune", 
              "Ceres","Pluto","Haumea","Makemake","Eris"};  
     planetList = new ArrayList<String>(); 
     planetList.addAll(Arrays.asList(planets)); 

     planetsAdapter = new PlanetsAdapter (this,planetList); 
     planetsAdapter .setNotifyOnChange(true); 

     // Set the ArrayAdapter as the ListView's adapter. 
     mainListView.setAdapter(planetsAdapter); 
     mainListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     mainListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       planetsAdapter.setSelectedPosition(position); 
       planetsAdapter.notifyDataSetChanged(); 
      } 
     }); 

改編類:

public class PlanetsAdapter extends ArrayAdapter<String> { 
     private ArrayList<String> planets; 
     private Context mContext = null; 
     private LayoutInflater inflater; 

     // used to keep selected position in ListView 
     private int selectedPos = -1; // init value for not-selected 

     public PlanetsAdapter (Context context,ArrayList<String> objects) { 
      super(context,0,objects); 
      mContext = context; 
      planets = objects; 
     } 

     public void setSelectedPosition(int pos){ 
      selectedPos = pos; 
      // inform the view of this change 
     } 

     public int getSelectedPosition(){ 
      return selectedPos; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       inflater = (LayoutInflater) mContext 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.list_item, null); 
      } 

      TextView title = (TextView) convertView 
        .findViewById(R.id.rowTextView); 

      // change the row color based on selected state 
      if(position == selectedPos){ 
       //title.setTextColor(Color.parseColor("#FFFFFF")); 
       title.setBackgroundColor(Color.parseColor("#ED07E1")); 
      }else{ 
       //title.setTextColor(Color.parseColor("#000000")); 
       title.setBackgroundColor(Color.parseColor("#e2e2e2")); 
      } 

      title.setText(getItem(position)); 
      return convertView; 
     } 
    } 

編輯:回答下面

For anyone who is looking for answers, i have answered it below hope it helps. 
+0

嘗試添加'notifyDataSetChanged();'到'setSelect edPosition'在你的適配器 – 2015-01-21 06:25:47

+0

@MD以前曾嘗試過,但仍然需要選擇項目兩次才能突出顯示。 – user2056563 2015-01-21 06:29:19

+0

你的問題對我來說是不可理解的。 '選擇項目兩次以突出顯示'。 – 2015-01-21 06:31:38

回答

0

設置該到你的列表視圖(XML或代碼)

XML: android:choiceMode="singleChoice" 

code: mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); 

我在您的xml中,嘗試添加android:listSelector背景以獲得您的選擇的反饋。

參考:http://developer.android.com/reference/android/widget/AbsListView.html

+0

不,它不會突出顯示該行,它只是表明用戶已選擇該項目 – user2056563 2015-01-21 07:22:13

+0

您是否爲您的項目視圖定義了可拖動的背景選擇器,並且它是否具有此:android :state_activated或android:state_selected。 – 2015-01-21 07:26:44

+0

是的,我已經完成了,我已經提到鏈接:http://stackoverflow.com/questions/18386035/custom-selector-for-list-background – user2056563 2015-01-21 07:31:51

0

大量的研究和R &後d終於得到了解決:

列表視圖中活動或片段:

ListView mainListView = (ListView) findViewById(R.id.mainListView); 

     // Create and populate a List of planet names. 
     planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
              "Jupiter", "Saturn", "Uranus", "Neptune", 
              "Ceres","Pluto","Haumea","Makemake","Eris"};  
     planetList = new ArrayList<String>(); 
     planetList.addAll(Arrays.asList(planets)); 

     planetsAdapter = new PlanetsAdapter (this,planetList); 
     planetsAdapter .setNotifyOnChange(true); 

     // Set the ArrayAdapter as the ListView's adapter. 
     mainListView.setAdapter(planetsAdapter); 
     mainListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     mainListView.setOnItemClickListener(new OnItemClickListener() { 

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

適配器類別:

public class PlanetsAdapter extends ArrayAdapter<String> { 
     private ArrayList<String> planets; 
     private Context mContext = null; 
     private LayoutInflater inflater; 


     public PlanetsAdapter (Context context,ArrayList<String> objects) { 
      super(context,0,objects); 
      mContext = context; 
      planets = objects; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       inflater = (LayoutInflater) mContext 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.list_item, null); 
      } 

      TextView title = (TextView) convertView 
        .findViewById(R.id.rowTextView); 

      title.setText(getItem(position));//Set the text to textview 
      return convertView; 
     } 
    } 

在XML中定義的ListView:

<ListView 
        android:id="@+id/mainListView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#FFFFFF" 
        android:choiceMode="singleChoice" 
        android:cacheColorHint="#00000000"> 
       </ListView> 

在上面的代碼定義在XML中list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rowTextView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="5dp" 
android:background="@drawable/list_item_selector" 
android:textColor="@drawable/text_color_selector" 
android:textSize="16sp" > 
</TextView> 

注意每個列表項我已經使用2只不同的選擇一個用於背景和一個用於改變文本的顏色。

list_item_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@color/defalut_color" android:state_activated="false"/> 
    <item android:drawable="@color/defalut_color" android:state_activated="false" android:state_pressed="false"/> 
    <item android:drawable="@color/pressed_color" android:state_pressed="true"/> 
    <item android:drawable="@color/pressed_color" android:state_activated="true"/> 

</selector> 

text_color_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:color="#000000" android:state_activated="false"/> 
    <item android:color="#000000" android:state_activated="false" android:state_pressed="false"/> 
    <item android:color="#FFFFFF" android:state_pressed="true"/> 
    <item android:color="#FFFFFF" android:state_activated="true"/> 

</selector> 

RES /價值/ colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="defalut_color">#e2e2e2</color> 
<color name="pressed_color">#ED07E1</color> 
</resources> 

因此多數民衆贊成它享受:)

+0

這就是NguyễnHoàiNam建議的。 – 2015-01-23 12:55:19