[自定义ListView点击问题对Android中的项目

问题描述 投票:110回答:4

所以我有一个自定义ListView对象。列表项具有两个彼此叠放的textview,以及一个水平进度条,在我实际执行操作之前,我一直希望保持水平状态。最右边是一个复选框,仅在用户需要将更新下载到其数据库时才显示。通过将可见性设置为Visibility.GONE禁用复选框时,我可以单击列表项。当该复选框可见时,我无法单击列表中除复选框以外的任何内容。我已经进行了一些搜索,但没有找到与我目前的情况有关的任何内容。我找到了this question,但是由于我使用ArrayLists内部包含数据库列表,所以我使用了重写的ArrayAdapter。我是否只需要获取LinearLayout视图并像Tom一样添加onClickListener?我不确定。

这是列表视图行布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/UpdateNameText"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center_vertical"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/UpdateStatusText"
            android:singleLine="true"
            android:ellipsize="marquee"
            />
        <ProgressBar android:id="@+id/UpdateProgress" 
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content"
                     android:indeterminateOnly="false" 
                     android:progressDrawable="@android:drawable/progress_horizontal" 
                     android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal" 
                     android:minHeight="10dip" 
                     android:maxHeight="10dip"                    
                     />
    </LinearLayout>
    <CheckBox android:text="" 
              android:id="@+id/UpdateCheckBox" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              />
</LinearLayout>

这是扩展ListActivity的类。显然它仍在开发中,因此可以原谅遗漏或遗留的东西:

public class UpdateActivity extends ListActivity {

    AccountManager lookupDb;
    boolean allSelected;
    UpdateListAdapter list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lookupDb = new AccountManager(this);
        lookupDb.loadUpdates();

        setContentView(R.layout.update);
        allSelected = false;

        list = new UpdateListAdapter(this, R.layout.update_row, lookupDb.getUpdateItems());
        setListAdapter(list);

        Button btnEnterRegCode = (Button)findViewById(R.id.btnUpdateRegister);
        btnEnterRegCode.setVisibility(View.GONE);

        Button btnSelectAll = (Button)findViewById(R.id.btnSelectAll);
        btnSelectAll.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                allSelected = !allSelected;

                for(int i=0; i < lookupDb.getUpdateItems().size(); i++) {
                    lookupDb.getUpdateItem(i).setSelected(!lookupDb.getUpdateItem(i).isSelected());
                }

                list.notifyDataSetChanged();
                // loop through each UpdateItem and set the selected attribute to the inverse 

            } // end onClick
        }); // end setOnClickListener

        Button btnUpdate = (Button)findViewById(R.id.btnUpdate);
        btnUpdate.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
            } // end onClick
        }); // end setOnClickListener

        lookupDb.close();
    } // end onCreate


    @Override
    protected void onDestroy() {
        super.onDestroy();

        for (UpdateItem item : lookupDb.getUpdateItems()) {
            item.getDatabase().close();        
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        UpdateItem item = lookupDb.getUpdateItem(position);

        if (item != null) {
            item.setSelected(!item.isSelected());
            list.notifyDataSetChanged();
        }
    }

    private class UpdateListAdapter extends ArrayAdapter<UpdateItem> {
        private List<UpdateItem> items;

        public UpdateListAdapter(Context context, int textViewResourceId, List<UpdateItem> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = null;

            if (convertView == null) {
                LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = li.inflate(R.layout.update_row, null);
            } else {
                row = convertView;
            }

            UpdateItem item = items.get(position);

            if (item != null) {
                TextView upper = (TextView)row.findViewById(R.id.UpdateNameText);
                TextView lower = (TextView)row.findViewById(R.id.UpdateStatusText);
                CheckBox cb = (CheckBox)row.findViewById(R.id.UpdateCheckBox);

                upper.setText(item.getName());
                lower.setText(item.getStatusText());

                if (item.getStatusCode() == UpdateItem.UP_TO_DATE) {
                    cb.setVisibility(View.GONE);
                } else {
                    cb.setVisibility(View.VISIBLE);
                    cb.setChecked(item.isSelected());
                }

                ProgressBar pb = (ProgressBar)row.findViewById(R.id.UpdateProgress);
                pb.setVisibility(View.GONE);
            }
            return row;
        }

    } // end inner class UpdateListAdapter
}

编辑:我仍然有这个问题。我在作弊并将onClick处理程序添加到textviews中,但是当我不单击复选框时,根本没有调用onListItemClick()函数似乎非常愚蠢。

java android android-listview
4个回答
243
投票

问题是Android不允许您选择列表元素上具有可聚焦元素的列表项。我修改了列表项上的复选框,使其具有如下属性:

android:focusable="false"

现在,我的包含复选框的列表项(也适用于按钮)在传统意义上是“可选的”(它们亮起,您可以单击列表项中的任何位置,并且将触发“ onListItemClick”处理程序,等等。

编辑:作为更新,评论员提到“只是一个注释,在更改按钮的可见性之后,我不得不再次以编程方式禁用焦点。”


42
投票

如果列表项中有ImageButton,则应在根列表项元素中将descendantFocusability值设置为'blocksDescendants'。

android:descendantFocusability="blocksDescendants"

[focusableInTouchMode视图中的true标志指向ImageButton

android:focusableInTouchMode="true"

12
投票

我也遇到过类似的问题,发现ListView中的CheckBox非常挑剔。发生的事情是将其强加给整个ListItem,并覆盖了onListItemClick。您可能要为此实现一个单击处理程序,并同时为CheckBox设置text属性,而不是使用TextViews。

我也想说一下这个View对象,它可能比CheckBox更好地工作

Checked Text View


8
投票

在列表项的根视图中使用此行

android:descendantFocusability =“ blocksDescendants”

© www.soinside.com 2019 - 2024. All rights reserved.