我在更改ImageView
的状态时遇到问题,它将列表项标记为书签。ImageView onClickListener()
人们已经提到要创建一个新的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消息,然后单击ImageView并将图像视图切换到图像中以及后端的书签状态。
目前我有吐司消息工作,但ImageView的不完全是工作......
我附上一个列表项的截图展示它是如何设置和解释,当我点击正在发生的事情在各个领域。
顶部列表项已被点击并举行,我所得到的是图像变为聚焦,但是未点击图像,如图我上面绘制XML,但是当我完成点击星标变暗灰色,然后是其他条目,但它不会转到黄色星形的点击状态图像。
当我点击其他地方,而不是星上,然后弹出这是我打算发生的敬酒信息,但我也注意到,明星也成为焦点。我觉得我有一些继承问题,也没有得到坚持ImageView的状态。
这篇文章以及其中提到的链接在获取imageview使用xml drawable改变状态方面非常有帮助。我如何解决这个问题,而不是在drawable xml中使用android:state_checked,而是用android:state_selected替换它,然后view.setSelected(true);在onClick()事件中。 – DeucePie 2012-03-21 23:53:02