2012-03-19 44 views
0

我在更改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的不完全是工作......

我附上一个列表项的截图展示它是如何设置和解释,当我点击正在发生的事情在各个领域。

ListItem with ImageView clickable

顶部列表项已被点击并举行,我所得到的是图像变为聚焦,但是未点击图像,如图我上面绘制XML,但是当我完成点击星标变暗灰色,然后是其他条目,但它不会转到黄色星形的点击状态图像。

当我点击其他地方,而不是星上,然后弹出这是我打算发生的敬酒信息,但我也注意到,明星也成为焦点。我觉得我有一些继承问题,也没有得到坚持ImageView的状态。

回答

1

检查此链接。

https://stackoverflow.com/a/9754248/1244489

它是一个类似的问题。它比你实施的方式稍微复杂一些。

+0

这篇文章以及其中提到的链接在获取imageview使用xml drawable改变状态方面非常有帮助。我如何解决这个问题,而不是在drawable xml中使用android:state_checked,而是用android:state_selected替换它,然后view.setSelected(true);在onClick()事件中。 – DeucePie 2012-03-21 23:53:02

1

所以你说的是,当你点击图片时,你想要一个Toast消息出现,这就是你想要发生的事情吗?

如果是这样,这是一种我会这样做的方式。

在你的XML中,你需要首先确保你已经将onClick设置到你的按钮或图像上。您需要将其添加到每个变量

android:onClick = "Image1" 

然后在您的java文件中,您需要引用它。您可能要实现OnClickListener到您的活动

public class DxSimpleCursorAdapter extends SimpleCursorAdapter implements OnClickListener{ 

然后参考它在你的java文件

public void Image1 (View v) { 
    Toast.makeText(this, R.string.TEXT, Toast.LENGTH_SHORT).show(); 
} 

你可以做你创建土司的方法,但我的只包含1行代码。

我不确定这是否完全回答你的问题,但这是我所知道的关于迄今为止的Android编码(Java)。我希望这有帮助。

相关问题