首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >listview setonitemclicklistener不工作吗?

listview setonitemclicklistener不工作吗?
EN

Stack Overflow用户
提问于 2017-09-12 17:34:33
回答 2查看 162关注 0票数 1

CustomAdapter.class

代码语言:javascript
复制
 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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

行layout.xml

代码语言:javascript
复制
<?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.setOnItemClickListener之前的代码中添加了listview的根布局中的android:descendantFocusability="blocksDescendants"listView.setItemsCanFocus(false);,但在此onitem单击之后,侦听器不起作用且没有响应。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-03 17:50:17

如果您的所有代码都是正确的,并且代码中没有保留任何异常,但是ItemClickListener上的ListView不起作用,那么您需要使用

Android studio中提供的重新启动/无效缓存选项,然后运行Listview Project,然后OnItemClick Listener就可以正常工作了。单击android studio左上角的File选项,然后选择invalidate /restart选项。

票数 0
EN

Stack Overflow用户

发布于 2017-09-12 17:54:43

代码语言:javascript
复制
        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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46172888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档