listview setonitemclicklistener不起作用?

时间:2017-09-12 09:34:33

标签: android listview android-arrayadapter onitemclicklistener

CustomAdapter.class

 public class CustomAdapter extends ArrayAdapter<String> {
    Context context;
    int[] images;
    String[] titleArray;
    String[] descriptionArrayl;
    public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl) {
super(c,R.layout.row,R.id.etTitle,memeTitles);
        this.context=c;
        this.images = imgs;
        this.titleArray = memeTitles;
        this.descriptionArrayl=descriptionArrayl;
    }
    //Sub class of CustomAdapter
    class MyViewHolder{
        ImageView myImage;
        TextView myTitle;
        TextView myDescription;
        MyViewHolder(View v){

            myImage= (ImageView) v.findViewById(R.id.imageView);
            myTitle = (TextView) v.findViewById(R.id.etTitle);
            myDescription = (TextView) v.findViewById(R.id.etDescriptions);
        }
    }

    //Get View Method of CustomAdapter class
    public View getView(int position, View convertView, ViewGroup parent){
        View row=convertView;
        MyViewHolder myViewHolder =null;
        if (row==null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row, parent, false);
            myViewHolder = new MyViewHolder(row);
            Log.d("List View","Creating New Row");
            row.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) row.getTag();
            Log.d("List View","Recycling Stuff");

        }
        myViewHolder.myImage.setImageResource(images[position]);
        myViewHolder.myTitle.setText(titleArray[position]);
        myViewHolder.myDescription.setText(descriptionArrayl[position]);


        return row;
    }
}

MainActivity.class

package com.faisal.listviewtask;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    String[] memeTitles;
    String[] memeDescriptions;
    CustomAdapter customAdapter;
    int[] image = {R.drawable.meme1, R.drawable.meme2,
            R.drawable.meme3, R.drawable.meme4,
            R.drawable.meme5, R.drawable.meme6,
            R.drawable.meme7, R.drawable.meme8,
            R.drawable.meme9, R.drawable.meme10,
            R.drawable.meme1, R.drawable.meme2,
            R.drawable.meme3, R.drawable.meme4,
            R.drawable.meme5, R.drawable.meme6,
            R.drawable.meme7, R.drawable.meme8,
            R.drawable.meme9, R.drawable.meme10};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);


        Resources res = getResources();
        memeTitles = res.getStringArray(R.array.titles);
        memeDescriptions = res.getStringArray(R.array.descriptions);

        customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions);
        listView.setAdapter(customAdapter);

        listView.setItemsCanFocus(false);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show();
                Log.d("onitemclick Listener","called");
            }
        });

    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:descendantFocusability="blocksDescendants"
    tools:context="com.faisal.listviewtask.MainActivity">
    <ListView
        android:clickable="true"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
    </ListView>

</RelativeLayout>

row layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/imageView"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_margin="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/etTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Title"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@id/imageView"
        android:layout_alignParentRight="true"
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/etDescriptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:textSize="15sp"
        android:layout_below="@+id/etTitle"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/imageView" />
</RelativeLayout>

这是我给出的所有代码。我在列表视图的行中添加了android:focusable="false"android:focusableInTouchMode="false"等所有标记,并在listview的根布局中添加了android:descendantFocusability="blocksDescendants",在listView.setItemsCanFocus(false);之前的代码中添加了listView.setOnItemClickListener但是在此onitem之后,单击侦听器不起作用且没有响应。

2 个答案:

答案 0 :(得分:0)

        add this in Activity [updated]

        customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions,new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
        int i=(Integer)view.getTag();
                       Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show();
                        Log.d("onitemclick Listener","called");
                    }


                });
                listView.setAdapter(customAdapter);

        And add id as rootLayout in row xml

        public class CustomAdapter extends ArrayAdapter<String> {
            Context context;
            int[] images;
            String[] titleArray;
            String[] descriptionArrayl;
    OnClickListener onClickListener;
            public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl,OnClickListener onClickListener) {
        super(c,R.layout.row,R.id.etTitle,memeTitles);
                this.context=c;
                this.images = imgs;
                this.titleArray = memeTitles;
                this.descriptionArrayl=descriptionArrayl;
                this.onClickListener=onClickListener;
            }
            //Sub class of CustomAdapter
            class MyViewHolder{
                ImageView myImage;
                TextView myTitle;
                TextView myDescription;
                RelativeLayout rootLayout;
                MyViewHolder(View v){
        rootLayout=(RelativeLayout)v.findViewById(R.id.rootLayout);
                    myImage= (ImageView) v.findViewById(R.id.imageView);
                    myTitle = (TextView) v.findViewById(R.id.etTitle);
                    myDescription = (TextView) v.findViewById(R.id.etDescriptions);
                }
            }

            //Get View Method of CustomAdapter class
            public View getView(int position, View convertView, ViewGroup parent){
                View row=convertView;
                MyViewHolder myViewHolder =null;
                if (row==null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.row, parent, false);
                    myViewHolder = new MyViewHolder(row);
                    Log.d("List View","Creating New Row");
                    row.setTag(myViewHolder);
                }else {
                    myViewHolder = (MyViewHolder) row.getTag();
                    Log.d("List View","Recycling Stuff");

                }
                myViewHolder.myImage.setImageResource(images[position]);
                myViewHolder.myTitle.setText(titleArray[position]);
                myViewHolder.myDescription.setText(descriptionArrayl[position]);
        rootLayout.setTag(position);
        rootLayout.setOnClickListener(onClickListener);

                return row;
            }
        }


row layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<RelativeLayout
android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/imageView"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_margin="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/etTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Title"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@id/imageView"
        android:layout_alignParentRight="true"
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/etDescriptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:textSize="15sp"
        android:layout_below="@+id/etTitle"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/imageView" />
</RelativeLayout>
</RelativeLayout>

答案 1 :(得分:0)

如果您的所有代码都正确并且代码中没有异常但您的ListView On ItemClickListener无法正常工作则需要使用
在Android工作室中重新启动/无效缓存选项然后运行Listview项目,然后OnItemClick侦听器正常工作。 单击android studio左上角的File Option,选择invalidate caches / restart选项。