Android ListView+;图像按钮+;子体聚焦

Android ListView+;图像按钮+;子体聚焦,android,android-listview,imagebutton,listview-adapter,Android,Android Listview,Imagebutton,Listview Adapter,我有一个listview,在其中添加带有1个imagebutton的行。。我试图将imagebutton设置为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=

我有一个listview,在其中添加带有1个imagebutton的行。。我试图将imagebutton设置为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;
}
公共类项\u ListAdapter扩展了BaseAdapter{
私人最终活动;
私人最终ArrayList项目;
私有视图vi;
专用图像按钮;
公共项\u列表适配器(活动活动,ArrayList项){
这个。活动=活动;
这个项目=项目;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共对象getItem(int位置){
返回项目。获取(位置);
}
@凌驾
公共长getItemId(int位置){
return items.get(position.getId();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
vi=转换视图;
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u服务);
vi=充气机充气(R.布局.项目列表,空);
}
Item\u List Item=items.get(位置);
//科罗卡尔·泰特罗
//科罗卡·拉费查
//Colocar el背景
//Hacer-visible-el-Lado安装布局图
TextView txtitulo=(TextView)vi.findViewById(R.id.TextView);
TextView txtFecha=(TextView)vi.findViewById(R.id.textView2);
LinearLayout LL_Installed=(LinearLayout)vi.findViewById(R.id.LL_Installed);
setText(item.getTitle());
setText(item.getFecha());
如果(item.getInstalled()){
LL_已安装。设置可见性(View.VISIBLE);
Resources res=vi.getResources();
位图位图=位图工厂.decodeFile(item.getRutaImagen());
最终BitmapDrawable bd=新的BitmapDrawable(分辨率,位图);
// ----------------------------------
按钮=(ImageButton)vi.findViewById(R.id.ImageButton);
//按钮.立根可拉拔(bd);
按钮.立根接地(bd);
setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
v、 getBackground().setColorFilter(Color.parseColor(#B7B2B0)),PorterDuff.Mode.MULTIPLY;
对数d(“aaa”,“向下”);
返回true;
}else if(event.getAction()==MotionEvent.ACTION\u UP){
v、 getBackground().clearColorFilter();
v、 使无效();
日志d(“aaa”、“UP”);
返回true;
}
返回false;
}
});
}否则{
LL_已安装。设置可见性(视图。不可见);
}
返回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 lv=(ListView)findviewbyd(R.id.ListView);
Item_ListAdapter adapter=新Item_ListAdapter(ListActivity.this,items);
低压设置适配器(适配器);
lv.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView parnet、android.view.view、int位置、长id){
//这是西多普尔萨多的一个项目
Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH\u SHORT.show();
Log.d(“aaa”,字符串.valueOf(位置));
}
});

我认为,要同时单击listView行和ImageButton,您需要在ImageButton对象上设置这两个选项:

  android:focusable="false"
  android:focusableInTouchMode="false"

试一试。我也会尝试在您的ImageButton上使用onClickListener,而不是touchListener。

您需要在布局上使用android:DegenantFocusability=“BlocksDescents”来处理上述问题。@user1959076在我们的例子中有它。