1

我有一个列表视图,并使用包含 1 个 ImageView、2 个 TextView 和 1 个按钮的自定义布局对其进行了扩展。我想在单击 listview item 时更改按钮背景,但我无法执行它。有人能帮助我吗?非常感谢你。

在此处输入图像描述

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
     }
        }); */
  }

在 MySimpleCursorAdap 中,我可以使用 findViewById 方法获取 Button,我如何在 Segnalibro 类中做到这一点?

4

3 回答 3

2

好的..所以基本上你需要一个状态列表来实现它..看到这个知道如何创建它..然后将它设置为你的按钮的背景..

于 2012-04-20T10:21:34.657 回答
1

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

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
}
于 2012-04-20T10:20:13.107 回答
1

尝试在 bindView 方法中为列表项视图添加一个 ClickListener:

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(...)
        }
   });
)
于 2012-04-20T10:31:15.633 回答