java - Android中项目的自定义ListView点击问题

标签 java android android-listview

所以我有一个自定义 ListView 对象。列表项有两个堆叠在一起的 TextView ,加上一个水平进度条,我想在我真正做某事之前一直隐藏它。最右边是一个复选框,我只想在用户需要将更新下载到他们的数据库时显示。当我通过将可见性设置为 Visibility.GONE 来禁用复选框时,我可以单击列表项。当复选框可见时,除了复选框之外,我无法单击列表中的任何内容。我已经进行了一些搜索,但没有找到与我目前的情况相关的任何内容。我找到了 this question但我使用的是重写的 ArrayAdapter,因为我使用 ArrayLists 在内部包含数据库列表。我只需要像 Tom 那样获取 LinearLayout View 并添加一个 onClickListener 吗?我不确定。

这是 ListView 行布局 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 处理程序添加到 TextView 中,但是当我没有点击我的复选框时,我的 onListItemClick() 函数根本没有被调用,这似乎非常愚蠢。

最佳答案

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

android:focusable="false"

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

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

关于java - Android中项目的自定义ListView点击问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1121192/

相关文章:

java - 表示一组整数的 boolean 值数组

java - 将对象编码为 xml 并删除 xmlns

java - 将 KeyEvent 发布到焦点组件

java - 我如何找出哪个 JAR 应用程序未使用?

Android SensorManager.java getOrientation 和 getRotationMatrix 算法

android - 将 facebook 的个人资料图片设置为 LinearLayout 背景

android - 如何在通知点击时重新启动/刷新 Activity 内容

android - 向下滚动时 ListView 项目重复

android - 从 AsyncTask 调用自定义对话框

android - 如何更改特定 ListView 的项目或元素的颜色