Customized implementation of baseadapter for Android (general adapter 3)

In the previous article, we said that if there are many controls in SetData, we still need to write a lot of code in this method. In order to reduce the convenience of development, we will optimize it again on this basis. The implementation principle is as follows. Each time we find the control in SetData, and then setXXX (), we can put this implementation into the viewholder, and write a chain method in the viewholder to help us realize the function (I won't write the code of viewhandler class again. Here's the code: Android's custom implementation baseadapter) (universal adapter I)), the chain method is as follows:

public ViewHolder setText(int viewId,String data){
  TextView tv = getView(viewId);
  tv.setText(data);
  return this;
 }

Through this method, we can set the corresponding content well. We only need to pass the ID of the control to be set, and then pass the corresponding data to achieve the effect of setting the text. Let's take a look at the code in myadapter.java

MyAdapter.java

/**
* 上一篇中的MyAdapter
*/
public class MyAdapter extends MyBaseAdapter {
 public MyAdapter(List<Student> data) {
  super(data);
 }
 @Override
 public void setData(ViewHolder holder,Student t) {
  TextView tvName = holder.getView(R.id.mTv1);
  tvName.setText(t.getName());
  TextView tvSex = holder.getView(R.id.mTv2);
  tvSex.setText(t.getSex());
 }
}

/**
* 优化后的MyAdapter
*/
public class MyAdapter extends MyBaseAdapter {
 public MyAdapter(List<Student> data) {
  super(data);
 }
 @Override
 public void setData(ViewHolder holder,Student t) {
  holder.setText(R.id.mTv1,t.getName()).setText(R.id.mTv2,t.getSex());
 }
}

OK, compared with the implementation in SetData, we can replace the above implementation with only one code. Is this more convenient? In this way, our extension is more convenient. If we don't just set the text content, but set the picture through the picture control? Very simple, we just need to add the method we want to implement in viewholder, such as setting pictures. We can add the following code:

public ViewHolder setImageResource(int viewId,int resId){
  ImageView img = getView(viewId);
  img.setImageResource(resId);
  return this;
 }
 public ViewHolder setImageBitmap(int viewId,Bitmap bm){
  ImageView img = getView(viewId);
  img.setImageBitmap(bm);
  return this;
 }

After adding these two methods, it is easy to set the picture. Just pass the corresponding parameters in SetData

@Override
 public void setData(ViewHolder holder,t.getSex());
  holder.setImageResource(R.id.img1,资源id).setImageBitmap(R.id.img2,bm);
 }

Well, a general adapter has been completed, and the overall framework and code have been implemented. I will synthesize all the codes and write them below for your convenience. They are mainactivity.java (the main interface class, which is responsible for transmitting the data of parameter setting listview), myadapter.java (custom adapter), mybaseadapter.java (General adapter class) Viewholder.java (general holding class object) and entity class student.java

MainActivity.java

public class MainActivity extends AppCompatActivity{

 private List<Student> data;
 private ListView mList;
 MyAdapter adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  getData();
  mList = (ListView) findViewById(R.id.mList);
  adapter = new MyAdapter(data);
  mList.setAdapter(adapter);
 }

 private void getData() {
  data = new ArrayList<>();
  Student stu = null;
  for (int i = 0; i < 20; i++) {
   stu = new Student();
   stu.setName("姓名" + i);
   stu.setSex(i % 2 == 0 ? "男" : "女");
   data.add(stu);
  }
 }

}

MyAdapter.java

public class MyAdapter extends MyBaseAdapter<Student> {

 public MyAdapter(List<Student> data) {
  super(data);
 }

 @Override
 public void setData(ViewHolder holder,t.getSex());

 }

}

MyBaseAdapter.java

public abstract class MyBaseAdapter<T> extends BaseAdapter {
 protected List<T> data;
 public MyBaseAdapter(List<T> data){
  this.data = data;
 }
 @Override
 public int getCount() {
  return data == null ? 0 : data.size();
 }

 @Override
 public Object getItem(int position) {
  return data.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position,View convertView,ViewGroup parent) {
  ViewHolder holder = ViewHolder.getHolder(convertView,parent,position,R.layout.list_item);
  setData(holder,data.get(position));
  return holder.getConvertView();
 }
 public abstract void setData(ViewHolder holder,T t);
}

ViewHolder.java

public class ViewHolder {
 private int position;
 private SparseArray<View> array;
 private View convertView;
 private Context context;

 private ViewHolder(ViewGroup parent,int position,int layout) {
  this.position = position;
  this.context = parent.getContext();
  convertView = LayoutInflater.from(parent.getContext()).inflate(layout,null);
  convertView.setTag(this);
  array = new SparseArray<>();
 }

 public static ViewHolder getHolder(View convertView,ViewGroup parent,int layout) {
  if (convertView == null) {
   return new ViewHolder(parent,layout);
  } else {
   ViewHolder holder = (ViewHolder) convertView.getTag();
   holder.position = position;
   return holder;
  }
 }

 public <T extends View> T getView(int viewId) {
  View view = array.get(viewId);
  if (view == null) {
   view = convertView.findViewById(viewId);
   array.put(viewId,view);
  }
  return (T) view;
 }

 public View getConvertView() {
  return convertView;
 }

 public ViewHolder setText(int viewId,String data) {
  TextView tv = getView(viewId);
  tv.setText(data);
  return this;
 }

 public ViewHolder setImageResource(int viewId,int resId) {
  ImageView img = getView(viewId);
  img.setImageResource(resId);
  return this;
 }

 public ViewHolder setImageBitmap(int viewId,Bitmap bm) {
  ImageView img = getView(viewId);
  img.setImageBitmap(bm);
  return this;
 }
}

Student.java

public class Student {
 private String name;
 private String sex;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>