onListItemClick 在我的自定义 ListActivity 中不起作用

onListItemClick not working in my custom ListActivity

我是 Android 开发的新手。我正在学习它。这不是我完成的第一个练习应用程序,它是前几个应用程序之一。

此应用程序的目的是:屏幕应显示项目列表。每个项目都应显示 SD 卡上特定文件夹中图像的缩略图和文件名。单击该行应将该图像打开为全屏。

我的项目显示在列表中,但这些项目对任何点击都没有反应。我的 onListItemClick() 没有被调用。当我点击一行时,logcat 中没有任何内容。

我自己尝试了很多东西,但都无法正常工作。当我了解到这一点时,我看到了一个例子。

我还在 SO 上阅读了一些相关内容并阅读了教程。例如,我确实在 SO 上看到了类似的问题。一个答案是将其添加到 Textview:

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

所以我尝试将其添加到我的 TextView 和 ImageView 中,但仍然没有成功。

感谢任何帮助,这真的很令人沮丧。

这是初始的 activity:

public class MainActivity extends ListActivity {

    private PictureListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new PictureListAdapter(getApplicationContext());
        readImages();
        setListAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View v, int pos, long l){
        File selectedFile = (File) getListAdapter().getItem(pos);
        Intent intent = new Intent(getApplicationContext(), ImageActivity.class);
        intent.putExtra("fullImage", BitmapFactory.decodeFile(selectedFile.getAbsolutePath()));
        startActivity(intent);
    }

    private void readImages(){
        File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        for(File f : dir.listFiles()){
            mAdapter.add(f);
        }
    }

}

这是我的适配器:

public class PictureListAdapter extends BaseAdapter {

    List<File> mItems = new ArrayList<>();
    Context context;

    //Other implements methods here

    public PictureListAdapter(Context context){
        this.context = context;
    }

    public void add(File file){
        mItems.add(file);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        File picture = mItems.get(pos);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.picture_layout, parent, false);
        view.setClickable(true);

        ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
        final Bitmap fullImage = BitmapFactory.decodeFile(picture.getAbsolutePath());
        imageView.setImageBitmap(Bitmap.createScaledBitmap(fullImage, 160, 120, true));

        TextView textView = (TextView) view.findViewById(R.id.image_filename);
        textView.setText(picture.getName());
        return view;
    }

}

最后,这里是 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="The thumbnail" />

    <TextView
        android:id="@+id/image_filename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

编辑:我现在可以点击了。我刚刚删除了适配器中的行:

view.setClickable(true);

虽然我不明白。当我不再告诉它可点击时它开始可点击?那没有意义。谁能解释一下吗?

顺便说一句,我现在遇到了另一个错误,但我至少点击了这个问题的 onListItemClick() 方法。我现在收到 Failed Binder Transaction,但我想我知道是什么原因造成的,明天会修复它。

是因为你的视图拦截了点击事件。现在您从视图中删除了可点击的 属性 以处理点击的允许列表。