在android中,大家都知道列表的点击事件 setOnItemClickListener()方法,,可以实现对列表项的操作,但是如果你想实现列表项的点击事件,又想实现内部按钮的的点击事件应该怎么办?
那么就需要接口回调。
接口回调按我理解的意思是指:接口变量引用了某个界面类的上下文对象,接口变量引用接口里的方法,调用的实现方法体是在对应的哪个界面类使用该接口实现的方法体。
interface clickinview
{
public void click_one(View view);
public void click_two(View view);
public void click_icon(View view);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:id="@+id/click_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/click_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="第一个记事本"/>
<TextView
android:id="@+id/click_txt_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="第二个记事本"/>
LinearLayout>
3个控件 一个ImageView 2个TextView
适配器
package com.example.administrator.csdn_internal_click;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2018/6/8 0008.
*/
public class Myadapter extends BaseAdapter {
Context context;
int layout;
List list;
clickinview clickinview;
public Myadapter(Context context, int layout, List list, clickinview clickinview) {
this.context = context;
this.layout = layout;
this.list = list;
this.clickinview=clickinview;
}
interface clickinview
{
public void click_one(View view);
public void click_two(View view);
public void click_icon(View view);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(layout,null);
ImageView imageView=convertView.findViewById(R.id.click_icon);
TextView txt1=convertView.findViewById(R.id.click_txt);
TextView txt2=convertView.findViewById(R.id.click_txt_two);
imageView.setImageResource(list.get(position).getIcon());
imageView.setTag(position);
txt1.setText(list.get(position).getTxt1());
txt1.setTag(position);
txt2.setText(list.get(position).getTxt2());
txt2.setTag(position);
txt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickinview.click_one(v);
}
});
txt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickinview.click_two(v);
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickinview.click_icon(v);
}
});
return convertView;
}
}
package com.example.administrator.csdn_internal_click;
/**
* Created by Administrator on 2018/6/8 0008.
*/
public class unit_object {
private int icon;
private String txt1;
private String txt2;
public unit_object(int icon, String txt1, String txt2) {
this.icon = icon;
this.txt1 = txt1;
this.txt2 = txt2;
}
public unit_object() {
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTxt1() {
return txt1;
}
public void setTxt1(String txt1) {
this.txt1 = txt1;
}
public String getTxt2() {
return txt2;
}
public void setTxt2(String txt2) {
this.txt2 = txt2;
}
}
主界面
package com.example.administrator.csdn_internal_click;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Myadapter.clickinview{
protected ImageView imageView;
protected TextView txt_one,txt_two;
protected ListView listView;
protected List list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
list=getData();
Myadapter myadapter=new Myadapter(MainActivity.this,R.layout.unit_layout,list,MainActivity.this);
listView.setAdapter(myadapter);
setListern();
}
private void setListern() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"你点击的是:第"+position+"项item",Toast.LENGTH_SHORT).show();
}
});
}
private void initview() {
listView=findViewById(R.id.listview);
imageView=findViewById(R.id.click_icon);
txt_one=findViewById(R.id.click_txt);
txt_two=findViewById(R.id.click_txt_two);
}
private List getData()
{ List lists=new ArrayList<>();
unit_object object=new unit_object(R.drawable.ic_launcher_background,"我是文本1","我是文本2");
lists.add(object);
return lists;
}
@Override
public void click_one(View view) {
int values= (int) view.getTag();
unit_object object= list.get(values);
Toast.makeText(MainActivity.this,"你点击的是"+object.getTxt1(),Toast.LENGTH_SHORT).show();
}
@Override
public void click_two(View view) {
int values= (int) view.getTag();
unit_object object=list.get(values);
Toast.makeText(MainActivity.this,"你点击的是"+object.getTxt2(),Toast.LENGTH_SHORT).show();
}
@Override
public void click_icon(View view) {
int values= (int) view.getTag();
unit_object object=list.get(values);
Toast.makeText(MainActivity.this,"你点击的是图片",Toast.LENGTH_SHORT).show();
}
}
这样就可以实现点击主item和item的内部控件就会实现不同的效果。
作者:谭健
原文地址: [点击这里] (https://blog.csdn.net/weixin_39028072/article/details/80629017)