2013-06-19 161 views
0

我想將當前點擊的項目設置爲我以前實現的不同顏色。ListView更改選定項目的背景

@Override 
public void onItemClick(StaggeredGridView parent, View view, int position, 
     long id) { 
    Toast.makeText(MainActivity.this, "Clicked Position "+position, Toast.LENGTH_LONG).show(); 
    Log.d("Clicked","Clicked Position "+position+" Content "+contentList.get(position)); 
    if(prevSelected !=null) 
    { 
     prevSelected.setBackgroundColor(getResources().getColor(android.R.color.white)); 

    } 
    prevSelected = view; 
    view.setBackgroundResource(R.drawable.list_pressed_holo_light); 
    selectedPosition = position; 
} 

現在我面臨的問題是,如果這種選擇的觀點在getView回收()所有這些觀點也有此相同的背景。如果我改變他們的背景,那麼這個視圖的背景也會改變。任何人都可以解決這個問題。

+0

使用選擇此puspose – Raghunandan

回答

0

這裏面的文字,甚至顏色是我getView的解決方案,我已經結束了()。

  if (convertView != null){ 
      // If the selected view is used somewhere else 
      if (convertView.equals(prevSelected) && position != selectedPosition) 
      { 
       convertView.setBackgroundColor(getResources().getColor(android.R.color.white)); 
       Log.d("Yup", "Changing color"); 
       Log.d("Yup", position + " " + selectedPosition); 
      } 
      // If the selected view is redrawn and the recycled view is not 
      // the same view again 
      else if (position == selectedPosition && !convertView.equals(prevSelected)) 
      { 
       // Make all other views if any which were prev selected 
       // white 
       prevSelected.setBackgroundColor(getResources().getColor(android.R.color.white)); 
       prevSelected = convertView; 
       convertView.setBackgroundResource(R.drawable.list_pressed_holo_light); 
       Log.d("Yup", "Setting pressed color"); 
       Log.d("Yup", position + " " + selectedPosition); 
      } 
      // The case where pos == selected pos and same view was used 
      // again.Need to set it to that colour as it could have been 
      // changed in the first condition 
      else if(position == selectedPosition && convertView.equals(prevSelected)) 
      { 
       prevSelected = convertView; 
       convertView.setBackgroundResource(R.drawable.list_pressed_holo_light); 
       Log.d("Yup!", "Setting that view colour"); 
       Log.d("Yup!", position + " " + selectedPosition); 
      } 
      } 
+0

是在3.0以下的Android版本的工作? –

+0

是的。它應該適用於所有版本的Android。 – Rajul

+0

我有一個查詢做你的幫助 –

1

如果您設置所需的背景也getView()方法都將正常工作;) 只要把你的getView(int position, View convertView, ViewGroup parent)方法:

if (convertView != null){ 
    if (position == selectedPosition) { 
     convertView.setBackgroundColor(getResources().getColor(android.R.color.white)); 
    } else { 
     convertView.setBackgroundResource(R.drawable.list_pressed_holo_light); 
    } 
} 
+0

但是這是沒有的。你應該遵循的方式,而不是你應該添加view.setBackgroundResource(R.drawable.some_state_list_drawable) – pskink

+0

你是對的,但那不是重點。重要的是在'getView()'方法中設置背景。當談到如何做到這一點時,我只是複製了Rajul正在使用的代碼來爲選定和未選擇的項目設置背景 – marcin

+0

問題在於,它是無望的低效率,因爲所有的循環視圖都必須重新繪製。 – Rajul

0

那麼容易我的朋友去this site,你會找到答案。

變化單擊時的物品的顏色和列表視圖

+0

多數民衆贊成只用於懸停。我希望它保持選定。 – Rajul

0

嘗試使用自定義適配器,這也可以幫助您完全控制您的項目並設置默認項目; listView XML和項目XML沒有特別的設置。使用getViewTypeCount()和selectedViewType是避免回收視圖的關鍵。

public class ListAdapter extends ArrayAdapter<MyObj> { 

private final int layoutInflater; 
private Context context; 
private List<MyObj> items; 
private int mSelectedItem = 0; 
private int TAG_UNSELECTED = 0; 
private int TAG_SELECTED = 1; 

public ListAdapter(Context context, int resource, List<MyObj> items) { 
    super(context, resource, items); 
    this.context = context; 
    this.layoutInflater = resource; 
    this.items = items; 
} 

public void selectItem(int position) { 
    mSelectedItem = position; 
    notifyDataSetChanged(); 
} 


@Override 
public int getViewTypeCount() { 
    return 2; 
} 

@Override 
public int getItemViewType(int position) { 
    return position == mSelectedItem ? TAG_SELECTED : TAG_UNSELECTED; 
} 


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

    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(layoutInflater, null); 
    } 

    MyObj myObj = items.get(position); 
    TextView textView = (TextView) v.findViewById(R.id.title); 
    textView.setText(myObj.title); 

    int type = getItemViewType(position); 
    if(type == TAG_SELECTED) { 
     v.setBackgroundColor(Color.parseColor("#1da7ff")); 
     textView.setTextColor(Color.parseColor("#ffffff")); 
    } else { 
     v.setBackgroundColor(Color.parseColor("#f8f8f8")); 
     textView.setTextColor(Color.parseColor("#474747")); 
    } 

    return v; 
} 

} 

然後在您的活動:

  ListView listView = (ListView) findViewById(R.id.list_view); 
      ListAdapter adapter = new ListAdapter(mContext, R.layout.item_layout, list); 
      listView.setAdapter(adapter); 
      adapter.selectItem(0); // Default selected item 

      // Get selected item and update its background 
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
        adapter.selectItem(position); 
       } 
      }); 
相關問題