Android:onItemClick 和 onItemLongClick 不响应

Posted

技术标签:

【中文标题】Android:onItemClick 和 onItemLongClick 不响应【英文标题】:Android: onItemClick and onItemLongClick do not respond 【发布时间】:2018-11-11 05:53:52 【问题描述】:

我的 android 应用由一个 SQLite 数据库组成,该数据库使用用户保存的数据填充单个 ListView 项目。这些项目可在activity_main.xml 中显示。 我有一个名为RecordsListFragment 的类,其中包含两个有问题的方法:onItemClickonItemLongClick。这是整个课程:

package com.example.benignfella.projectworkinghoursapplication.Fragment;

import android.app.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.example.benignfella.projectworkinghoursapplication.R;
import com.example.benignfella.projectworkinghoursapplication.Adapter.RecordsListAdapter;
import com.example.benignfella.projectworkinghoursapplication.Database.RecordsDAO;
import com.example.benignfella.projectworkinghoursapplication.GetSet.Records;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class RecordsListFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener 

    public static final String ARGUMENT_ITEM_ID = "records_list";

    Activity activity;
    ListView recordsListView;
    ArrayList<Records> records;

    RecordsListAdapter recordsListAdapter;
    RecordsDAO recordsDAO;

    private GetRecordsTask task;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    activity = getActivity();
    recordsDAO = new RecordsDAO(activity);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    View view = inflater.inflate(R.layout.fragment_records_list, container, false);
    findViewsById(view);

    task = new GetRecordsTask(activity);
    task.execute((Void) null);
    return view;
    

    private void findViewsById(View view) 
    recordsListView = (ListView) view.findViewById(R.id.list_records);
    

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    Records record = (Records) parent.getItemAtPosition(position);

    if (records != null) 
        Bundle arguments = new Bundle();
        arguments.putParcelable("selectedRecord,", record);

        CustomRecordsDialogFragment customRecordsDialogFragment = new CustomRecordsDialogFragment();
        customRecordsDialogFragment.setArguments(arguments);
        customRecordsDialogFragment.show(getFragmentManager(), CustomRecordsDialogFragment.ARGUMENT_ITEM_ID);
    
    

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
    Records records = (Records) parent.getItemAtPosition(position);

    recordsDAO.deleteRecord(records);
    recordsListAdapter.remove(records);

    return true;
    


    public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> 

    private final WeakReference<Activity> activityWeakRef;

    public GetRecordsTask(Activity context) 
        this.activityWeakRef = new WeakReference<Activity>(context);
    

    @Override
    protected ArrayList<Records> doInBackground(Void... arg0) 
        ArrayList<Records> recordsArrayList = recordsDAO.getRecords();
        return recordsArrayList;
    

    @Override
    protected void onPostExecute(ArrayList<Records> empList) 
        if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) 
            records = empList;
            if (empList != null) 
                if (empList.size() != 0) 
                    recordsListAdapter = new RecordsListAdapter(activity, empList);
                    recordsListView.setAdapter(recordsListAdapter);
                 else 
                    Toast.makeText(activity, "No Records about records... wait wot m8?",
                            Toast.LENGTH_LONG).show();
                
            
        
    
    

    public void updateView() 
    task = new GetRecordsTask(activity);
    task.execute((Void) null);
    

    public void onResume() 
    getActivity().setTitle(R.string.app_name);
    getActivity().getActionBar().setTitle(R.string.app_name);
    super.onResume();
    

这里是activity_main.xmlFrameLayout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity">

    <FrameLayout
    android:id="@+id/content_frame"
    android:layout_
    android:layout_/>

</RelativeLayout>

包含ListView 的布局资源文件称为fragment_records_list.xml,它是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="#f9f9f9">

    <ListView
    android:id="@+id/list_records"
    android:layout_
    android:layout_
    android:dividerHeight="10dp"
    android:drawSelectorOnTop="true"
    android:footerDividersEnabled="false"
    android:padding="10dp"
    android:scrollbarStyle="outsideOverlay"/>

</RelativeLayout>

最后,还有一个资源文件,其中包含单个项目的布局,list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="#ededed"
    android:descendantFocusability="blocksDescendants">

    <RelativeLayout
    android:id="@+id/layout_item"
    android:layout_
    android:layout_>

    <TextView
        android:id="@+id/text_record_id"
        android:layout_
        android:layout_
        android:padding="5dp"
        android:text="ID"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/text_record_date"
        android:layout_
        android:layout_
        android:layout_toRightOf="@id/text_record_id"
        android:padding="5dp"
        android:text="Date"
        android:textColor="#00942b"
        android:textSize="20dp"
        />

    <TextView
        android:layout_
        android:layout_
        android:layout_toRightOf="@id/text_record_date"
        android:drawableStart="@drawable/ic_date_range_black_24dp"
        android:padding="5dp"
        />

    <TextView
        android:id="@+id/text_record_description"
        android:layout_
        android:layout_
        android:layout_below="@id/text_record_id"
        android:padding="5dp"
        android:text="Description"
        />

    <TextView
        android:id="@+id/text_record_start"
        android:layout_
        android:layout_
        android:layout_below="@id/text_record_description"
        android:padding="5dp"
        android:text="17:00"
        android:textSize="16dp"
        android:textColor="#004561"
        />

    <TextView
        android:id="@+id/text_record_dash"
        android:layout_
        android:layout_
        android:layout_below="@id/text_record_description"
        android:layout_toRightOf="@id/text_record_start"
        android:padding="5dp"
        android:text="-"
        android:textSize="16dp"
        />

    <TextView
        android:id="@+id/text_record_finish"
        android:layout_
        android:layout_
        android:layout_below="@id/text_record_description"
        android:layout_toRightOf="@id/text_record_dash"
        android:padding="5dp"
        android:text="20:00"
        android:textSize="16dp"
        android:textColor="#c7002a"
        />

    <TextView
        android:layout_
        android:layout_
        android:padding="5dp"
        android:layout_below="@id/text_record_description"
        android:layout_toRightOf="@id/text_record_finish"
        android:drawableStart="@drawable/ic_timer_black_24dp"
       />
    </RelativeLayout>

    <View
    android:layout_
    android:layout_
    android:layout_below="@id/layout_item"
    android:background="#000000"
    />

</RelativeLayout>

我的问题还没有找到明确的答案,所以我在这里寻求一些帮助。

【问题讨论】:

我没有看到你设置了`recordsListView.setOnItemClickListener(this)。这就是为什么它不被称为 setOnItemLongClickListener 我使用了@mr.boyfox 提供的答案,它可以工作。我现在知道我在哪里犯了错误,谢谢你的帮助! 【参考方案1】:

您需要将OnItemClickListenerOnLongItemClickListener 设置为ListView,我在初始化变量ListView 中编辑了您的方法:

private void findViewsById(View view) 
    recordsListView = (ListView) view.findViewById(R.id.list_records);
    recordsListView.setOnItemClickListener(this);
    recordsListView.setOnItemLongClickListener(this);

【讨论】:

以上是关于Android:onItemClick 和 onItemLongClick 不响应的主要内容,如果未能解决你的问题,请参考以下文章

自 android 版本 3.0 起,图库 onItemClick 和 onTouchEvent 不能一起使用

Android ListView 在 onItemClick 之后修改

Android onItemClick 方法不适用于 0 以外的 id 项

Android:带按钮的 ListView -> OnItemClick 啥都不做

java Android Activity + onItemClicker(示例)

android中onItemClick中四个参数的作用