ListView + ImageButton + 后代Focusability [英] ListView + ImageButton + descendantFocusability

查看:15
本文介绍了ListView + ImageButton + 后代Focusability的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,我向其中添加了 1 个图像按钮的行.. 我试图将图像按钮设置为 setfocusable false 但仍然无法正常工作..

I have a listview to which I add rows with 1 imagebutton .. I tried to set the imagebutton the setfocusable false but still not working ..

item_list.xml

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

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;
}

这是我的主要"代码..我想在这里检测新闻

This is my 'main' code.. i want detect the press here

    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 对象上设置这两个:

I believe in order to have BOTH the listView row and the ImageButton clickable, you'll need to set both of these on the ImageButton object:

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

试一试.我也会尝试在 ImageButton 上使用 onClickListener 而不是 touchListener.

Give that a shot. I'd also try using an onClickListener on your ImageButton instead of the touchListener.

这篇关于ListView + ImageButton + 后代Focusability的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆