2017-01-31 68 views
0

我使用下面的代碼突出顯示點擊的項目。 但有2個問題:ListView:當我滾動時突出顯示消失

  • 我第一次點擊一個項目,然後突出顯示不出現。因此,只有在我第二次單擊項目後纔會突出顯示項目。
  • 當我滾動時,突出顯示消失。相反,我需要這些項目保持高亮顯示,直到點擊另一個項目。

你能幫我解決這兩個問題嗎?

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      view.setSelected(true); 

bg_key.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    android:id="@+id/myselector" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item 
     android:state_selected="true" 
     android:drawable="@color/activated_color" /> 
    <item 
     android:drawable="@color/default_color" /> 
</selector> 

song_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    tools:context=".MainActivity" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:background="@drawable/bg_key"> 
+0

亮點消失,因爲表的子視圖被回收 – Redman

+0

@Redman所以我能做些什麼? :) – xRobot

+0

我知道一個解決方案,你可以在getview()方法中移除條件'if(convertview == null)'條件,如果列表中有很少的元素,那麼使用它,它不是推薦的方法 – Redman

回答

1

你需要在你的適配器強調position參考。然後

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     view.setSelected(true); 
     lvAdpapter.highlightPos = position; // declare highlightPos =-1 by default in you adapter. 

然後裏面你的適配器的getView,這樣做:

public View getView(int position, View convertView, ViewGroup parent) { 
//existing code 

songLay.setSelected(position ==highlightPos); 
return songLay; 
} 
+0

剛剛完成,然後我該怎麼處理參考? – xRobot

+0

在您的監聽器中設置它的值,然後在'getview'中檢查值,就像我上面寫的2個片段一樣。 – Saurabh

+0

我得到這個:java.lang.NullPointerException:嘗試調用虛擬方法'void android.view.View.setSelected(boolean)'null對象引用 at com.xrobotx.alltest.musictest2.myAdapter.getView – xRobot

0

ListView的getView()呼籲每一個滾動和每一個新的列表項調用它getView,如果listview項目當前不可見在UI上,它的convertView等於null:

解決此問題:採用布爾isSelected []的數組,每個初始值爲false。

boolean[] isSelected = new boolean[20]; //your list view size 
Arrays.fill(isSelected, false); 
在適配器類

public View getView(int position, View convertView, ViewGroup parent) { 

     final ViewHolder holder; 
        string text = itemData[position] 
     if (convertView == null) { 
      rowLayout = (RelativeLayout) LayoutInflater.from(context) 
        .inflate(R.layout.list_view_item, parent, false); 
      holder = new ViewHolder(); 

      holder.txtString= (TextView) rowLayout 
        .findViewById(R.id.txtTitle); 
      rowLayout.setTag(holder); 
     } else { 
      rowLayout = (RelativeLayout) convertView; 
      holder = (ViewHolder) rowLayout.getTag(); 
     } 

     if(isSelected[position] == true){ 
        holder.txtString.setText("Selected") 
        rowLayout.setBackGround(selected) 
     }else{ 
      holder.txtString.setText("Not Selected") 
      rowLayout.setBackGround(notSelected) 
     } 




    public class ViewHolder { 
     public TextView txtString; 
     //your listview contents 
    } 

您的ListView oItemClick使用:

listView.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, 
         int position, long arg3) { 
        // TODO Auto-generated method stub 
         isSelected[position] = true // on selection 

         RelativeLayout rowLayout = (RelativeLayout) view; 
         rowLayout.setBackGround(Selected); 

        } 
      }); 
+0

我必須在「你的列表視圖內容」中輸入什麼內容? – xRobot

+0

它是你的textview和imageview,它是在佈局中......你還需要在'convertView == null'條件中初始化它們 – rafsanahmad007

相關問題