Android - 自定义 ArrayAdapter - 无法识别对 ListItems 的点击

Android - Custom ArrayAdapter - Clicks on ListItems are not recognized

我实现了一个自定义 ArrayAdapter,这会导致 ListView 无法识别对 ListItem 的 TextView 元素的任何点击;但是,单击 ListItem 的 ImageView 元素会产生预期的效果。我只是无法弄清楚这种行为是如何导致的,即使我按照教程进行操作也很困难。

当我使用标准 ArrayAdapater.

时一切正常

我将包括所涉及的 类,因为我不知道他们中的哪一个与解决此问题相关。

CustomArrayAdapter:

public class ClassCustomArrayAdapter extends ArrayAdapter {

    private ArrayList<ArrayList<String>> contactsArray;
    private Context context;

    public ClassCustomArrayAdapter(Context context, int textViewResourceId, ArrayList<ArrayList<String>> contactsArray) {

        super(context, textViewResourceId, contactsArray);

        this.contactsArray = contactsArray;
        this.context = context;
    }

    private class ViewHolder{
        ImageView imageView;
        TextView textView;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if(convertView == null) {
            convertView = layoutInflater.inflate(R.layout.fragment_custom_listview_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView.findViewById(R.id.tv);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(String.format("%s %s", contactsArray.get(position).get(2), contactsArray.get(position).get(1)));
        viewHolder.imageView.setImageResource(R.drawable.ic_baseline_person_24);

        return convertView;
    }

}

FragmentListContacts:

public class FragmentListContacts extends Fragment {

    private ArrayList<ArrayList<String>> contacts_arraylist;
    private ArrayList<ArrayList<String>> contacts_array_listview;
    private ArrayAdapter<String> contact_ids;
    private ArrayAdapter<ArrayList<String>> contacts_arrayadapter;
    private ListView contacts_listview;
    private FloatingActionButton add_contact_fab;
    private Database db;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list_contacts, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        contacts_listview = getActivity().findViewById(R.id.contacts_listview);
        add_contact_fab = getActivity().findViewById(R.id.add_contact_fab);
        initAddContactFAB();
        db = new Database(getContext());
        //db.insertNewContact("A", "B", "123", "321");
        //db.insertNewContact("C", "D", "456", "654");
        contacts_arraylist = new ArrayList<>();
        contacts_arraylist.addAll(db.getContacts());

        //contacts_arrayadapter = new ArrayAdapter<ArrayList<String>>(getContext(), android.R.layout.simple_list_item_1, android.R.id.text1, contacts_arraylist);

        ClassCustomArrayAdapter classCustomArrayAdapter = new ClassCustomArrayAdapter(getContext(), R.layout.fragment_custom_listview_item, contacts_arraylist);
        //contacts_listview.setAdapter(contacts_arrayadapter);
        contacts_listview.setAdapter(classCustomArrayAdapter);
        
        contacts_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getContext(), ActivityListNotesByContact.class);
                startActivity(intent);
            }
        });

        registerForContextMenu(contacts_listview);
    }

    ...
}

custom_listview_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:longClickable="true"
        android:clickable="true"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tv"
        android:gravity="center_vertical"
        android:padding="5px"
        android:layout_marginLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textSize="60px"
        android:textColor="#000000"
        android:background="#FFFFFF"
        android:longClickable="true"
        android:clickable="true"/>

</LinearLayout>

提前致谢!

ListView setOnItemClickListener 拦截列表项根视图的点击,当您启用 TextView 可点击时,它有自己的点击监听器不会被 ListView setOnItemClickListener .

拦截

解法:

您需要将 TextViewImageView 设为不可点击或聚焦

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

您可以使用下面的列表项布局..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical"
        android:clickable="false"
        android:focusable="false"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:clickable="false"
        android:focusable="false"
        android:layout_marginLeft="5dp"
        android:background="#FFFFFF"
        android:gravity="center_vertical"
        android:textColor="#000000" />

</LinearLayout>