【发布时间】:2012-03-21 04:58:20
【问题描述】:
我在更改 ImageView 的状态时遇到问题,该状态将列表项标记为已添加书签。
人们已经提到要创建一个新的OnClickListener(),但是当我单击 ImageView 时,它会进入焦点状态,但是当您完成单击时,它不会变为按下状态图像。此外,当我单击列表项时,我注意到 ImageView 再次聚焦。我不确定我错过了什么:
public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
ImageView image;
public DxSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
long id = getItemId(position);
final ImageView image = (ImageView) view.findViewById(R.id.fav);
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
image.setBackgroundResource(R.drawable.ic_fav);
final Button btn = (Button) v.findViewById(R.id.fav);
btn.setPressed(true);
}
});
return view;
}
}
我已经为 ImageView 状态创建了 xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_star_off_normal"
android:state_checked="false"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_normal"
android:state_checked="true"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_pressed"
android:state_checked="true"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_off_pressed"
android:state_checked="false"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_on_focused"
android:state_checked="true"
android:state_focused="true" />
<item android:drawable="@drawable/btn_star_off_focused"
android:state_checked="false"
android:state_focused="true" />
<item android:state_checked="false" android:drawable="@drawable/btn_star_off_normal" />
<item android:state_checked="true" android:drawable="@drawable/btn_star_on_normal" />
</selector>
在我的活动中,我创建了以下OnListItemClick() 来创建一个弹出消息,但似乎也将焦点放在ImageView 上。
public void onListItemClick(ListView parent, View view, int position, long id) {
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor c = (Cursor) getListAdapter().getItem(position);
String arg = c.getString(c.getColumnIndex("_id"));
Cursor cursor = db.rawQuery("Select category, subcategory From DiagLookup Where _id = ?", new String[]{""+arg});
cursor.moveToFirst();
Context context = getApplicationContext();
CharSequence text = "Category: " + cursor.getString(cursor.getColumnIndex("category")) + "\nSubcategory: " + cursor.getString(cursor.getColumnIndex("subcategory"));
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
澄清我想要发生的事情:
我有我想做两件事的清单项目;单击该项目并获得一条 toast 消息,然后单击图像视图并将图像视图切换到图像和后端的书签状态。
目前我的 toast 消息正常,但 imageview 不完全正常...
我附上了一个列表项的屏幕截图,以显示它是如何设置的,并解释了当我单击各个区域时发生的情况。
顶部列表项已被单击并按住,我得到的是图像更改为集中但未单击的图像,如上面的可绘制 xml 所示,但是当我完成单击时,星形变得比其他条目更深灰色但是它不会变成黄色星形的单击状态图像。
当我点击其他任何地方而不是星星时,会弹出 toast 消息,这是我打算发生的,但我也注意到星星也变得聚焦。我觉得我遇到了一些继承问题,也没有让状态保持在 ImageView 上。
【问题讨论】:
标签: java android onclick imageview onclicklistener