自定义ListView项目不可单击

时间:2016-04-25 14:47:34

标签: android listview

我正在开发一个在其主要活动中包含Custom ListView的应用程序。它工作正常,但前天,列表视图项突然变得不可点击。我搜索了很多,但没有答案解决了我的这个问题。它很奇怪。普通列表视图工作正常,但自定义不是。可能是什么问题?

编辑:自定义列表视图正常,没什么奇怪的。我在这里给CustomAdapter课。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_list, null);
    }
    ClassDetailPedia detailPedia = personDetail.get(position);

    if(detailPedia !=null){
        TextView txtPersonName = (TextView) convertView.findViewById(R.id.txtPersonName);
        TextView txtReason = (TextView) convertView.findViewById(R.id.txtReason);
        TextView txtTags = (TextView) convertView.findViewById(R.id.txtTags);
        TextView txtDate = (TextView) convertView.findViewById(R.id.txtDate);
        ImageView imgPerson = (ImageView) convertView.findViewById(R.id.imgPerson);


        txtPersonName.setText(detailPedia.getTitle());
        txtReason.setText("Description: " + detailPedia.getDescription());
        txtTags.setText("Tags: " + detailPedia.getTags());
        txtDate.setText(GetFormattedDate(detailPedia.getDated()));

        if(detailPedia.getImage() != null) {
            imgPerson.setImageURI(detailPedia.getImage());
            imgPerson.setScaleType(ImageView.ScaleType.CENTER);
        }
    }
    return convertView;
}

并扩展ArrayAdapter<ClassDetailPedia> 在这里我设置CustomAdapter

ListView lstPersons = (ListView) findViewById(R.id.lstDetail);
lstPersons.setAdapter(new CustomAdapter(getApplicationContext(), R.layout.custom_list, pediaList));

custom_list.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_list"
android:gravity="center">
<ImageButton
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:src="@drawable/empty_picture"
    android:id="@+id/imgPerson"
    android:scaleType="centerCrop" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="2dp">

    <TextView
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Friday, April 1, 2016 @ 8:30pm"
        android:id="@+id/txtDate"
        android:textStyle="italic"
        android:textSize="12sp"
        android:layout_below="@+id/txtTags"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="3dp"
        android:layout_marginRight="3dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Mr. Bill Gates"
        android:id="@+id/txtPersonName"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="3dp"
        android:singleLine="true"
        android:textSize="17sp"
        android:layout_marginBottom="3dp"
        android:textStyle="bold"/>

    <TextView
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reason: Marketing Strategy"
        android:id="@+id/txtReason"
        android:layout_below="@+id/txtPersonName"
        android:layout_alignParentStart="true"
        android:layout_marginStart="3dp"
        android:singleLine="true"
        android:layout_marginTop="-3dp"
        android:textSize="13sp"/>

    <TextView
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tags: business, start-up"
        android:id="@+id/txtTags"
        android:layout_below="@+id/txtReason"
        android:layout_alignParentStart="true"
        android:layout_marginStart="3dp"
        android:singleLine="true"
        android:textSize="13sp"/>
</RelativeLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

尝试为自定义列表项的根视图添加android:descendantFocusability="blocksDescendants"(在您的情况下为LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_list"
android:gravity="center"
android:descendantFocusability="blocksDescendants"
>
...
</LinearLayout>

答案 1 :(得分:-2)

我制作了新的应用程序,复制并粘贴了所有文件和代码,并且工作正常。我不知道是什么导致了这个问题,以及为什么它完全可以知道。