适配器模式

简介: 适配器模式定义:将一个类的接口转换为客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。来自《head first设计模式》解释:其实适配器就像是手机的数据线,它可以将原本接口不同的电脑和手机连接起来并实现数据的互通,也像是手机的充电器可以将原本很大的电压转换为可以为手机充电的电压。

适配器模式

定义:将一个类的接口转换为客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。

img_2a0f67caf6eaad0122f663fe7c4a1608.png
来自《head first设计模式》

解释:其实适配器就像是手机的数据线,它可以将原本接口不同的电脑和手机连接起来并实现数据的互通,也像是手机的充电器可以将原本很大的电压转换为可以为手机充电的电压。

案例:在安卓中应用此模式的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,效果展示
img_92f23b416839d28506f0746848a8e75a.gif
效果展示
目录
相关文章
|
1月前
|
Windows
|
4月前
|
设计模式 C++
结构型 适配器模式
结构型 适配器模式
30 0
|
4月前
|
设计模式
【适配器模式】—— 每天一点小知识
【适配器模式】—— 每天一点小知识
|
12月前
|
Java API
适配器模式的运用
适配器模式的运用
67 0
|
设计模式 数据库 C++
2023-6-15-第六式适配器模式
2023-6-15-第六式适配器模式
84 0
|
设计模式 前端开发
关于适配器模式我所知道的
关于适配器模式我所知道的
43 0
|
Java 程序员 API
结构型模式-适配器模式
结构型模式-适配器模式
82 0
|
设计模式
我学会了,适配器模式
适配器模式属于结构型模式,这个类型的设计模式总结出了 类、对象组合后的经典结构,将类、对象的结构和使用解耦了,花式的去借用对象。
92 0
我学会了,适配器模式
|
设计模式 Java
【玩转23种Java设计模式】结构型模式篇:适配器模式
适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。适配器模式属于结构型模式,主要分为三类:类适配器模式、对象适配器模式、接口适配器模式。
【玩转23种Java设计模式】结构型模式篇:适配器模式
|
设计模式 Java Spring
适配器模式详解与应用
引用一个最经典的例子,我们自己家里的电压都是220V的,而早期的手机充电时所用的都是5V的电压。要想让手机在220V的家庭电压下充电,就需要一个电源适配器。在编程中,也经常会出现一个类程序无法直接去使用,而需要通过适当变换的行为抽象出来的设计模式就是适配器模式。