首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView + ImageButton + descendantFocusability

ListView + ImageButton + descendantFocusability
EN

Stack Overflow用户
提问于 2013-07-06 19:04:47
回答 1查看 8.7K关注 0票数 0

我有一个列表视图,我用1个imagebutton向其中添加行。我试着将imagebutton设置为setfocusable false,但仍然不起作用..

item_list.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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;
}

这是我的“main”代码..我想要侦测这里的媒体

代码语言:javascript
复制
    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) );

        }
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-06 19:13:04

我相信为了使listView行和ImageButton都可单击,您需要在ImageButton对象上同时设置这两个行:

代码语言:javascript
复制
  android:focusable="false"
  android:focusableInTouchMode="false"

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17502317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档