android - 单击 ListView 项时如何更改按钮背景

标签 android listview layout button inflate

我有一个 ListView ,并使用包含 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
     }
        }); */
  }

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

最佳答案

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

关于android - 单击 ListView 项时如何更改按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10244237/

相关文章:

c# - 在哪里指定 FocusVisualStyle ="{x:Null}"?

行的CSS表格高度不均匀

android - Kotlin - 对象表达式中的继承

android - 用对象项填充 listView 的正确方法?

android - 如何将我的 listView 转换为动态 ListView ?我指的是 http ://nhaarman. github.io/ListViewAnimations/#dynamiclistview

c# - 具有静态和动态内容的 WinForms UserControl 的最佳布局?

javascript - CSS3 列和图像

android - Galaxy S3 上无法加载图像

android - PhoneGap Build - 无法为 org.gradle 类型的对象获取未知属性 'GMS_VERSION'

Android 存储 URL 的最佳方式是什么?静态数据成员还是在 String.xml 中?