适配器模式的定义
适配器(Adapter)模式就是将某一些对象转成我们需要的适配的对象。分为两种为类适配和对象适配。
小案例
一个项目在1.0版本只有人民币支付的方式,后在1.1版本加上了美金支付。现在使用适配器模式来适配美金支付。
类适配写法:
其实是通过继承来实现
1.0版本
public class RMBAdaptee {
private float mRmb;
public RMBAdaptee(float rmb){
this.mRmb = rmb;
}
public float getRmb() {
return mRmb;
}
}
public void main(){
RMBAdaptee mRMBAdaptee = new RMBAdaptee(560);
float rmb = mRMBAdaptee.getRmb();
System.out.println("需要支付人民币:"+rmb);
}
1.1版本
public interface UsdTarget {
float getUsd();
}
public class Adapter extends RMBAdaptee implements UsdTarget {
public Adapter(float rmb) {
super(rmb);
}
@Override
public float getUsd() {
return getRmb()/7.0f;
}
}
public void main() {
RMBAdaptee mRMBAdaptee = new RMBAdaptee(560);
float rmb = mRMBAdaptee.getRmb();
System.out.println("需要支付人民币:" + rmb);
// 第二个版本要去兼容美元 ,采用适配器模式,
Adapter adapter = new Adapter(560);
float usd = adapter.getUsd();
System.out.println("需要支付美元" + usd);
}
对象适配写法
通过设置参数的写法
1.0版本
public class RMBAdaptee {
private float mRmb;
public RMBAdaptee(float rmb){
this.mRmb = rmb;
}
public float getRmb() {
return mRmb;
}
}
public void main(){
RMBAdaptee mRMBAdaptee = new RMBAdaptee(560);
float rmb = mRMBAdaptee.getRmb();
System.out.println("需要支付人民币:"+rmb);
}
1.1版本
public interface UsdTarget {
float getUsd();
}
public class Adapter implements UsdTarget {
private RMBAdaptee mRMBAdaptee;
public Adapter(RMBAdaptee mRMBAdaptee) {
this.mRMBAdaptee = mRMBAdaptee;
}
@Override
public float getUsd() {
return mRMBAdaptee.getRmb()/7.0f;
}
}
public void main(){
com.haiming.adapterpattern.simple2.RMBAdaptee mRMBAdaptee = new RMBAdaptee(560);
float rmb = mRMBAdaptee.getRmb();
System.out.println("人民币:"+rmb);
// 第二个版本要去兼容美元 ,采用适配器模式,
Adapter adapter = new Adapter(mRMBAdaptee);
float usd = adapter.getUsd();
System.out.println("人名币换美元"+usd);
}
RecyclerView的Adapte
这里写一个简易版的recyclerView说明适配器模式
public class SimpleRecyclerView extends ScrollView {
private LinearLayout mContainer;
private Adapter mAdapter;
public SimpleRecyclerView(Context context) {
this(context, null);
}
public SimpleRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SimpleRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContainer = new LinearLayout(context);
mContainer.setOrientation(LinearLayout.VERTICAL);
addView(mContainer, 0);
}
@Override
public void addView(View child) {
mContainer.addView(child);
}
/**
* 对象适配
*
* @param recyclerViewAdapter
*/
public void setAdapter(Adapter recyclerViewAdapter) {
this.mAdapter = recyclerViewAdapter;
int count = mAdapter.getCount();
for (int i = 0; i < count; i++) {
View childView = mAdapter.onCreateViewHolder(i, mContainer);
addView(childView);
}
}
public interface Adapter{
int getCount();
View onCreateViewHolder(int position, ViewGroup parent);
}
}
/**
* 具体的适配器
* 把数据集合适配成View--对象适配
*/
public class RecyclerViewAdapter implements SimpleRecyclerView.Adapter {
public List<String> mItems;
private Context mContext;
public RecyclerViewAdapter(Context context, List<String> mItems) {
this.mItems = mItems;
this.mContext = context;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public View onCreateViewHolder(int position, ViewGroup parent) {
TextView itemView = (TextView) LayoutInflater.from(mContext)
.inflate(R.layout.item_layout,parent,false);
itemView.setText(mItems.get(position));
return itemView;
}
}
public class MainActivity extends AppCompatActivity {
private SimpleRecyclerView mListView;
private List<String> mDatas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.simple_list_view);
for (int i=0;i<100;i++){
mDatas.add(""+i);
}
mListView.setAdapter(new RecyclerViewAdapter(this,mDatas));
}
}
网友评论