2014-01-05 22 views
0

我有一個列表視圖包含一個按鈕和一個文本視圖正在點擊正常;但我有一個按鈕點擊的方法,當點擊按鈕它應該改變背景圖像,所以通過點擊一個按鈕所有的按鈕實例正在改變背景圖像,所以我點擊列表視圖中的fiew按鈕和向下滾動,我看到幾個按鈕的背景圖像改變的背景圖像比改變背景更改的按鈕的位置,同時再次滾動列表視圖; 我該如何解決這個問題?我有一個按鈕在列表視圖中,當點擊所有的實例被點擊

我的ListView的內容的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <Button 
    android:id="@+id/bt_rating" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:background="@android:drawable/btn_star_big_off" 
    android:onClick="myClickHandler"/> 

<TextView 

android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="@dimen/fsinlistview" 

/> 
</LinearLayout> 

我按一下按鈕的代碼是:

public void myClickHandler(View v) 
    { 


    Button button = (Button)v.findViewById(R.id.bt_rating);  
    button.setBackgroundResource(android.R.drawable.btn_star_big_on); 


    } 
+0

「myClickHandler」方法的代碼是什麼樣的? –

回答

0

你指定的背景資源,所有按鈕與R.id.bt_rating ID,在那裏你想修改按鈕是點擊

你已經有了一個參考 - 它是在通過了View

你的代碼更改爲:

public void myClickHandler(View v) 
{ 
    v.setBackgroundResource(android.R.drawable.btn_star_big_on); 
} 

當然,由於您使用的列表視圖,一旦你。滾動/過去,它不會持續狀態。你應該做的是:

  1. 更新數據適配器
  2. 處理按鈕在適配器/關狀態顯示getView
+0

當我打電話v.setbackgroundRessource ...我有相同的結果幾個按鈕,其中調用.. –

+0

當你說'幾',你的意思是在屏幕上一次,或者他們只顯示滾動後? –

+0

它們在滾動後顯示 –

0

把上備份您的適配器

  • 通話notifyDataSetChanged setOnclickListener在你的適配器的getView方法中

  • 相關問題