ListView onclick监听器无法正常工作

时间:2017-05-09 19:07:32

标签: android listadapter onitemclicklistener

在我的AppCompatActivity扩展活动中,我在onCreate方法中加载了一个列表视图:

我在initListView()方法中调用OnCreate

 public void initListView(){

        ListView listView = (ListView)findViewById(R.id.more_list_view);
        listView.setTextFilterEnabled(true);
        listView.setAdapter(new MoreViewAdapter(this,loadDataSource()));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }

列表视图加载正常,但我无法获得每行的onClick侦听器。断点没有giet hith也没有吐司!

MoreViewAdapter.java

public class MoreViewAdapter implements ListAdapter {

    private final Context context;
    private final List<Map<String, Integer>> values;

    public MoreViewAdapter(Context context, List<Map<String, Integer>> values) {
        this.context = context;
        this.values = values;

    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

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

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

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
                LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.more_list_cell, viewGroup, false);
        TextView textView = (TextView) rowView.findViewById(R.id.more_cel_text);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.more_cell_icon);

        Map dicObject = values.get(i);
        String rowTitle = (String) dicObject.get("rowTitle");
        textView.setText(rowTitle);
        imageView.setBackgroundResource(Integer.parseInt((String) dicObject.get("icon")));

        return rowView;
    }

    @Override
    public int getItemViewType(int i) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

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

}

我在行xml中将clickable,focusable,focusableInTouchMode设置为false。

以下是列表单元格的xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/more_cell_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="10dp"
            android:clickable="false"     
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:background="@drawable/results" />

        <TextView
            android:id="@+id/more_cel_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="none"
            android:background="@null"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="10dp"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"

            android:text="Name" />
    </LinearLayout>
</LinearLayout>

activity_content的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.logitapp.LogIt.Activities.MoreActivity"
    tools:showIn="@layout/activity_more">

    <ListView
        android:id="@+id/more_list_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:2)

1. 将ListAdapter更改为BaseAdapter或ArrayAdapter

2. 在getView方法中实现onClick侦听器

rowView.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
       //handle click here
    }

});

使可点击,可聚焦等为真或删除这些属性。

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

同时删除以下属性

android:descendantFocusability="blocksDescendants"


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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/more_cell_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="10dp"
            android:clickable="true"     
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/results" />

        <TextView
            android:id="@+id/more_cel_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="none"
            android:background="@null"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="10dp"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"

            android:text="Name" />
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:1)

MoreViewAdapter类中删除以下重写方法:

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public int getItemViewType(int i) {
    return 0;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean isEmpty() {
    return false;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

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

更新getItemId()方法如下:

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

以下是您的最终MoreViewAdapter课程:

public class MoreViewAdapter implements ListAdapter {

    private final Context context;
    private final List<Map<String, Integer>> values;

    public MoreViewAdapter(Context context, List<Map<String, Integer>> values) {
        this.context = context;
        this.values = values;

    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
                LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.more_list_cell, viewGroup, false);
        TextView textView = (TextView) rowView.findViewById(R.id.more_cel_text);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.more_cell_icon);

        Map dicObject = values.get(i);
        String rowTitle = (String) dicObject.get("rowTitle");
        textView.setText(rowTitle);
        imageView.setBackgroundResource(Integer.parseInt((String) dicObject.get("icon")));

        return rowView;
    }
}

答案 2 :(得分:0)

在我的自定义适配器类中,我已经覆盖了这些方法,并且我能够让onclick侦听器工作。

    @Override
    public boolean areAllItemsEnabled() {
        return true; //by default these are false
    }

    @Override
    public boolean isEnabled(int i) {
        return true;
    }