fresco+recyclerView展示+MVP
阿新 • • 發佈:2018-11-08
public class Contant { public static final String BAUS_URI = "http://www.zhaoapi.cn/"; }
public interface Api { @GET("home/getHome") Call<Bean> tohome(); @GET("product/getProductDetail") Call<ShowBean> Show(@Query("pid") String pid); }
public final Api api; public HttpUtils() { OkHttpClient httpClient = new OkHttpClient.Builder() .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Contant.BAUS_URI) .addConverterFactory(GsonConverterFactory.create()) .client(httpClient) .build(); api = retrofit.create(Api.class); } private static class GETHttpUtilsinfo{ private static HttpUtils httpUtils = new HttpUtils(); } public static HttpUtils getHttpUtilsimg() { return GETHttpUtilsinfo.httpUtils; }
public class MainModel { public void MainModel(final Main main) { Call<Bean> call = HttpUtils.getHttpUtilsimg().api.tohome(); call.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { List<Bean.DataBean.TuijianBean.ListBeanX> list = response.body().getData().getTuijian().getList(); main.success(list); } @Override public void onFailure(Call<Bean> call, Throwable t) { } }); } public interface Main{ void success(List<Bean.DataBean.TuijianBean.ListBeanX> list); } }
public class BasePresenter < V extends IBaseView> { private V v; public void attacthView(V v){ this.v = v; } public void getDataView(){ this.v = null; } public V getView(){ return v; } }
public class MainPresenter extends BasePresenter<IMainView> { private final MainModel model; public MainPresenter() { model = new MainModel(); } public void getdata(){ model.MainModel(new MainModel.Main() { @Override public void success(List<Bean.DataBean.TuijianBean.ListBeanX> list) { getView().onSuccess(list); } }); } }
public class MainActivity extends AppCompatActivity implements IMainView { @BindView(R.id.recycler_view) RecyclerView recyclerView; private MainPresenter mainPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mainPresenter = new MainPresenter(); mainPresenter.attacthView(this); mainPresenter.getdata(); } @Override public void onSuccess(final List<Bean.DataBean.TuijianBean.ListBeanX> list) { StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(manager); SpacesItemDecoration decoration = new SpacesItemDecoration(50); recyclerView.addItemDecoration(decoration); MyAdapter adapter = new MyAdapter(MainActivity.this, list); recyclerView.setAdapter(adapter); } @Override public void onError() { } }
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.holder> { private Context context; private List<Bean.DataBean.TuijianBean.ListBeanX> list; public MyAdapter(Context context, List<Bean.DataBean.TuijianBean.ListBeanX> list) { this.context = context; this.list = list; } @NonNull @Override public MyAdapter.holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.bu_layout, parent, false); holder holder = new holder(view); return holder; } @Override public void onBindViewHolder(@NonNull MyAdapter.holder holder, final int position) { Uri uri = Uri.parse(list.get(position).getImages().split("\\|")[0]); holder.sim.setImageURI(uri); holder.text.setText(list.get(position).getPrice()+"元"); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pid = list.get(position).getPid(); Intent intent = new Intent(context,ShowActivity.class); intent.putExtra("pid",String.valueOf(pid)); context.startActivity(intent); } }); } @Override public int getItemCount() { return list.size(); } class holder extends RecyclerView.ViewHolder { private final SimpleDraweeView sim; private final TextView text; public holder(View itemView) { super(itemView); sim = itemView.findViewById(R.id.sim_view); text = itemView.findViewById(R.id.text_view); } } private OnItemClickListener onItemClickListener; public interface OnItemClickListener{ void onItemClick(int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } }
public class ShowModel { public void ShowModel(String pid,final Show show) { Call<ShowBean> call = HttpUtils.getHttpUtilsimg().api.Show(pid); call.enqueue(new Callback<ShowBean>() { @Override public void onResponse(Call<ShowBean> call, Response<ShowBean> response) { ShowBean.DataBean data = response.body().getData(); show.onsuccess(data); } @Override public void onFailure(Call<ShowBean> call, Throwable t) { } }); } public interface Show{ void onsuccess(ShowBean.DataBean data); } }
public class Showpresenter extends BasePresenter<ShowView> { private final ShowModel model; public Showpresenter() { model = new ShowModel(); } public void getda(String pid){ model.ShowModel(pid, new ShowModel.Show() { @Override public void onsuccess(ShowBean.DataBean data) { getView().onSuccess(data); } }); } }
public class ShowActivity extends Activity implements ShowView { @BindView(R.id.sim_view) SimpleDraweeView simView; @BindView(R.id.text_view) TextView textView; @BindView(R.id.text_name) TextView textName; private Showpresenter showpresenter; private String pid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); ButterKnife.bind(this); pid = getIntent().getStringExtra("pid"); showpresenter = new Showpresenter(); showpresenter.attacthView(this); showpresenter.getda(this.pid); } @Override public void onSuccess(ShowBean.DataBean data) { Uri uri = Uri.parse(data.getImages().split("\\|")[0]); simView.setImageURI(uri); textName.setText(data.getTitle()); textView.setText(data.getPrice() + "¥"); } @Override public void onerror() { } }