1.main.xml
2.list.xml
3.MainActivity代码
package com.haitun.baseadaptertest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.os.Bundle;import android.app.Activity;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.Button;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity { private ListView listView=null; private List> listMap=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(ListView)findViewById(R.id.listView); listMap=new ArrayList >(); HashMap map=null; String[] userName={"user1","user2","user3"}; String[] listText={"HelloAPPPPPPfabNifadg migm afagaegegagege1","HelloAPPPPPPfabNifadg migm afagaegegagege2","HelloAPPPPPPfabNifadg migm afagaegegagege3"}; for(int i=0;i (); map.put("userName",userName[i]); map.put("text",listText[i]); listMap.add(map); } MyAdapter myAdapter=new MyAdapter(this);// myAdapter.notifyDataSetChanged(); listView.setAdapter(myAdapter); } public final class ViewHolder{ public TextView tv; public Button mButton; public LinearLayout linearLayout; public TextView userText; } private ViewHolder holder=null; class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater=null; public MyAdapter(Context context){ this.mInflater=LayoutInflater.from(context); } private List holderList=new ArrayList (); @Override public int getCount() { // TODO Auto-generated method stub return listMap.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ holder=new ViewHolder(); holderList.add(position, holder); //放倒队列中进行缓存 convertView=mInflater.inflate(R.layout.list,null); holder.tv=(TextView)convertView.findViewById(R.id.userName); holder.mButton=(Button)convertView.findViewById(R.id.mButton); holder.linearLayout=(LinearLayout)convertView.findViewById(R.id.info); holder.userText=(TextView)convertView.findViewById(R.id.tv); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } //数据和控件的匹配 holder.tv.setText((String)listMap.get(position).get("userName")); holder.userText.setText((String)listMap.get(position).get("text")); Listener listener=new Listener(position); //自定义监听器,把position参数传进去 holder.mButton.setOnClickListener(listener); return convertView; } private boolean bchange=false; private ViewHolder viewHolder=null; //用户缓存用户的ViewHolder对象 class Listener implements OnClickListener{ private int position; public Listener(int position){ this.position=position; } @Override public void onClick(View v) { // TODO Auto-generated method stub viewHolder=(ViewHolder)holderList.get(position+1); if(bchange){ bchange=false; viewHolder.linearLayout.setVisibility(LinearLayout.GONE); }else{ bchange=true; viewHolder.linearLayout.setVisibility(LinearLayout.VISIBLE); } listView.setSelection(position); //显示ListView的最后一行 } } } }