如何使用我的列表CheckBoxes来选择行?

时间:2013-06-26 22:45:57

标签: java android list checkbox

我在ListView中创建了一个文件浏览器。每行包含一个用于图标的ImageView,用于文件名的TextView和一个CheckBox。在我添加CheckBox之前,单击导航到该目录的目录,但由于添加了CheckBox,目录导航不起作用,单击目录不会执行任何操作。

我想要做的是维护标准文件浏览器功能,即单击目录并导航到该目录,同时允许用户使用CheckBoxes选择目录。

public class MultiSelectIconifiedTextView extends LinearLayout {

        private TextView mText;
        private ImageView mIcon;
        private CheckBox mCheckBox;
        private MultiSelectionIconifiedText mCheckBoxText;

        public MultiSelectIconifiedTextView(Context context, MultiSelectionIconifiedText multiSelectionIconifiedText) {
                super(context);

                /* First Icon and the Text to the right (horizontal),
                 * not above and below (vertical) */
                this.setOrientation(HORIZONTAL);

                mIcon = new ImageView(context);
                mCheckBoxText = multiSelectionIconifiedText;
                mIcon.setImageDrawable(multiSelectionIconifiedText.getIcon());
                mCheckBox = new CheckBox(context);
                mCheckBox.setPadding(0, 0, 20, 0); // 5px to the right

                /* Set the initial state of the checkbox. */
                mCheckBox.setChecked(multiSelectionIconifiedText.getChecked());
                // left, top, right, bottom
                mIcon.setPadding(0, 2, 5, 0); // 5px to the right

                /* At first, add the Icon to ourself
                 * (! we are extending LinearLayout) */
                addView(mIcon,  new LinearLayout.LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                mText = new TextView(context);
                mText.setText(multiSelectionIconifiedText.getText());
                /* Now the text (after the icon) */
                addView(mText, new LinearLayout.LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                mText.setPadding(0, 2,  150, 0);
                addView(mCheckBox, new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }

        public void setText(String words) {
                mText.setText(words);
        }

        public void setIcon(Drawable bullet) {
                mIcon.setImageDrawable(bullet);
        }

        public void setCheckBoxState(boolean bool)
        {
         mCheckBox.setChecked(mCheckBoxText.getChecked());
         mCheckBoxText.setChecked(true);
        }
}

在我的Activity中,我有一个名为local的ListView。

local.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                l_itla.setSelectedPosition(position);
                l_itla.setChecked(true, position);
                String selectedFileString = l_directoryEntries.get(position)
                        .getText();
                currenLocalFile = new File(selectedFileString);
                if (selectedFileString.equals(getString(R.string.current_dir))) {
                    // Refresh
                    browseTo(currentLocalDirectory);
                } else if (selectedFileString
                        .equals(getString(R.string.up_one_level))) {
                    upOneLevel();
                } else {
                    File clickedFile = null;
                    switch (displayMode) {
                    case RELATIVE:
                        clickedFile = new File(currentLocalDirectory
                                .getAbsolutePath()
                                + l_directoryEntries.get(position).getText());
                        localFiles.add(clickedFile);

                        break;
                    case ABSOLUTE:
                        clickedFile = new File(l_directoryEntries.get(position)
                                .getText());
                        localFiles.add(clickedFile);
                        Toast.makeText(FTPConnector.this, localFiles.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    }
                    if (clickedFile != null)
                        currenLocalFile = clickedFile;
                    Toast.makeText(FTPConnector.this, localFiles.toString(), Toast.LENGTH_SHORT).show();
                    browseTo(clickedFile);
                }
            }
        });

当我从我的视图中删除CheckBox代码时,单击这些目录就可以了,就像Checkbox正在阻止ist item.l

1 个答案:

答案 0 :(得分:0)

确实这是一个焦点问题。将mCheckBox.setFocusable(false);添加到我的Checkbox就可以了。现在列表项和CheckBox有不同的操作。