2012-04-20 133 views
1

我有一个列表视图,并用一个包含1个ImageView,2个TextView和1个按钮的自定义布局给它充气。我想在点击ListView项目时更改按钮背景,但我无法执行它。有人能帮助我吗?提前谢谢你。当你点击列表视图项目时如何更改按钮背景

enter image description here

row_segnalibro.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout01" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" android:layout_height="50dp" > 

<ImageView android:id="@+id/ImageView01" android:src="@drawable/star" 
android:layout_height="40dp" 
    android:layout_width="40dp" 
    android:enabled="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false" 
    android:paddingLeft="10dp" /> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout02" 
    android:orientation="vertical" 
    android:layout_width="220dp" android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/tvRow1" 
    android:layout_width="220dp" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false" 
    android:paddingLeft="10dp"  
    android:paddingTop="5dp" 
    android:textColor="#0967AD" 
    android:textStyle="bold" 
    /> 
<TextView 
    android:id="@+id/tvRow2" 
    android:layout_below="@id/tvRow1" 
    android:layout_width="220dp" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false" 
    android:paddingLeft="10dp"  
    android:textColor="#0967AD" 
    android:textStyle="bold" 
/> 
</RelativeLayout> 
<Button 
    android:id="@+id/butt_segnalib" 
    android:background="@drawable/freccia_o" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_gravity="center" /> 

Segnalibro.java

public class Segnalibro extends ListActivity{ 
      super.onCreate(savedInstanceState); 
     setContentView(R.layout.segnalibro); 

     lv = (ListView) getListView();   
.....some code 
     MySimpleCursorAdap myadap = new MySimpleCursorAdap(getApplicationContext(),R.layout.rowlayout_segnalibro,curr,campi, to); 
     lv.setAdapter(myadap); 
     lv.setClickable(true); 

      lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v,int position, long arg3) { 
        Log.i("","I have clicked on item "+position); 

      }   
     }); 

MySimpleCursorAdap.java

 public class MySimpleCursorAdap extends SimpleCursorAdapter{ 

     public MySimpleCursorAdap(Context context, int layout, Cursor c, String[] from, int[] to) { 
      super(context, layout, c, from, to); 
     this.mLayout = layout; 
     this.curr = c; 
       this.cont = context; 
      } 

public void bindView(View view, Context context, final Cursor cursor) { 

    ....some code 

/*  Button butt = (Button) view.findViewById(R.id.butt_segnalib); 
      butt.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
    .....code to change the background 
    } 
     }); */ 
    } 

in MySimpleCursorAdap我可以通过findViewById方法获得Button,我怎么能在类Segnalibro中做到这一点?

+0

是** ** freccia_o是国家名单..? – ngesh 2012-04-20 10:17:01

+0

freccia_o是一个PNG图像 – sleone08 2012-04-20 10:18:54

回答

2

好了..所以基本上你需要acheiving它的状态列表..看到this知道如何创建它..然后将它设置为按钮的背景..

+0

非常感谢,我已经使用了signpainter的解决方案。也许我会尝试你的解决方案好奇:) – sleone08 2012-04-20 10:45:24

+1

@crazykeyboard ..可能是有效的..使用状态列表是首选,因为你没有保持改变按钮图像在运行时每次点击... – ngesh 2012-04-20 10:50:36

+0

好吧,谢谢你你的建议,我现在明白了。我会尝试使用它。祝你有个美好的一天;) – sleone08 2012-04-20 12:17:10

1

当您使用ListActivity作为基类时,您不必自己创建onItemClickListener,它已经在那里。只需覆盖protected void onListItemClick(ListView l, View v, int position, long id)。在里面你将能够通过它的id获取按钮。

http://developer.android.com/reference/android/app/ListActivity.html

protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    Button bt = (Button) v.findViewById(R.id.button); 

    //do something fancy with the button 
} 
+0

非常感谢你的作品,你让我的一天非常开心。祝你有美好的一天:) – sleone08 2012-04-20 10:37:16

+1

@crazykeyboard如果你只是想改变按钮的背景,因为它的状态发生变化(按下,聚焦等),我会使用sandy的解决方案。这样你就可以在一个地方完成所有工作,并可以在其他活动中轻松地重新使用该按钮上的效果。 – signpainter 2012-04-20 11:34:49

1

尝试添加ClickListener在bindView方法列表项的看法:

public void bindView(View view, Context context, final Cursor cursor) { 

    //some code 

    view.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     final ImageButton button = (ImageButton)v.findViewById(R.id.list_item_button); 
     button.setBackgroundColor(...) 
     } 
    }); 
) 
相关问题