适配器模式
定义:将一个类的接口转换为客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。
解释:其实适配器就像是手机的数据线,它可以将原本接口不同的电脑和手机连接起来并实现数据的互通,也像是手机的充电器可以将原本很大的电压转换为可以为手机充电的电压。
案例:在安卓中应用此模式的ListView,GridView,RecyclerView想必大家都不会陌生,这次就通过手写一个简单的ListView来对适配器模式进行解释。
1,定义一个接口
public interface MyAdapter {
//获取item数量
int getCount();
//获取itemView
View getView(int position, ViewGroup parent);
}
2,创建MyListView继承自LinearLayout
public class MyListView extends LinearLayout {
private MyAdapter myAdapter;
private LinearLayout linearLayout;
public MyListView(Context context) {
this(context,null);
}
public MyListView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//这里将MyListView设置为填充父元素
LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(layoutParams);
//内部View纵向排列
setOrientation(VERTICAL);
//添加ScrollView防止item数量过多导致显示不完全
ScrollView scrollView=new ScrollView(context);
scrollView.setLayoutParams(layoutParams);
//在ScrollView中新增一个LinearLayout用于存放itemView,内部View排列方向依然是纵向
linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(VERTICAL);
//添加scrollView到MyListView
scrollView.addView(linearLayout);
//添加linearLayout到scrollView
addView(scrollView);
}
//为MyListView设置适配器
public void setAdapter(MyAdapter myAdapter){
this.myAdapter=myAdapter;
if(myAdapter.getCount()>0){
for (int x=0;x<myAdapter.getCount();x++){
//根据适配器返回的返回的item数量添加itemView
linearLayout.addView(myAdapter.getView(x,linearLayout));
}
}
}
}
3,Activity布局文件展示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.shipeiqi.MainActivity">
<com.example.shipeiqi.view.MyListView
android:id="@+id/myListview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.shipeiqi.view.MyListView>
</LinearLayout>
4,item布局文件展示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp"
android:clickable="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="50dp"
android:id="@+id/img"
android:layout_centerVertical="true"
android:layout_height="50dp" />
<TextView
android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_toRightOf="@id/img"
android:layout_marginLeft="20dp"
android:text="标题"
android:layout_centerVertical="true"
android:layout_height="wrap_content" />
</RelativeLayout>
5,适配器创建
public class MAdapter implements MyAdapter{
private Context mContex;
private ArrayList<String> datas;
public MAdapter(Context mContex, ArrayList<String> datas) {
this.mContex = mContex;
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public View getView(int position, ViewGroup parent) {
View inflate = View.inflate(mContex, R.layout.item_view, null);
ImageView imageView=inflate.findViewById(R.id.img);
TextView textView=inflate.findViewById(R.id.tv);
textView.setText(datas.get(position));
imageView.setBackgroundResource(R.mipmap.ic_launcher);
return inflate;
}
}
6,在Activity中为MyListView添加Adapter
public class MainActivity extends AppCompatActivity {
private MyListView myListView;
private ArrayList<String> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView=findViewById(R.id.myListview);
initDatas();
}
private void initDatas() {
datas=new ArrayList<>();
for (int x=0;x<15;x++){
datas.add("我是标题"+x);
}
myListView.setAdapter(new MAdapter(this,datas));
}
}
7,效果展示