我有一个listview,我添加了1个图像按钮的行..我试图设置图像按钮setfocusable false但仍然无法正常工作..
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/RL_item">
<ImageButton
android:layout_width="200dp"
android:layout_height="fill_parent"
android:id="@+id/imageButton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/LL_Installed"
android:layout_alignRight="@+id/textView2"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/item_list_left_right"
android:paddingLeft="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView"
android:background="@drawable/tick"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/list_item_installed"
android:id="@+id/textView3"
android:paddingLeft="6dp"
android:textColor="#ffffff"
style="@style/TextShadow"
android:gravity="center_vertical|fill_vertical"
android:layout_weight="1"
android:layout_width="0dip"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
style="@style/TextShadow"
android:paddingTop="@dimen/item_list_top"
android:paddingRight="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
style="@style/TextShadow"
android:layout_toLeftOf="@+id/textView2"
android:paddingLeft="@dimen/item_list_left_right"
android:paddingTop="@dimen/item_list_top"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
Item_ListAdapter.java
public class Item_ListAdapter extends BaseAdapter {
private final Activity activity;
private final ArrayList<Item_List> items;
private View vi;
private ImageButton button;
public Item_ListAdapter(Activity activity, ArrayList<Item_List> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
vi=convertView;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.item_list, null);
}
Item_List item = items.get(position);
// Colocar el titulo
// Colocar la fecha
// Colocar el background
// Hacer visible o invisible el layout de instalado
TextView txtTitulo = (TextView) vi.findViewById(R.id.textView);
TextView txtFecha = (TextView) vi.findViewById(R.id.textView2);
LinearLayout LL_Installed = (LinearLayout) vi.findViewById(R.id.LL_Installed);
txtTitulo.setText(item.getTitle());
txtFecha.setText(item.getFecha());
if (item.getInstalled()) {
LL_Installed.setVisibility(View.VISIBLE);
Resources res = vi.getResources();
Bitmap bitmap = BitmapFactory.decodeFile(item.getRutaImagen());
final BitmapDrawable bd = new BitmapDrawable(res, bitmap);
// ----------------------------------
button = (ImageButton) vi.findViewById(R.id.imageButton);
//button.setBackgroundDrawable(bd);
button.setBackground(bd);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
Log.d("aaa","DOWN");
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().clearColorFilter();
v.invalidate();
Log.d("aaa","UP");
return true;
}
return false;
}
});
} else {
LL_Installed.setVisibility(View.INVISIBLE);
}
return vi;
}
这是我的“主要”代码..我想在这里检测新闻
ListView lv = (ListView)findViewById(R.id.listView);
Item_ListAdapter adapter = new Item_ListAdapter(ListActivity.this, items);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parnet, android.view.View view, int position, long id) {
// Que item ha sido pulsado
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
Log.d("aaa", String.valueOf(position) );
}
});
解决方法:
我相信为了让listView行和ImageButton可以点击,你需要在ImageButton对象上设置这两个:
android:focusable="false"
android:focusableInTouchMode="false"
试一试.我还尝试在ImageButton上使用onClickListener而不是touchListener.