给Listview里面Item里的每一个控件怎么设定点击事件
adpter用的是继承的baseadapter,给整个listview上的Item设置点击事件就用listview.setOnItemClickListener(listener)就可以,下面介绍如果给Item里面某一控件设置点击事件。方法是重写 Adapter。
ListView工作原理(针对下面代码):
1. 给ListView设置数据适配器,此处程序是自己重写的Adapter,创建Adapter 的时候主要做下面的工作:
(1)把ListView需要显示的数据传给Adapter
(2)把ListView显示Item的界面传给Adapter
(3)把上下文对象传给Adapter,主要用来得到LayoutInflater对象来得到Item界面
2. 给ListView设置当点击Item对象的时候执行的操作,此处要实现给Item中的各项也就是该程序的三个TextView设置监听器,也就是调用Adapter中的getView函数。
3. getView函数实现的操作是:第一要将Item要显示的数据显示,然后设置监听器,为监听器设置操作。
代码如下:
ListView中Item的布局文件
activity_my_goods_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id = "@+id/goodBarcode"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:paddingTop="10dip"
android:clickable="true"//设置可点击
/>
<TextView
android:id="@+id/goodName"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:paddingTop="10dip"
android:clickable="true"
/>
<TextView
android:id="@+id/goodProvider"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:paddingTop="10dip"
android:clickable="true"
/>
</LinearLayout>
ListView控件所在的布局文件
activity_my_goods.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:gravity="center"
android:orientation="vertical">
<LinearLayout //标题
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15px"
android:layout_marginLeft="5dip"
android:layout_marginTop="93dip">
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:text="商品条码"
android:paddingTop="10dip"
/>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:text="商品名称"
android:paddingTop="10dip"
/>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:text="供应商"
android:paddingTop="10dip"
/>
</LinearLayout>
<ScrollView
android:id="@+id/feedbacklayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="103dip"
android:paddingTop="20.0dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20px">
<ListView
android:id="@+id/goodsinfoListView"
android:layout_width="wrap_content"
android:layout_height="354dp"
android:layout_marginBottom="5.0dip"
android:layout_marginRight="5.0dip"
android:textColor="#000"
android:textSize="16.0dip">
</ListView>
</ScrollView>
</RelativeLayout>
重写Adapter类
MyGoodsAdapter.java
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
/*
* 我的商品ListView的adapter。
* */
public class MyGoodsAdapter extends BaseAdapter {
private List<Good> goods;//ListView显示的数据
private int resource;//显示列表项的Layout
private LayoutInflater inflater;//界面生成器
private Context context;
public MyGoodsAdapter(List<Good> goods,int resource,Context context){
this.goods = goods;
this.resource = resource;
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return goods.size();
}
@Override
public Object getItem(int arg0) {
return goods.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1 == null){
arg1 = inflater.inflate(resource, null);
}
final Good good = goods.get(arg0);
TextView goodBarcode =(TextView) arg1.findViewById(R.id.goodBarcode);
//设置ListView中的Item中的TextView
goodBarcode.setText(good.getGoodBarcode());
//为TextView设置监听器
goodBarcode.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(context, good.getGoodBarcode(), Toast.LENGTH_LONG).show();
}
});
TextView goodName =(TextView) arg1.findViewById(R.id.goodName);
goodName.setText(good.getGoodName());
goodName.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(context, good.getGoodName(), Toast.LENGTH_LONG).show();
}
});
TextView goodProvider =(TextView) arg1.findViewById(R.id.goodProvider);
goodProvider.setText(good.getGoodProvider());
goodProvider.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(context, good.getGoodProvider(), Toast.LENGTH_LONG).show();
}
});
return arg1;
}
}
Activity文件
MyGoodsActivity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyGoodsActivity extends Activity implements MeunOnClickListener{
private ListView goodsinfoListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_goods);
// 获得ListView句柄
goodsinfoListView = (ListView)findViewById(R.id.goodsinfoListView);
final MyGoodsAdapter mgs = new MyGoodsAdapter(getListData(),R.layout.activity_my_goods_listview,this);
goodsinfoListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//int position =Integer.parseInt(arg1.getTag().toString());
mgs.getView(arg2, arg1, goodsinfoListView);
}
});
goodsinfoListView.setAdapter(mgs);
}
/*ListView数据*/
private List<Good> getListData() {
List<Good> list = new ArrayList<Good>();
Good good = new Good("1111111","果冻","XXX区域代理商");
list.add(good);
Good good1 = new Good("2222222","营养快线","XXX区域代理商");
list.add(good1);
Good good2 = new Good("333333","豆干","XXX区域代理商");
list.add(good2);
return list;
}
}
模型类
Good.java
package net.aufo.apps.certclient.afterlogin.adpter;
public class Good {
private String goodBarcode;
private String goodName;
private String goodProvider;
public String getGoodBarcode() {
return goodBarcode;
}
public void setGoodBarcode(String goodBarcode) {
this.goodBarcode = goodBarcode;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public String getGoodProvider() {
return goodProvider;
}
public void setGoodProvider(String goodProvider) {
this.goodProvider = goodProvider;
}
public Good(String goodBarcode, String goodName, String goodProvider) {
this.goodBarcode = goodBarcode;
this.goodName = goodName;
this.goodProvider = goodProvider;
}
}
补充知识:
1. getView函数介绍
public abstract View getView (int position, View convertView, ViewGroup parent)
Since: API Level 1
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.
position | The position of the item within the adapter's data set of the item whose view we want. |
convertView | The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)). |
parent | The parent that this view will eventually be attached to |
A View corresponding to the data at the specified position.
position是指当前dataset的位置,通过getCount和getItem来使用。如果list向下滑动的话那么就是最低端的item的位置,如果是向上滑动的话那就是最上端的item的位置。conert是指可以重用的视图,即刚刚出队的视图。parent是list。
2. 获得ListView中点击的Item
在onClick(View v)方法里面用
int position =Integer.parseInt(v.getTag().toString())就可以得到你当前点击的是哪一个条目
3. Adapter内的onItemClick监听器四个arg参数
public void onItemClick (AdapterView<?> parent, View view, int position, long id )
parent 相当于listview Y适配器的一个指针,可以通过它来获得Y里装着的一切东西,再通俗点就是说告诉你,你点的是Y,不是X - -。
view 是你点b item的view的句柄,就是你可以用这个view,来获得b里的控件的id后操作控件
position 是b在Y适配器里的位置(生成listview时,适配器一个一个的做item,然后把他们按顺序排好队,在放到listview里,意思就是这个b是第position号做好的)
id 是b在listview Y里的第几行的位置(很明显是第2行),大部分时候position和id的值是一样的,如果需要的话,你可以自己加个log把position和id都弄出来在logcat里瞅瞅,看了之后心里才踏实。