Android自定义通用适配器

BaseAdapter:getCount() getItem() getItemId() 基本都是固定的写法,每次都这样写,所以我们可以将他们写到父类中固定下来,以后不必再写;getView()中两种优化的方法和流程是固定的,我们也可以把这些优化的代码固定下来,只留下需要每次修改的部分让子类实现即可。
根据以往我们写过的适配器:
每次编写Adapter会变化的部分:1.ViewHolder装载具体的控件。2.加载数据。
1. 通用适配器MyBaseAdapter.java

package com.example.zhaoxin.commonbaseadapter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by zhaoxin on 17/11/23.
 * 自定义通用适配器
 */

public abstract class MyBaseAdapter<T, E extends MyBaseAdapter.ViewHolder> extends BaseAdapter{
    // 数据
    private List entityList;
    // 界面
    private int resourceId;
    // 上下文
    private Context context;

    public MyBaseAdapter(Context context, int resourceId, List entityList){
        this.context = context;
        this.resourceId = resourceId;
        this.entityList = entityList;
    }

    @Override
    public int getCount() {
        return entityList.size();
    }

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

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (null == view){
            // 新建视图
            view = LayoutInflater.from(context).inflate(resourceId, null);
            // 完成ViewHolder
            E holder = createViewHolder(view);
            // 保存ViewHolder
            view.setTag(holder);
        }
        // 获取ViewHolder
        E holder = (E)view.getTag();
        // 获取数据
        T entity = (T) getItem(position);
        // 绑定数据
        bindDataAndView(holder, entity);
        return view;
    }

    /**
     * 创建ViewHolder
     * @param convertView
     * @return 自己定义的ViewHolder
     */
    public abstract E createViewHolder(View convertView);

    /**
     * 绑定数据到界面
     * @param holder 自定义的viewHolder
     * @param data
     */
    public abstract void bindDataAndView(E holder, T data);

    /**
     * 子类viewholder必须继承
     */
    public static abstract class ViewHolder{

    }
}
  1. 自定义适配器继承通用适配器MyListViewAdapter.java
package com.example.zhaoxin.commonbaseadapter.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.example.zhaoxin.commonbaseadapter.R;
import com.example.zhaoxin.commonbaseadapter.entity.Book;

import java.util.List;

/**
 * Created by zhaoxin on 17/11/23.
 * ListView适配器
 */

public class MyListViewAdapter extends MyBaseAdapter<Book, MyListViewAdapter.BookViewHolder> {

    public MyListViewAdapter(Context context, int resourceId, List bookList) {
        super(context, resourceId, bookList);
    }

    @Override
    public BookViewHolder createViewHolder(View convertView) {
        BookViewHolder holder = new BookViewHolder();
        // 获取视图
        holder.tv_book_name = convertView.findViewById(R.id.tv_book_name);
        holder.tv_book_price = convertView.findViewById(R.id.tv_book_price);
        return holder;
    }

    @Override
    public void bindDataAndView(BookViewHolder holder, Book data) {
        // 为列表项设置数据
        holder.tv_book_name.setText(data.getBookName());
        holder.tv_book_price.setText(data.getBookPrice());
    }

    class BookViewHolder extends MyBaseAdapter.ViewHolder{
        // View
        TextView tv_book_name;
        TextView tv_book_price;
    }
}
  1. MainActivity.java
package com.example.zhaoxin.commonbaseadapter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.zhaoxin.commonbaseadapter.adapter.MyListViewAdapter;
import com.example.zhaoxin.commonbaseadapter.entity.Book;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private ListView listView;// View
    private List bookList;// 图书集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取listView组件
        listView = (ListView) findViewById(R.id.list);
        // 获取图书集合对象
        bookList = new ArrayList<>();
        // 循环添加集合数据
        for (int i = 0; i < 10; i++) {
            Book book = new Book("Android", "45");
            bookList.add(book);
        }
        // 为组件设置适配器展示数据
        listView.setAdapter(new MyListViewAdapter(this, R.layout.item_list, bookList));
    }
}
  1. 实体类 Book.java
package com.example.zhaoxin.commonbaseadapter.entity;

/**
 * Created by zhaoxin on 17/11/23.
 * 图书实体类
 */

public class Book {

    private String bookName;
    private String bookPrice;

    public Book(){}

    public Book(String bookName, String bookPrice) {
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }
}
  1. 主页面布局 activity_main.java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    ListView>
LinearLayout>
  1. 列表项布局 item_list.java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_book_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_book_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
LinearLayout>

你可能感兴趣的:(Android)