添加自定义适配器android后,listview无法点击

时间:2016-10-24 16:15:28

标签: android xml android-layout listview

我想从列表视图中获取图像。但问题是无论我创建的列表视图是不可点击的。我也尝试使用onItemclick监听器,但由于listview不可用,因此无法正常工作。似乎listview根本没有可点击的..下面给出了适用于listview的适配器。

// GridView的适配器,包含检测到的面部的详细信息。

private class FaceListAdapter extends BaseAdapter {
    //List<FaceRectangle> ffRect;
    // The detected faces.
    List<Face> faces;

    List<UUID> faceIdList;

    List<IdentifyResult> mIdentifyResults;

    // The thumbnails of detected faces.
    List<Bitmap> faceThumbnails;

    // Initialize with detection result.
    FaceListAdapter(Face[] detectionResult) {
        //ffRect=new ArrayList<>();
        faceIdList = new ArrayList<>();
        faces = new ArrayList<>();
        faceThumbnails = new ArrayList<>();
        mIdentifyResults = new ArrayList<>();

        if (detectionResult != null) {
            faces = Arrays.asList(detectionResult);
            for (Face face: faces) {
                try {
  // Crop face thumbnail with five main landmarks drawn from original image.
                    faceThumbnails.add(ImageHelper.generateFaceThumbnail(mBitmap, face.faceRectangle));
                    faceRect=face.faceRectangle;
                    //ffRect.add(faceRect);

                    faceIdList.add(null);
                } catch (IOException e) {
                    // Show the exception when generating face thumbnail fails.
                    //setInfo(e.getMessage());
                }
            }
        }
    }

    public void setIdentificationResult(IdentifyResult[] identifyResults) {
        mIdentifyResults = Arrays.asList(identifyResults);
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public int getCount() {
        return faces.size();
    }

    @Override
    public Object getItem(int position) {
        return faces.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.item_face_with_description, parent, false);
        }
        convertView.setId(position);

        // Show the face thumbnail.
        ((ImageView)convertView.findViewById(R.id.face_thumbnail)).setImageBitmap(faceThumbnails.get(position));

        if (mIdentifyResults.size() == faces.size()) {
            // Show the face details.
            DecimalFormat formatter = new DecimalFormat("#0.00");
            if (mIdentifyResults.get(position).candidates.size() > 0) {
                String personId = mIdentifyResults.get(position).candidates.get(0).personId.toString();

                String personName = StorageHelper.getPersonName(personId, "person", Face_detection.this);

                String identity = "Person: " + personName + "\n" + "Confidence: " + formatter.format(mIdentifyResults.get(position).candidates.get(0).confidence);

                ((TextView) convertView.findViewById(R.id.text_detected_face)).setText(identity);
                photoAdd.setEnabled(false);
            } else {
                ((TextView) convertView.findViewById(R.id.text_detected_face)).setText(
                        R.string.face_cannot_be_identified);
                photoAdd.setEnabled(true);
               // btmp.add(faceThumbnails.get(position));
                //fRect.add(ffRect.get(position));

            }
        }

        return convertView;
    }
}

> item_face_with_description.xml



       <!-- Copyright (c) Microsoft. All rights reserved. -->

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

        <ImageView
            android:id="@+id/face_thumbnail"
            android:layout_width="80dp"
            android:layout_height="80dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:id="@+id/text_detected_face" />

    </LinearLayout>
  

包含ListView的Xml

        <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/activity_face_detection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="myapptest.techm.com.myapptest.Face_detection">


        <include
            android:id="@+id/tool_bar"
            layout="@layout/tacho_toolbar"></include>


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tool_bar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <Button
                    android:text="Take Again"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="22dp"
                    android:id="@+id/takePhoto"
                    android:layout_below="@+id/camerapreview"
                    android:layout_centerHorizontal="true"
                    android:layout_width="150dp"
                    android:onClick="clickPhoto (Face_detection)"
                    android:visibility="gone" />

                <TextView
                    android:text="Faces in Image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView23"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:layout_below="@+id/takePhoto"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="42dp" />

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:layout_below="@+id/textView23"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="28dp"
                    android:id="@+id/face_detect_list"
                    android:focusable="false" />

                <Button
                    android:text="Add"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="25dp"
                    android:layout_marginStart="25dp"
                    android:layout_marginBottom="103dp"
                    android:id="@+id/photoAdd" />

                <Button
                    android:text="Cancle"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/photoAdd"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="66dp"
                    android:layout_marginEnd="66dp"
                    android:id="@+id/cancle" />

                <SurfaceView
                    android:id="@+id/camerapreview"
                    android:layout_width="350dp"
                    android:layout_height="400dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp" />

                <Button
                    android:text="Identify"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/cancle"
                    android:layout_alignRight="@+id/takePhoto"
                    android:layout_alignEnd="@+id/takePhoto"
                    android:layout_marginRight="18dp"
                    android:layout_marginEnd="18dp"
                    android:id="@+id/identify"
                    android:visibility="gone" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

适配器上的isEnabled方法负责ListView忽略点击。

@Override
public boolean isEnabled(int position) 
{
    return false;
}

您的适配器会覆盖isEnabled并返回false所有位置,表示列表中的每个项目都已被禁用。 已禁用的视图不会收到输入事件。

如果可以禁用列表中的项目,那么您的自定义适配器需要以某种方式跟踪这些项目(例如列表),否则您应该为所有位置返回true