我有一个listview,我用一个包含1个ImageView,2个TextView和1个Button的自定义布局来扩充它。我想在单击列表视图项时更改按钮背景,但我无法执行它。有谁可以帮助我吗?非常感谢你。
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类中做到这一点?
答案 0 :(得分:2)
好的..所以基本上你需要一个状态列表来实现它..请参阅this以了解如何创建它...然后将其设置为按钮的背景..
答案 1 :(得分: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
}
答案 2 :(得分: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(...)
}
});
)