2011-12-08 50 views
1

嗨,我有一個列表視圖和點擊我想顯示一個背景圖像到選定的列表項,並更改背景,如果選擇被改變。我一直在試圖實現這一點,但無法做到這一點。請幫我這一點,將不勝感激,所以如何在點擊時將背景設置爲列表視圖項並在其他點擊時動態更改

請找我使用

@Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     View view = convertView; 
     final int getstartedItemPos = position; 
     Resources res = getResources(); 

     if (convertView == null) { 
      view = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.getting_starteditem, null); 
     } 
     synchronized (view) { 
      TextView textTopic = (TextView) view 
        .findViewById(R.id.indexItems); 
      textTopic.setText(getStartedItems[getstartedItemPos]); 
      textTopic.setTypeface(tf); 
      view.setBackgroundColor(Color.TRANSPARENT); 

     } 
     return view; 
    } 

}; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     Intent myintent = new Intent(getApplicationContext(),GetStartedWebview.class); 
     myintent.putExtra("SelectedItem", getStartedItems[position]); 
     startActivity(myintent); 
    } 
}; 

回答

1

我無法從你的代碼,看看是否已附上OnItemClickListener到你的ListView的代碼,你應該做的如果你沒有。在onItemClick你可以抓住被點擊查看(ARG1),並使用.setBackgroundResource(int resID)它來改變它的背景

編輯:您可以使用保持顯示背景當前列表項的本地變量,所以這樣做:

private View pressedView = null; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     Intent myintent = new Intent(getApplicationContext(),GetStartedWebview.class); 
     myintent.putExtra("SelectedItem", getStartedItems[position]); 
     startActivity(myintent); 

     if(pressedView != null) { 
      pressedView.setBackgroundResource(..); // reset background of old item 
      pressedView = arg1; // Point pressedView to new item 
     } 
    } 
}; 
+0

感謝索倫,我這樣做,但問題是背景,如果我點擊其他項目,背景設置爲被點擊,而不是所有的項目都不會改變應該只有一個項目 – cavallo

+0

哦!看看我的編輯,這可能會幫助 –

+0

我做了同樣的方式,但仍然無法正常工作,背景不會改變...請幫助 – cavallo

3

pinkcandy使用此,View lastView;

public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) 
    { 


     if(lastView==null){ 
      v.setBackgroundColor(Color.BLACK); 

      lastView=v; 
      } 
      else{ 
      lastView.setBackgroundColor(Color.WHITE); 
      v.setBackgroundColor(Color.BLACK); 

      lastView=v; 
      } 

    } 

它可以幫助你

1

我不知道,但如果我理解你的權利,你必須設置你的觀點的背景只有一次,但它應該是選擇繪製,這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item 
     android:state_pressed="true" 
     android:drawable="*background for pressed state*" /> 
    <item 
     android:state_focused="true" 
     android:drawable="*background for focused state*" /> 
    <item 
     android:drawable="*default background*" /> 

</selector> 
3

在列表視圖試試這個: -

android:listSelector="@color/orange" 
相關問題