开发者社区> 问答> 正文

Android ListView 点击,求大神帮助 ?

listview 整个区域点击事件怎么获取?
1. 为 listview 设置 onclick 事件 ,程序 直接报错
2. 为listview 添加父容器 Relativelayout ,并对Relativelayout添加 onClick事件,点击没有反应。。
求各位大神帮我解决这个问题

展开
收起
爱吃鱼的程序员 2020-08-21 14:07:24 453 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    使用接口回调和使用抽象类回调。

    正好可以复习一下接口和抽象类的区别,于是写了两个Demo:

    1.使用接口回调:

    Adapter类

    复制代码
    复制代码
    1packagecom.ivan.adapter;
    2
    3importjava.util.List;
    4
    5importandroid.content.Context;
    6importandroid.util.Log;
    7importandroid.view.LayoutInflater;
    8importandroid.view.View;
    9importandroid.view.View.OnClickListener;
    10importandroid.view.ViewGroup;
    11importandroid.widget.BaseAdapter;
    12importandroid.widget.Button;
    13importandroid.widget.TextView;
    14
    15importcom.ivan.listvieweventcallback.R;
    16
    17publicclassContentAdapterextendsBaseAdapterimplementsOnClickListener{
    18
    19privatestaticfinalStringTAG="ContentAdapter";
    20privateListmContentList;
    21privateLayoutInflatermInflater;
    22privateCallbackmCallback;
    23
    24/**
    25*自定义接口,用于回调按钮点击事件到Activity
    26*@authorIvanXu
    27*2014-11-26
    28*/
    29publicinterfaceCallback{
    30publicvoidclick(Viewv);
    31}
    32
    33publicContentAdapter(Contextcontext,ListcontentList,
    34Callbackcallback){
    35mContentList=contentList;
    36mInflater=LayoutInflater.from(context);
    37mCallback=callback;
    38}
    39
    40@Override
    41publicintgetCount(){
    42Log.i(TAG,"getCount");
    43returnmContentList.size();
    44}
    45
    46@Override
    47publicObjectgetItem(intposition){
    48Log.i(TAG,"getItem");
    49returnmContentList.get(position);
    50}
    51
    52@Override
    53publiclonggetItemId(intposition){
    54Log.i(TAG,"getItemId");
    55returnposition;
    56}
    57
    58@Override
    59publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
    60Log.i(TAG,"getView");
    61ViewHolderholder=null;
    62if(convertView==null){
    63convertView=mInflater.inflate(R.layout.list_item,null);
    64holder=newViewHolder();
    65holder.textView=(TextView)convertView
    66.findViewById(R.id.textView1);
    67holder.button=(Button)convertView.findViewById(R.id.button1);
    68convertView.setTag(holder);
    69}else{
    70holder=(ViewHolder)convertView.getTag();
    71}
    72holder.textView.setText(mContentList.get(position));
    73
    74

    75holder.button.setOnClickListener(this);
    76holder.button.setTag(position);
    77returnconvertView;
    78}
    79
    80publicclassViewHolder{
    81publicTextViewtextView;
    82publicButtonbutton;
    83}
    84
    85//响应按钮点击事件,调用子定义接口,并传入View
    86@Override
    87publicvoidonClick(Viewv){
    88mCallback.click(v);
    89}
    90}
    复制代码
    复制代码
    Activity类:

    复制代码
    复制代码
    1packagecom.ivan.listvieweventdemo;
    2
    3importjava.util.ArrayList;
    4importjava.util.List;
    5
    6importandroid.app.Activity;
    7importandroid.os.Bundle;
    8importandroid.view.Menu;
    9importandroid.view.View;
    10importandroid.widget.AdapterView;
    11importandroid.widget.AdapterView.OnItemClickListener;
    12importandroid.widget.ListView;
    13importandroid.widget.Toast;
    14
    15importcom.ivan.adapter.ContentAdapter;
    16importcom.ivan.adapter.ContentAdapter.Callback;
    17importcom.ivan.listvieweventcallback.R;
    18//MainActivity需要实现自定义接口
    19publicclassMainActivityextendsActivityimplementsOnItemClickListener,
    20Callback{
    21
    22//模拟listview中加载的数据
    23privatestaticfinalString[]CONTENTS={"北京","上海","广州","深圳","苏州",
    24"南京","武汉","长沙","杭州"};
    25privateListcontentList;
    26privateListViewmListView;
    27
    28@Override
    29protectedvoidonCreate(BundlesavedInstanceState){
    30super.onCreate(savedInstanceState);
    31setContentView(R.layout.activity_main);
    32
    33init();
    34}
    35
    36privatevoidinit(){
    37mListView=(ListView)findViewById(R.id.listview);
    38contentList=newArrayList();
    39for(inti=0;i<CONTENTS.length;i++){
    40contentList.add(CONTENTS[i]);
    41}
    42//
    43mListView.setAdapter(newContentAdapter(this,contentList,this));
    44mListView.setOnItemClickListener(this);
    45}
    46
    47@Override
    48publicbooleanonCreateOptionsMenu(Menumenu){
    49getMenuInflater().inflate(R.menu.main,menu);
    50returntrue;
    51}
    52
    53/**
    54*响应ListView中item的点击事件
    55/
    56@Override
    57publicvoidonItemClick(AdapterView<?>arg0,Viewv,intposition,longid){
    58Toast.makeText(this,"listview的item被点击了!,点击的位置是-->"+position,
    59Toast.LENGTH_SHORT).show();
    60}
    61
    62/
    *
    63*接口方法,响应ListView按钮点击事件
    64*/
    65@Override
    66publicvoidclick(Viewv){
    67Toast.makeText(
    68MainActivity.this,
    69"listview的内部的按钮被点击了!,位置是-->"+(Integer)v.getTag()+",内容是-->"
    70+contentList.get((Integer)v.getTag()),
    71Toast.LENGTH_SHORT).show();
    72}
    73}
    复制代码
    复制代码
    2.使用抽象类回调

    Adapter类:

    复制代码
    复制代码
    1packagecom.ivan.adapter;
    2
    3importjava.util.List;
    4
    5importandroid.content.Context;
    6importandroid.util.Log;
    7importandroid.view.LayoutInflater;
    8importandroid.view.View;
    9importandroid.view.View.OnClickListener;
    10importandroid.view.ViewGroup;
    11importandroid.widget.BaseAdapter;
    12importandroid.widget.Button;
    13importandroid.widget.TextView;
    14
    15importcom.ivan.listvieweventabstract.R;
    16
    17publicclassContentAdapterextendsBaseAdapter{
    18
    19privatestaticfinalStringTAG="ContentAdapter";
    20privateListmContentList;
    21privateLayoutInflatermInflater;
    22privateMyClickListenermListener;
    23
    24publicContentAdapter(Contextcontext,ListcontentList,
    25MyClickListenerlistener){
    26mContentList=contentList;
    27mInflater=LayoutInflater.from(context);
    28mListener=listener;
    29}
    30
    31@Override
    32publicintgetCount(){
    33Log.i(TAG,"getCount");
    34returnmContentList.size();
    35}
    36
    37@Override
    38publicObjectgetItem(intposition){
    39Log.i(TAG,"getItem");
    40returnmContentList.get(position);
    41}
    42
    43@Override
    44publiclonggetItemId(intposition){
    45Log.i(TAG,"getItemId");
    46returnposition;
    47}
    48
    49@Override
    50publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
    51Log.i(TAG,"getView");
    52ViewHolderholder=null;
    53if(convertView==null){
    54convertView=mInflater.inflate(R.layout.list_item,null);
    55holder=newViewHolder();
    56holder.textView=(TextView)convertView
    57.findViewById(R.id.textView1);
    58holder.button=(Button)convertView.findViewById(R.id.button1);
    59convertView.setTag(holder);
    60}else{
    61holder=(ViewHolder)convertView.getTag();
    62}
    63holder.textView.setText(mContentList.get(position));
    64holder.button.setOnClickListener(mListener);
    65holder.button.setTag(position);
    66returnconvertView;
    67}
    68
    69publicclassViewHolder{
    70publicTextViewtextView;
    71publicButtonbutton;
    72}
    73
    74/**
    75*用于回调的抽象类
    76*@authorIvanXu
    77*2014-11-26
    78/
    79publicstaticabstractclassMyClickListenerimplementsOnClickListener{
    80/
    *
    81*基类的onClick方法
    82*/
    83@Override
    84publicvoidonClick(Viewv){
    85myOnClick((Integer)v.getTag(),v);
    86}
    87publicabstractvoidmyOnClick(intposition,Viewv);
    88}
    89}
    复制代码
    复制代码
    Activity类:

    复制代码
    复制代码
    1packagecom.ivan.listvieweventdemo;
    2
    3importjava.util.ArrayList;
    4importjava.util.List;
    5
    6importandroid.app.Activity;
    7importandroid.os.Bundle;
    8importandroid.view.Menu;
    9importandroid.view.View;
    10importandroid.widget.AdapterView;
    11importandroid.widget.AdapterView.OnItemClickListener;
    12importandroid.widget.ListView;
    13importandroid.widget.Toast;
    14
    15importcom.ivan.adapter.ContentAdapter;
    16importcom.ivan.adapter.ContentAdapter.MyClickListener;
    17importcom.ivan.listvieweventabstract.R;
    18
    19publicclassMainActivityextendsActivityimplementsOnItemClickListener{
    20
    21//模拟listview中加载的数据
    22privatestaticfinalString[]CONTENTS={"北京","上海","广州","深圳","苏州",
    23"南京","武汉","长沙","杭州"};
    24privateListcontentList;
    25privateListViewmListView;
    26
    27@Override
    28protectedvoidonCreate(BundlesavedInstanceState){
    29super.onCreate(savedInstanceState);
    30setContentView(R.layout.activity_main);
    31
    32init();
    33}
    34
    35privatevoidinit(){
    36mListView=(ListView)findViewById(R.id.listview);
    37contentList=newArrayList();
    38for(inti=0;i<CONTENTS.length;i++){
    39contentList.add(CONTENTS[i]);
    40}
    41//实例化ContentAdapter类,并传入实现类
    42mListView.setAdapter(newContentAdapter(this,contentList,mListener));
    43

    44mListView.setOnItemClickListener(this);
    45}
    46
    47@Override
    48publicbooleanonCreateOptionsMenu(Menumenu){
    49getMenuInflater().inflate(R.menu.main,menu);
    50returntrue;
    51}
    52
    53//响应item点击事件
    54@Override
    55publicvoidonItemClick(AdapterView<?>arg0,Viewv,intposition,longid){
    56Toast.makeText(this,"listview的item被点击了!,点击的位置是-->"+position,
    57Toast.LENGTH_SHORT).show();
    58}
    59
    60/**
    61*实现类,响应按钮点击事件
    62*/
    63privateMyClickListenermListener=newMyClickListener(){
    64@Override
    65publicvoidmyOnClick(intposition,Viewv){
    66Toast.makeText(
    67MainActivity.this,
    68"listview的内部的按钮被点击了!,位置是-->"+position+",内容是-->"
    69+contentList.get(position),Toast.LENGTH_SHORT)
    70.show();
    71}
    72};
    73}
    复制代码
    复制代码

    以下是布局文件

    复制代码
    复制代码
    12xmlns:tools="http://schemas.android.com/tools"
    3android:layout_width="match_parent"
    4android:layout_height="match_parent"
    5android:paddingBottom="@dimen/activity_vertical_margin"
    6android:paddingLeft="@dimen/activity_horizontal_margin"
    7android:paddingRight="@dimen/activity_horizontal_margin"
    8android:paddingTop="@dimen/activity_vertical_margin"
    9tools:context=".MainActivity">
    10
    1112android:id="@+id/listview"
    13android:layout_width="match_parent"
    14android:layout_height="match_parent">
    15
    16
    17
    复制代码
    复制代码
    复制代码
    复制代码
    <?xmlversion="1.0"encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="content"android:textColor="#ff0000"android:textSize="20sp"/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:focusable="false"android:text="Button"android:textColor="#ff0000"android:textSize="20sp"/>


    复制代码
    复制代码

    两种方式的区别在于,抽象类在Activity中实现的时候,只能定义一个成员变量来实现,不能由Activity直接实现,因为Java不支持多继承。而接口既可以由Activity直接实现,也可以由其成员变量来实现。

    2020-08-21 14:46:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载