我在 ListView 中制作了一个文件浏览器。每行包含一个 ImageView 的 icon 、一个 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);
}
}
在我的活动中,我有一个名为 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