1. 程式人生 > >MVP+RecyclerView請求資料

MVP+RecyclerView請求資料

1.CallBack介面
public interface CallBack {
void OnSuccess(List<JsonBean.ResultBean.DataBean> list);
void OnFailer(String error);
}

2.View層
public interface OverView {
void OnSuccess(List<JsonBean.ResultBean.DataBean> list);
void OnFailer(String error);
}

3.Model層
public class GainModel {
private String path=“

http://v.juhe.cn/toutiao/index?type=&key=83a69c67e9272f816e42450ef0eb50ee”;
public void show(final CallBack callBack){
new Okhttp().get(path).setInfor(new Okhttp.Infor() {
@Override
public void success(String s) {
Gson gson = new Gson();
JsonBean bean = gson.fromJson(s, JsonBean.class);
List<JsonBean.ResultBean.DataBean> data = bean.getResult().getData();
if (data!=null){
callBack.OnSuccess(data);
}else {
callBack.OnFailer(“獲取資料失敗”);
}
}
});
}
}

4.presenter層

public class LjPresenter {
private OverView overView;
private GainModel gainModel;

public LjPresenter(OverView overView) {
    this.overView = overView;
    gainModel = new GainModel();
}
public void show(){
    gainModel.show(new CallBack() {
        @Override
        public void OnSuccess(List<JsonBean.ResultBean.DataBean> list) {
            overView.OnSuccess(list);
        }

        @Override
        public void OnFailer(String error) {
            overView.OnFailer("失敗");
        }
    });
}

}

5.Activity
public class Main2Activity extends AppCompatActivity implements OverView {
private LjPresenter ljPresenter;
private ImageView imageView;
private TextView textView;
private RecyclerView recyclerView;
private Button jian;
private Button jia;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    initData();
    ljPresenter = new LjPresenter(this);
    ljPresenter.show();
}

private void initData() {
    Intent intent = getIntent();
    String pro = intent.getStringExtra("pro");
    String name = intent.getStringExtra("name");
    Glide.with(Main2Activity.this).load(pro).into(imageView);
    textView.setText(name);
}

private void initView() {
    imageView = findViewById(R.id.im21);
    textView = findViewById(R.id.text21);
    recyclerView = findViewById(R.id.recyclerview);
    jian = findViewById(R.id.jian);
    jia = findViewById(R.id.jia);
}


@Override
public void OnSuccess(final List<JsonBean.ResultBean.DataBean> list) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
    // LinearLayoutManager layoutManager =new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);
    final RecyclerAdapter adapter = new RecyclerAdapter(Main2Activity.this,list);
    recyclerView.setAdapter(adapter);
    adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            String thumbnail_pic_s03 = list.get(position).getThumbnail_pic_s();
            Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
            intent.putExtra("io",thumbnail_pic_s03);
            startActivity(intent);
        }
    });
    jian.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
           adapter.removedata(0);
        }
    });
    jia.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            adapter.add(1);
        }
    });
}

@Override
public void OnFailer(String error) {

}

}

6.adapter
public class RecyclerAdapter extends RecyclerView.Adapter {
private Context context;
private List<JsonBean.ResultBean.DataBean> list;

public RecyclerAdapter(Context context, List<JsonBean.ResultBean.DataBean> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewtype) {
    View view=null;
    RecyclerView.ViewHolder viewHolder =null;

            view = View.inflate(context,R.layout.rec3,null);
            viewHolder = new MyViewHolder3(view);


    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

        ((MyViewHolder3)viewHolder).textView31.setText(list.get(i).getTitle());
        Glide.with(context).load(list.get(i).getThumbnail_pic_s()).into(((MyViewHolder3) viewHolder).imageView31);


}

public void removedata(int position){
    if (list.size()==0||list==null){
        Toast.makeText(context.getApplicationContext(), "沒有資料了", Toast.LENGTH_SHORT).show();
    }else {
        list.remove(position);
        notifyItemRemoved(position);
    }

}

public void add(int position){
    list.add(position,list.get(0));
    notifyItemInserted(position);
}
@Override
public int getItemCount() {
    return list.size();
}

public interface OnItemClickListener{
    void onItemClick(View view, int position);
}
private OnItemClickListener onItemClickListener;

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}



private class MyViewHolder3 extends RecyclerView.ViewHolder{


    private final TextView textView31;
    private ImageView imageView31;

    public MyViewHolder3(@NonNull View itemView) {
        super(itemView);
        textView31 = itemView.findViewById(R.id.text31);
        imageView31 = itemView.findViewById(R.id.iamge31);
        //imageView32 = itemView.findViewById(R.id.iamge32);
        //imageView33 = itemView.findViewById(R.id.iamge33);
        imageView31 = itemView.findViewById(R.id.iamge31);
        ViewGroup.LayoutParams params = imageView31.getLayoutParams();
        //設定控制元件的寬高
        params.width=(1000)/3;
        params.height =(int)(+Math.random()*400);
        imageView31.setLayoutParams(params);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,getAdapterPosition());
            }
        });
    }
}

}