可复用adapter,构建任意ListView、GridView

本文重点是构建可复用adapter,以创建GridView为例,效果图:

可复用adapter,构建任意ListView、GridView_第1张图片

先贴上GridView的布局activity_gridview.xml



    
        
    

GridView中每一个item的布局 activity_gridview_item.xml



    
        
        

    

为item布局创建实体

package com.xhx.testapplication.bean;

public class GridViewBean {
    private int imgId;
    private String name;

    public GridViewBean(int imgId,String name){
        this.imgId = imgId;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
}

开始创建adapter,合理利用泛型,让adapter不用关注创建View的数据内容,View的具体布局

package com.xhx.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;


public abstract class MulitAdapter extends BaseAdapter {
    //构建view的数据
    private List lists;
    private Context mContext;
    //item的view布局
    private int layoutId;

    public MulitAdapter(List lists, Context context,int layoutId){
        this.lists = lists;
        this.mContext = context;
        this.layoutId = layoutId;
    }
    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if(null == convertView)
        {
            convertView = LayoutInflater.from(mContext).inflate(layoutId,parent,false);
            viewHolder = new ViewHolder();
            bindView(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        setView(viewHolder,lists,position);
        return convertView;
    }

    //view的绑定抽取出来
    public abstract ViewHolder bindView(ViewHolder viewHolder,View converView);

    //view的赋值抽取出来
    public abstract void setView(ViewHolder viewHolder,List lists,int position);

    //添加一条记录
    public void addItem(T data){
        if(null == lists){
            lists = new ArrayList<>();
        }
        lists.add(data);
        notifyDataSetChanged();
    }
}

ViewHolder.java

package com.xhx.testapplication.adapter;

import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder{
    public ImageView imageView;
    public TextView txt_name;
}

最后是Activity

package com.xhx.testapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;

import com.xhx.testapplication.adapter.MulitAdapter;
import com.xhx.testapplication.adapter.ViewHolder;
import com.xhx.testapplication.bean.GridViewBean;

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

public class MulitAdapterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        //构建view的数据集
        List lists = new ArrayList<>();
        lists.add(new GridViewBean(R.drawable.app_small,"企鹅"));
        lists.add(new GridViewBean(R.drawable.icon_small,"图标"));
        lists.add(new GridViewBean(R.drawable.dlg_back,"图片"));
        lists.add(new GridViewBean(R.drawable.app_small,"企鹅"));

        MulitAdapter mulitAdapter = new MulitAdapter(lists,this,R.layout.activity_gridview_item){
            @Override
            public ViewHolder bindView(ViewHolder viewHolder, View converView) {
                viewHolder.imageView = converView.findViewById(R.id.imgtou);
                viewHolder.txt_name = converView.findViewById(R.id.name);
                return viewHolder;
            }

            @Override
            public void setView(ViewHolder viewHolder, List lists, int position) {
                viewHolder.imageView.setBackgroundResource(lists.get(position).getImgId());
                viewHolder.txt_name.setText(lists.get(position).getName());
            }
        };

        GridView gridView = findViewById(R.id.gridView);
        gridView.setAdapter(mulitAdapter);
    }
}

以上是一个GridView的示例,adapter不需要关注创建的view到底长什么样,是GridView?ListView?

创建ListView的布局只需要修改bean,ViewHolder和activity就行。

对于item的增删改只是对list的操作,记得调用notifyDataSetChanged方法对item重构就行

你可能感兴趣的:(android学习)