页面的布局
主页面布局
左面的RecyclerView的布局
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical"
>
android:id="@+id/txt_meal"
android:textSize="12sp"
android:text="效果"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Mode层
主题类Bean
public class Data {
public int status;
public String message;
public List data;
@Override
public String toString() {
return "Data{" +
"status=" + status +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}
public class DataBean {
public int id;
public String name;
public int restaurant_id;
public String created_at;
public int __v;
public List spus;
//定义布尔的选中判断
public boolean isClick;
public int pos;
}
public class SkusBean {
public String description;
public String price;
public int id;
public String _id;
@Override
public String toString() {
return "SkusBean{" +
"description='" + description + '\'' +
", price='" + price + '\'' +
", id=" + id +
", _id='" + _id + '\'' +
'}';
}
}
model层中的方法类为了获得数据
public class Task implements ITask {
@Override
public void getRecyclerView(String url, OkCallBack okCallBack) {
Ok.getOk().doGet(url,okCallBack);
}
}
接口方法
public interface ITask {
/**
* 读取列表的方法
* 两个参数 1.请求的接口 2.请求成功失败的方法
* @param url
*/
public void getRecyclerView(String url,OkCallBack okCallBack);
}
由于效果需要使用封装okHttp 请求数据的方法 那就需要写一个简单的网络请求封装类
第一个是 明叫Ok的类方法
public class Ok{
private static Ok ok;
private static OkHttpClient okHttpClient;
private static Handler handler;
public static OkHttpClient getOkHttpClient() {
return okHttpClient;
}
public static Handler getHandler() {
return handler;
}
private Ok(){
okHttpClient = new OkHttpClient
//TODO 拦截器,打印日志
.Builder()
.build();
handler = new Handler();
}
/**
* 获取单例(双重检测)
*
* @return
*/
public static Ok getOk() {
if (ok == null){//第一重检测
synchronized (Ok.class){//加同步锁
if (ok==null){//第二重检测
ok = new Ok();
}
}
}
return ok;
}
/**
* GET方法
*/
public void doGet(String url, Callback callback){
//组装请求
Request request = new Request.Builder()
.url(url)
.build();
//初始化调用类
Call call = okHttpClient.newCall(request);
//加入请求队列,回调结果
call.enqueue(callback);
}
public void post(String url, Map map, Callback callback){
/**
* Body实体,传入参数
*/
FormBody.Builder builder = new FormBody.Builder();
for (String key: map.keySet()) {
builder.add(key,map.get(key));
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
Call call = getOkHttpClient().newCall(request);
call.enqueue(callback);
}
}
第二个是明叫OkCallBack 静态类的方法
public abstract class OkCallBack implements Callback{
/**
* 成功
* @param json
*/
public abstract void onUI(String json);
/**
* 失败
* @param json
*/
public abstract void failed(String json);
/**
* 响应失败
* 这个方法在网络请求线程执行,意味无法更新UI
*
* @param call
* @param e
*/
@Override
public void onFailure(Call call, final IOException e) {
Ok.getHandler().post(new Runnable() {
@Override
public void run() {
// 这就是主线程
failed(e.getMessage());
}
});
}
/**
* 响应成功
* 这个方法在网络请求线程执行,意味无法更新UI
*
* @param call
* @param response
* @throws IOException
*/
@Override
public void onResponse(Call call, Response response) throws IOException {
/**
* 网络请求的数据必须有一个变量接受,body方法只执行一次。
*/
final String json = response.body().string();
Ok.getHandler().post(new Runnable() {
@Override
public void run() {
//成功返回数据
onUI(json);
}
});
}
}
Present层主要起处理业务层
主要业务层 类Presenter
public class Presenter extends OkCallBack implements IPresenter{
/**
* 注入view层
*/
IView view;
/**
* 注入model层
*/
Task task;
public Presenter(MainActivity activity) {
view = activity;
task = new Task();
}
@Override
public void start() {
}
@Override
public void onUI(String json) {
Gson gson=new Gson();
Data data = gson.fromJson(json, Data.class);
view.ShowData(data.data);
}
@Override
public void failed(String msg) {
}
//获得图片
@Override
public void getData() {
/**
* 使用model层的方法
*/
task.getRecyclerView("http://39.108.3.12:3000/v1/food/32", this);
}
//页面销毁
@Override
public void onDestory() {
view = null;
}
}
p层接口 为了防止内存溢出 需要继承 BasePresenter基类
public interface IPresenter extends BasePresenter{
public void getData();
}
View层视图层
View的视图层中接口 IView 里面传的参数是 视图要显示的数据层
public interface IView {
void ShowData(List data);
}
定义的基类 BasePresenter
public interface BasePresenter {
/**
* 开始执行
*/
void start();
/**
* 销毁正在操作,防止内存泄漏
*/
void onDestory();
}
以下是主要操作 左边的页面的适配器 右边的适配器 主题页面的操作
1.左边的是适配器
public class LeftAdapter extends RecyclerView.Adapter{
private List data = new ArrayList<>();
//监听
private ItemClickListencer listener;
public LeftAdapter() {
}
public void addData(List data) {
this.data.addAll(data);
notifyDataSetChanged();
}
@NonNull
@Override
public LeftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.left_item,viewGroup,false);
return new LeftHolder(view);
}
@Override
public void onBindViewHolder(@NonNull LeftHolder leftHolder, int i) {
final DataBean dataBean = data.get(i);
//赋值
leftHolder.lefttext.setText(dataBean.name);
//根据选中状态更新条目背景
if (dataBean.isClick){
leftHolder.itemView.setBackgroundColor(Color.WHITE);
}else {
leftHolder.itemView.setBackgroundColor(Color.GRAY);
}
//条目单击事件
leftHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//单击事件
listener.onItemClick(dataBean);
//遍历集合
for (DataBean d : data) {
if (d.id == dataBean.id){
d.isClick = true;
}else {
d.isClick=false;
}
}
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
Log.i("TAG",data.size()+"");
return data.size();
}
//定义单击事件接口
public interface ItemClickListencer{
void onItemClick(DataBean data);
}
public void setListener(ItemClickListencer listener){
this.listener = listener;
}
}
2.右边适配器
public class RightAdapter extends RecyclerView.Adapter{
List list;
List spus= new ArrayList<>();
Context context;
private RecyclerView recyclerView;
public RightAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
public void addData(List spusList){
this.list.clear();
this.list.addAll(spusList);
notifyDataSetChanged();
}
@NonNull
@Override
public DescViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.left_item,viewGroup,false);
return new DescViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DescViewHolder descViewHolder, int i) {
final SpusBean temp=list.get(i);
descViewHolder.textView.setText(temp.name);
//单击事件 为了选择条目
descViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rightListener.onItemRightClick(temp);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
/**
* 静态内部类 Holder
*/
public static class DescViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public DescViewHolder(@NonNull View itemView) {
super(itemView);
textView=(TextView)itemView.findViewById(R.id.txt_meal);
}
}
//单击事件
private RightListener rightListener;
//单击的监听
public interface RightListener {
void onItemRightClick(SpusBean temp);
}
// public void onClick(View view){
// //根据RecyclerView获得当前View的位置
// int position = recyclerView.getChildAdapterPosition(view);
// //程序执行到此,会去执行具体实现的onItemClick()方法
// if (rightListener != null){
// rightListener.onItemRightClick(recyclerView,view,position,list.get(position));
// }
// }
public void setRightListener(RightListener rightListener) {
this.rightListener = rightListener;
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
}
@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
}
主体页面操作
public class MainActivity extends AppCompatActivity implements IView{
//注入P层
IPresenter present;
//左边
RecyclerView leftRecycler;
LeftAdapter leftAdapter;
//右边
RecyclerView rightRecycler;
RightAdapter rightAdapter;
List list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图
initview();
present = new Presenter(this);
present.getData();
}
//初始化页面
private void initview() {
//左边
leftRecycler = (RecyclerView) findViewById(R.id.left_recycle);
final LinearLayoutManager leftManager = new LinearLayoutManager(this);
leftRecycler.setLayoutManager(leftManager);
leftRecycler.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
leftAdapter = new LeftAdapter();
leftRecycler.setAdapter(leftAdapter);
//单击事件
leftAdapter.setListener(new LeftAdapter.ItemClickListencer() {
@Override
public void onItemClick(DataBean data) {
rightAdapter.addData(data.spus);
}
});
//右边
rightRecycler=(RecyclerView)findViewById(R.id.right_recycle);
rightAdapter=new RightAdapter(list,MainActivity.this);
final LinearLayoutManager rightManager = new LinearLayoutManager(this);
rightRecycler.setLayoutManager(rightManager);
rightRecycler.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
//单击事件
rightAdapter.setRightListener(new RightAdapter.RightListener() {
@Override
public void onItemRightClick(SpusBean temp) {
Intent intent = new Intent(MainActivity.this,ShopActivity.class);
startActivity(intent);
}
});
rightRecycler.setAdapter(rightAdapter);
}
@Override
public void ShowData(List data) {
leftAdapter.addData(data);
DataBean dataBean = data.get(0);
dataBean.isClick =true;//设置默认点击
rightAdapter.addData(dataBean.spus);
}
/**
* 销毁
*/
@Override
protected void onDestroy() {
super.onDestroy();
present.onDestory();//调用防止内存泄漏
}
}
网友评论