主界面的基类
在使用时要依赖3个,下图右2,搜索butterknife(红框中的2个),recycler
public abstract class BaseAcyivity extends AppCompatActivity {
private Unbinder bind;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getlayoutId());
bind = ButterKnife.bind(this);
initdata();
setOnClick();
}
public abstract void initdata() ;
public abstract int getlayoutId();
public void setOnClick(){
}
@Override
protected void onDestroy() {
super.onDestroy();
bind.unbind();
}
}
APP的基类,承接上下文(记住APP这个类必须要在清单文件中注册)
记住APP这个类必须要在清单文件中注册
public class App extends Application{
public static App appContext;
@Override
public void onCreate() {
super.onCreate();
appContext = this;
}
}
适配器的基类(Recycler)
public abstract class AllBaseAdapter<T> extends RecyclerView.Adapter<AllBaseAdapter.MyViewHolder>{
private List<T> data;
private onItemClickListaner listaner;
public AllBaseAdapter(List<T> data) {
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//查找对应Item布局
View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final AllBaseAdapter.MyViewHolder holder, final int position) {
//设置数据
mset(holder,data.get(position));
//item设置的点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listaner!=null){
listaner.onItemClick(v,position);
}
}
});
}
//传过来的数据个数
@Override
public int getItemCount() {
return data==null?0:data.size();
}
public abstract int getLayoutId() ;
public class MyViewHolder extends RecyclerView.ViewHolder{
public View itemView;
public MyViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
}
//将ID的强转写在这里
protected <E extends View> E getViewById(@IdRes int viewId) {
return (E) itemView.findViewById(viewId);
}
//设置数据
public void setText(@IdRes int id,String context){
TextView textView= getViewById(id);
textView.setText(context);
}
public void setIoce(@IdRes int id, Drawable ioce){
ImageView imageView=getViewById(id);
imageView.setImageDrawable(ioce);
}
//。。。。。可以设置所有的控件,用时直接点
}
//viewHolder重写承接,setData代表数组中的东西
public abstract void mset(MyViewHolder holder,Object setData);
//自己定义一个点击监听的接口
public interface onItemClickListaner{
void onItemClick(View itemView,int position);
}
//设置监听,用传进来的listaner代替它的listaner
public void setOnItemClick(onItemClickListaner listaner){
this.listaner = listaner;
}
}
举个例子
继承适配器的类
public class MainAdapter extends AllBaseAdapter {
public MainAdapter(List data) {
super(data);
}
@Override
public int getLayoutId() {
return R.layout.item_main;
}
@Override
public void mset(MyViewHolder viewHolder, Object setData) {
//这里可以设置各种控件的数据:viewHolder. 点后面跟控件的方法名
viewHolder.setText(R.id.tv_item_recycler, (String) setData);
}
}
主界面对点击的使用
public class MainActivity extends BaseAcyivity {
@BindView(R.id.recycler_main)
RecyclerView recyclerMain;
private MainAdapter adapter;
@Override
public void initdata() {
List<String> datas=new ArrayList<>();
datas.add("草莓");datas.add("火龙果");datas.add("苹果");datas.add("木瓜");
datas.add("西瓜");datas.add("橘子");datas.add("香蕉");datas.add("火龙果");
datas.add("梨");datas.add("草莓");datas.add("火龙果");datas.add("苹果");
datas.add("木瓜");datas.add("西瓜");datas.add("橘子");datas.add("香蕉");
datas.add("火龙果");datas.add("梨");
RecyclerView.LayoutManager manager=new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
recyclerMain.setLayoutManager(manager);
adapter = new MainAdapter(datas);
recyclerMain.setAdapter(adapter);
//分割线
recyclerMain.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
}
@Override
public int getlayoutId() {
return R.layout.activity_main;
}
public void setOnClick(){
adapter.setOnItemClick(new AllBaseAdapter.onItemClickListaner() {
@Override
public void onItemClick(View itemView, int position) {
Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show();
}
});
}
}
布局自己写找到对应ID