1. 程式人生 > >mvp解析RecyclerView

mvp解析RecyclerView

//view層 public interface HttpView { void onSuccess(String result); void onFailer(String msg); void onGetData(List<HttpUser.DataBean> data); }

//model層 public class HttpModel { public void jie(String path, final UrlConnection.HttpCallBack callBack){ UrlConnection urlConnection = new UrlConnection(); urlConnection.jie(path, new UrlConnection.HttpCallBack() { @Override public void getData(String s) { Gson gson = new Gson(); HttpUser user = gson.fromJson(s, HttpUser.class); List<HttpUser.DataBean> data = user.getData(); callBack.setData(data); }

        @Override
        public void setData(List<HttpUser.DataBean> data) {

        }
    });
}

}

//presenter層 public class HttpPresenter { private HttpView mHttpView; private HttpModel mHttpModel; private List<HttpUser.DataBean> mdata;

public HttpPresenter(HttpView httpView) {
    mHttpView = httpView;
    mHttpModel = new HttpModel();
}

public void jie(String path){
    mHttpModel.jie(path, new UrlConnection.HttpCallBack() {
        @Override
        public void getData(String s) {
            if(s.equals("成功的返回")){
                mHttpView.onSuccess("成功的返回");
            }else {
                mHttpView.onFailer("返回失敗");
            }
        }

        @Override
        public void setData(List<HttpUser.DataBean> data) {
            mdata = data;
            mHttpView.onGetData(mdata);
        }
    });
}

}

//獲取網路 public class UrlConnection {

public UrlConnection jie(String path, final HttpCallBack callBack){
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .get()
            .url(path)
            .build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            int code = response.code();
            if(code==200){
                ResponseBody body = response.body();
                String string = body.string();
                callBack.getData(string);
            }
        }
    });
    return this;
}
public interface HttpCallBack{
    void getData(String s);
    void  setData(List<HttpUser.DataBean> data);
}

}

//介面卡 public class HttpAdapter extends RecyclerView.Adapter<HttpAdapter.MyViewHolder> {

private Context mContext;
private List<HttpUser.DataBean> list;
private DisplayImageOptions options;
private RecycleitemClick click;

public void setClick(RecycleitemClick click) {
    this.click = click;
}

public HttpAdapter(Context context, List<HttpUser.DataBean> list) {
    mContext = context;
    this.list = list;
    options = new DisplayImageOptions.Builder()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}

public interface RecycleitemClick{
    void onItemClick(View view,int position);
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item, viewGroup, false);
    MyViewHolder holder = new MyViewHolder(view,click);
    return holder;

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    HttpUser.DataBean bean = list.get(i);
    ImageLoader.getInstance().displayImage(""+bean.getThumbnail_pic_s(),myViewHolder.image,options);
    myViewHolder.text.setText(""+bean.getAuthor_name()+"/n"+bean.getTitle());
}



@Override
public int getItemCount() {
    return list==null?0:list.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView text;
    ImageView image;

    public MyViewHolder(@NonNull View itemView, final RecycleitemClick click) {
        super(itemView);
        text = itemView.findViewById(R.id.textView);
        image = itemView.findViewById(R.id.imageView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.onItemClick(v,getAdapterPosition());
            }
        });
    }
}

}

//ImageLoader public class MyImageLoader extends Application { @Override public void onCreate() { super.onCreate(); initData(); }

private void initData() {
    ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this)
            .build();
    ImageLoader.getInstance().init(build);
}

}

//主介面 public class MainActivity extends AppCompatActivity implements HttpView {

private RecyclerView recycle;
private List<HttpUser.DataBean> list = new ArrayList<>();
private HttpPresenter mPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recycle = findViewById(R.id.recycle);
    mPresenter = new HttpPresenter(this);
    mPresenter.jie(OkHttpUtil.GONG);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recycle.setLayoutManager(linearLayoutManager);
}

@Override
public void onSuccess(String result) {
    Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}

@Override
public void onFailer(String msg) {
    Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}

@Override
public void onGetData(final List<HttpUser.DataBean> data) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            HttpAdapter adapter = new HttpAdapter(getApplicationContext(),data);
            adapter.setClick(new HttpAdapter.RecycleitemClick() {
                @Override
                public void onItemClick(View view, int position) {
                    Toast.makeText(MainActivity.this,"第"+position+"條資料",Toast.LENGTH_SHORT).show();
                }
            });
            recycle.setAdapter(adapter);
        }
    });
}

}