1.看一下布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rb_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="这里填写注释了"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java" />
<RadioButton
android:id="@+id/rb_c_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="@+id/rb_kotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />
<RadioButton
android:id="@+id/rb_react_native"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="React Native" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/tv_click_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="text_1" />
<Button
android:id="@+id/tv_click_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="text_2" />
<Button
android:id="@+id/tv_click_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="text_3" />
</LinearLayout>
<com.youth.banner.Banner
android:id="@+id/bv_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="广告栏" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/sl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="下拉刷新">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="列表控件" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
来看一下生成控件实例化代码的一些常见:
- 1.这部分是生成注释必要的东西:注释
- 2.想要生成点击事件,需要添加如下clickable
- 3.然后一些特殊控件,比如
- 1).com.youth.banner.Banner
- 2).RadioGroup和RadioButton
- 3). com.scwang.smartrefresh.layout.SmartRefreshLayout
- 4). android.support.v7.widget.RecyclerView等
根据常见的业务逻辑,生成常见的模板代码:
接下来看一下生成代码前:
/***
*create by heinigger 2018/12/19
*@function:营销页面
**/
public class YingXiaoActivity extends BaseActivityV2<IYingXiaoContract.View, YingXiaoPresenter>
implements IYingXiaoContract.View {
@Override
public int getLayoutId() {
return R.layout.activity_ying_xiao;
}
@Override
public void init() {
}
@Override
public YingXiaoPresenter createPresenter() {
return new YingXiaoPresenter(this);
}
@Override
public <T> ObservableTransformer<T, T> bindLifecycle() {
return this.bindUntilEvent(ActivityEvent.DESTROY);
}
@Override
public void newsCommissionOrders(NewsCommissionOrders mResponse) {
}
}
在layout文件上,Alter+Insert,选择Generate FindView:
生成代码后:
/***
*create by heinigger 2018/12/19
*@function:营销页面
**/
public class YingXiaoActivity extends BaseActivityV2<IYingXiaoContract.View, YingXiaoPresenter>
implements IYingXiaoContract.View, OnClickListener {
/**这是生成注释的取的第一个部分**/
private RadioButton mRbJava;
/**第二个部分**/
private RadioButton mRbcPlus;
/**Kotlin**/
private RadioButton mRbKotlin;
/**React Native**/
private RadioButton mRbReactNative;
/**这里填写注释了**/
private RadioGroup mRbGroup;
/**text_1**/
private Button mTvClickText1;
/**text_2**/
private Button mTvClickText2;
/**text_3**/
private Button mTvClickText3;
/**广告栏**/
private Banner mBvContent;
/**列表控件**/
private RecyclerView mRvContent;
/**下拉刷新**/
private SmartRefreshLayout mSlContent;
private int currPage = 1;
@Override
public int getLayoutId() {
return R.layout.activity_ying_xiao;
}
@Override
public void init() {
mRbJava = (RadioButton) findViewById(R.id.rb_java);
mRbcPlus = (RadioButton) findViewById(R.id.rb_c_plus);
mRbKotlin = (RadioButton) findViewById(R.id.rb_kotlin);
mRbReactNative = (RadioButton) findViewById(R.id.rb_react_native);
mRbGroup = (RadioGroup) findViewById(R.id.rb_group);
mTvClickText1 = (Button) findViewById(R.id.tv_click_text_1);
mTvClickText2 = (Button) findViewById(R.id.tv_click_text_2);
mTvClickText3 = (Button) findViewById(R.id.tv_click_text_3);
mBvContent = (Banner) findViewById(R.id.bv_content);
mRvContent = (RecyclerView) findViewById(R.id.rv_content);
mSlContent = (SmartRefreshLayout) findViewById(R.id.sl_content);
mTvClickText1.setOnClickListener(this);
mTvClickText2.setOnClickListener(this);
mTvClickText3.setOnClickListener(this);
mRbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb_java) {
} else if (checkedId == R.id.rb_c_plus) {
} else if (checkedId == R.id.rb_kotlin) {
} else if (checkedId == R.id.rb_react_native) {
}
}
});
mBvContent.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
mBvContent.setImageLoader(ImageLoaders.getIns());
mBvContent.setBannerAnimation(Transformer.Default);
mBvContent.setDelayTime(3000);
mBvContent.setIndicatorGravity(BannerConfig.CENTER);
mBvContent.start();
mRvContent.setLayoutManager(new LinearLayoutManager(this));
mSlContent.autoRefresh();
mSlContent.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
currPage++;
loadData();
}
@Override
public void onRefresh(RefreshLayout refreshlayout) {
currPage = 1;
loadData();
}
});
}
@Override
public YingXiaoPresenter createPresenter() {
return new YingXiaoPresenter(this);
}
@Override
public <T> ObservableTransformer<T, T> bindLifecycle() {
return this.bindUntilEvent(ActivityEvent.DESTROY);
}
@Override
public void newsCommissionOrders(NewsCommissionOrders mResponse) {
}
@Override
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.tv_click_text_1) {//text_1
} else if (viewId == R.id.tv_click_text_2) {//text_2
} else if (viewId == R.id.tv_click_text_3) {//text_3
}
}
private void loadData() {
HashMap mMaps = IRequestConfig.getIns().getHashMap();
mMaps.put("currPage", currPage);
if (currPage == 1) {
mSlContent.finishRefresh();
if (mAdapter == null) {
mAdapter = new CommonRecylerAdapter(this, mDatas);
mRvContent.setAdapter(mAdapter);
} else {
mAdapter.setDatas(mDatas);
}
} else {
mAdapter.addDatas(mDatas);
mSlContent.finishLoadmore();
}
}
}
看一下生成前和生成后的代码行数:
其实通过代码行数不能代表什么:
主要是,减少了,我们复制粘贴,以及对一些命名控件的遗忘
比如:
我们实例化一个控件,都是通过复制粘贴,一个View的id,然后再进行findViewById,然后自己些的代码,通常为了效率,也不是很规范,嗯,大概就是又慢又不规范。
现在通过插件生成的都是非常规范,加上注释,方便之后代码维护
更加使我们回归到业务中,回到技术的探索中,而不是把大部分时间做着机械而浪费时间的代码工作
二.Adapter适配器的封装和代码生成:
普通写的代码:
/**
* @author:heinigger 2018/11/24 11:01
* @function:
*/
public class CollectGoodsAdapter extends RecyclerView.Adapter<CollectGoodsAdapter.TaoBaoViewHolder> implements OnClickListener {
private Fragment mFragment;
private ArrayList<TaoBaoGoods> mGoods;
public CollectGoodsAdapter(Fragment mFragment, ArrayList<TaoBaoGoods> mGoods) {
this.mFragment = mFragment;
this.mGoods = mGoods;
}
@Override
public TaoBaoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mParentView = LayoutInflater.from(mFragment.getContext()).inflate(R.layout.item_lechuan_collect_goods, null);
return new TaoBaoViewHolder(mParentView);
}
@Override
public int getItemCount() {
return mGoods.size();
}
@Override
public void onBindViewHolder(TaoBaoViewHolder holder, int position) {
TaoBaoGoods mGood = mGoods.get(position);
holder.mTvGoodsName.setText(mGood.getGoods_name());
holder.mTvGoodsDesc.setText(mGood.getGoods_desc());
holder.mTvPrice.setText(String.format("¥%.2f", StringUtil.secureFloat(mGood.getGoods_price())));
holder.mTvMonthSale.setText(String.format("月销%d", mGood.getGoods_sales_num()));
ImageLoaders.getIns().displayCornerWithDefault(holder.mIvGood, mGood.getGoods_pic(), 4);
holder.mRlyParent.setTag(holder.mRlyParent.getId(), position);
}
@Override
public void onClick(View v) {
int viewId = v.getId();
int position = (int) v.getTag(v.getId());
TaoBaoGoods mGood = mGoods.get(position);
if (viewId == R.id.rly_parent) {
TaobaoDetailActivity.start(mFragment.getContext(), mGood);
}
}
class TaoBaoViewHolder extends RecyclerView.ViewHolder {
ImageView mIvGood;
/**商品名称*/
TextView mTvGoodsName;
/**商品描述*/
TextView mTvGoodsDesc;
/**¥%.2f*/
TextView mTvPrice;
/**月销%s*/
TextView mTvMonthSale;
RelativeLayout mRlyParent;
public TaoBaoViewHolder(View itemView) {
super(itemView);
mIvGood = (ImageView) itemView.findViewById(R.id.iv_good);
mTvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
mTvGoodsDesc = (TextView) itemView.findViewById(R.id.tv_goods_desc);
mTvPrice = (TextView) itemView.findViewById(R.id.tv_price);
mTvMonthSale = (TextView) itemView.findViewById(R.id.tv_month_sale);
mRlyParent = (RelativeLayout) itemView.findViewById(R.id.rly_parent);
}
}
}
1.一个简单的Adapter,用普通的方式,没有封装过的,有整整91行。而且得看个人习惯,有些可能不止这个代码量,并且,你还要写注释呀,你不写注释,以后也很难修改维护
再来看看,封装后的代码:
特别是:
当后面封装后的代码还可以大部分被直接生成时:
生成代码前:
生成代码后,复制粘贴一下,便是如下:
然后就剩下填充点击事件,和填充数据