本文重点是构建可复用adapter,以创建GridView为例,效果图:
先贴上GridView的布局activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPagerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"></GridView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
GridView中每一个item的布局
activity_gridview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imgtou"
android:layout_width="64dp"
android:layout_height="64dp"
android:baselineAlignBottom="true"
android:focusable="false"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#1D1D1C"
android:textSize="20sp"
android:focusable="false"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
为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<T> extends BaseAdapter {
//构建view的数据
private List<T> lists;
private Context mContext;
//item的view布局
private int layoutId;
public MulitAdapter(List<T> 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<T> 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<GridViewBean> 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<GridViewBean> mulitAdapter = new MulitAdapter<GridViewBean>(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<GridViewBean> 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重构就行