ListView 的 setOnItemClickListener 不起作用

setOnItemClickListener of ListView doesn't work

我有一个很奇怪的情况。我在列表视图中显示了一些产品及其详细信息,这些产品是通过内容提供程序从 SQLite 数据库中读取的。但是,当我单击列表视图的任何行时,没有任何反应。所以我发布了一些我的代码。

MainActivity

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private static final String TAG = MainActivity.class.getSimpleName();
private static final int PRODUCT_LOADER = 0;
ProductCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddProductDialog();
        }
    });

    ListView productListView = (ListView) findViewById(R.id.list_view_product);

    View emptyView = findViewById(R.id.empty_view);
    productListView.setEmptyView(emptyView);

    mCursorAdapter = new ProductCursorAdapter(this, null);
    productListView.setAdapter(mCursorAdapter);

    productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent i = new Intent(MainActivity.this,EditActivity.class);
            Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI,id);
            i.setData(currentProductUri);
            startActivity(i);
        }
    });

    getLoaderManager().initLoader(PRODUCT_LOADER, null, this);
}

private void showAddProductDialog() {
    AddDialogFragment newFragment = new AddDialogFragment();
    newFragment.show(getSupportFragmentManager(), getString(R.string.add_product));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
            ProductEntry._ID,
            ProductEntry.COLUMN_PRODUCT_NAME,
            ProductEntry.COLUMN_PRODUCT_QUANTITY,
            ProductEntry.COLUMN_PRODUCT_PRICE,
            ProductEntry.COLUMN_PRODUCT_IMAGE};

    return new CursorLoader(this,
            ProductContract.ProductEntry.CONTENT_URI,
            projection,
            null,
            null,
            null
    );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursorAdapter.swapCursor(null);
}
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
tools:context="com.example.android.invetoryapp.MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_input_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="@dimen/fab_margin" />

<ListView
    android:id="@+id/list_view_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_margin"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

    />
<RelativeLayout
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/empty_title_text"
        style="@style/EmptyTextView"
        android:text="@string/empty_view_subtitle_text" />
</RelativeLayout>

如您所见,我正在使用这些属性,正如我发现的不同答案中所建议的那样,但是当我单击一行时仍然没有任何反应。

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

我该如何解决这个错误?

谢谢,

西奥

从 xml 中的 ListView 中删除以下行;

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

要么删除这些行

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

或让它们成为现实

android:focusable="true"
android:focusableInTouchMode="true"

删除这些行

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

并添加 Activity

listview.setOnClickListener

尝试在 activity_main.xmlListView 属性中添加以下行,如下所示:-

android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"

将此行添加到列表视图:

android:focusable="true"
android:focusableInTouchMode="true" 

将此行添加到具有可聚焦功能的其他视图:

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