我在更改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