##用ViewHoleder优化
package com.example.mynewadapter;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.mynewadapter.bean.LeftType;
import com.example.mynewadapter.bean.RightType;
import com.example.mynewadapter.bean.TiemType;
import com.example.mynewadapter.bean.Type;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List list;
private Button button;
public static final int TimeType = 0;
public static final int LeftType = 1;
public static final int RightType = 2;
public MyAdapter(Context context, List<Type> list) {
this.context = context;
this.list = list;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//不需要直接new,先应该让他为空,然后进行判断
viewHolderTime viewHolderTime = null;
viewHolderLeft viewHolderLeft = null;
viewHolderRight viewHolderRight = null;
//用type接收来自接口的对象
int type=list.get(position).getType();
if (convertView == null) {
//如果为空,则应该先新建布局
//进行选择是加载的那个布局
switch (type){
case TimeType:
//用布局填充器完成布局的加载
viewHolderTime =new viewHolderTime();
convertView=LayoutInflater.from(context).inflate(R.layout.time_layout,null);
viewHolderTime.textView= (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(viewHolderTime);
break;
case LeftType:
viewHolderLeft =new viewHolderLeft();
convertView=LayoutInflater.from(context).inflate(R.layout.left_layout,null);
viewHolderLeft.imageView= (ImageView) convertView.findViewById(R.id.image_view);
viewHolderLeft.textView= (TextView) convertView.findViewById(R.id.text_view);
viewHolderLeft.button= (Button) convertView.findViewById(R.id.left_btn);
viewHolderLeft.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"点击左键",Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolderLeft);
break;
case RightType:
viewHolderRight=new viewHolderRight();
convertView=LayoutInflater.from(context).inflate(R.layout.right_layout,null);
viewHolderRight.button= (Button) convertView.findViewById(R.id.right_btn);
viewHolderRight.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"点击右键",Toast.LENGTH_SHORT).show();
}
});
viewHolderRight.textView= (TextView) convertView.findViewById(R.id.text_view1);
viewHolderRight.imageView= (ImageView) convertView.findViewById(R.id.image_view1);
convertView.setTag(viewHolderRight);
break;
}
}else{
switch (type){
case TimeType:
viewHolderTime= (viewHolderTime) convertView.getTag();
break;
case LeftType:
viewHolderLeft= (viewHolderLeft) convertView.getTag();
break;
case RightType:
viewHolderRight= (viewHolderRight) convertView.getTag();
break;
}
}
switch (type){
case TimeType:
TiemType timeType= (TiemType) list.get(position);
viewHolderTime.textView.setText(timeType.getTime());
break;
case LeftType:
LeftType leftType= (LeftType) list.get(position);
viewHolderLeft.imageView.setImageResource(leftType.getImage());
viewHolderLeft.textView.setText(leftType.getText());
break;
case RightType:
RightType rightType= (RightType) list.get(position);
viewHolderRight.textView.setText(rightType.getText());
viewHolderRight.imageView.setImageResource(rightType.getImage());
break;
}
return convertView;
}
class viewHolderTime {
private TextView textView;
}
class viewHolderLeft {
private ImageView imageView;
private TextView textView;
private Button button;
}
class viewHolderRight {
private TextView textView;
private ImageView imageView;
private Button button;
}
}
package com.example.mynewadapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.mynewadapter.bean.LeftType;
import com.example.mynewadapter.bean.RightType;
import com.example.mynewadapter.bean.TiemType;
import com.example.mynewadapter.bean.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private List mList;
private ListView mListView;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
mListView= (ListView) findViewById(R.id.list_view);
mList=new ArrayList<>();
mList.add(new TiemType("11:43"));
mList.add(new LeftType(R.mipmap.ic_launcher,"好好学习"));
mList.add(new RightType(R.mipmap.ic_launcher,"天天向上"));
mList.add(new LeftType(R.mipmap.ic_launcher,"好好学习"));
mList.add(new RightType(R.mipmap.ic_launcher,"天天向上"));
mList.add(new TiemType("11:45"));
mList.add(new LeftType(R.mipmap.ic_launcher,"好好学习"));
mList.add(new RightType(R.mipmap.ic_launcher,"天天向上"));
MyAdapter adapter=new MyAdapter(mContext,mList);
mListView.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}