Java泛型应用之打造Android中ListView和GridView万能适配器【CommonAdapter】--超简洁写法

在android中使用最多的就是ListView,GridView,用到这两个控件那么肯定要用到适配器,那就是定义一个类继承BaseAdapter,让后覆写它里面的getCount(),getView()等方法,下面来写一个demo。

1.我们来实现一个普通的ListView的Demo

  • 主布局文件:activity_main.xml


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kejiang.yuandl.fastdev.MainActivity">


* ListView的item布局:item.xml
```xml



 

  • ListView的适配器代码:MyAdapter.java
   
       /**
        * * Created by yuandl on 2016-10-14.
        * *listView的适配器
        */
       
       public class MyAdapter extends BaseAdapter {
           private Context context;
           private List datas;
       
           public MyAdapter(Context context, List datas) {
               this.context = context;
               this.datas = datas;
           }
       
           @Override
           public int getCount() {
               return datas == null ? 0 : datas.size();
           }
       
           @Override
           public String getItem(int position) {
               return datas.get(position);
           }
       
           @Override
           public long getItemId(int position) {
               return position;
           }
       
           @Override
           public View getView(int position, View convertView, ViewGroup parent) {
               ViewHolder viewHolder;
               if (convertView == null) {
                   convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
                   viewHolder = new ViewHolder();
                   viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
                   convertView.setTag(viewHolder);
               } else {
                   viewHolder = (ViewHolder) convertView.getTag();
               }
               String name = datas.get(position);
               viewHolder.textView.setText(name);
               return convertView;
           }
       
           private class ViewHolder {
               public TextView textView;
           }
       }

  • 使用:MainActivity.java
    
    public class MainActivity extends Activity {
        private WebView webView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            myAdapterTest();
    
        }
    
        /**
         * 普通适配器的方法
         */
        private void myAdapterTest() {
            setContentView(R.layout.activity_main);
            ListView listView= (ListView) findViewById(R.id.listview);
            List datas=new ArrayList<>();
            datas.add("普通适配器测试1");
            datas.add("普通适配器测试2");
            datas.add("普通适配器测试3");
            datas.add("普通适配器测试4");
            listView.setAdapter(new MyAdapter(context,datas));
        }
  }
  • 看效果图
Java泛型应用之打造Android中ListView和GridView万能适配器【CommonAdapter】--超简洁写法_第1张图片
运行效果

2.打造万能适配器第一步,万能ViewHolder--CommonViewHolder

 
import android.util.SparseArray;
import android.view.View;

/**
 * Created by yuandl on 2016-10-13.
 * 万能的ViewHolder
 *
 * @author
 */
public class CommonViewHolder {
    /**
     * @param view 所有缓存View的根View
     * @param id   缓存View的唯一标识
     * @return
     */
    public static  T get(View view, int id) {

        SparseArray viewHolder = (SparseArray) view.getTag();
        //如果根view没有用来缓存View的集合
        if (viewHolder == null) {
            viewHolder = new SparseArray();
            view.setTag(viewHolder);//创建集合和根View关联
        }
        View chidlView = viewHolder.get(id);//获取根View储存在集合中的孩纸
        if (chidlView == null) {//如果没有改孩纸
            //找到该孩纸
            chidlView = view.findViewById(id);
            viewHolder.put(id, chidlView);//保存到集合
        }
        return (T) chidlView;
    }
}

3.打造万能适配器的最后封装--CommonAdapter

 
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 yuandl on 2016-10-13.
 * 万能适配器
 */

public abstract class CommonAdapter extends BaseAdapter {
    private Context context;
    private List datas;
    private int layoutId;

    public CommonAdapter(Context context, List datas, int layoutId) {
        this.context = context;
        this.datas = datas;
        this.layoutId = layoutId;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layoutId, null);
        }
        T t = getItem(position);
        convertView(convertView, t);
        return convertView;
    }

    /**
     * 需要去实现的对item中的view的设置操作
     * @param item
     * @param t
     */
    protected abstract void convertView(View item, T t);

}

4.万能适配器CommonAdapter的超简洁用法

  • 使用代码
     public class MainActivity extends Activity {
        private WebView webView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            myAdapterTest();

        }

         /**
     * 万能适配器的方法
     */
    private void commonAdapterTest() {
        setContentView(R.layout.activity_main);
        ListView listView= (ListView) findViewById(R.id.listview);
        List datas=new ArrayList<>();
        datas.add("万能适配器测试1");
        datas.add("万能适配器测试2");
        datas.add("万能适配器测试3");
        datas.add("万能适配器测试4");
        listView.setAdapter(new CommonAdapter(context,datas,R.layout.item) {

            @Override
            protected void convertView(View item, String s) {
                TextView textView=  CommonViewHolder.get(item,R.id.textView);
                textView.setText(s);
            }
        });
    }

  }
  • 运行效果
Java泛型应用之打造Android中ListView和GridView万能适配器【CommonAdapter】--超简洁写法_第2张图片
这里写图片描述

你可能感兴趣的:(Java泛型应用之打造Android中ListView和GridView万能适配器【CommonAdapter】--超简洁写法)