### MainActivity
public class MainActivity extends AppCompatActivity { //声明 @BindView(R.id.main_name) TextView mainName; @BindView(R.id.simple) SimpleDraweeView simple; //获取图片 String path = "http://img2.imgtn.bdimg.com/it/u=559724675,422552619&fm=27&gp=0.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注解和绑定 ButterKnife.bind(this); //注册 EventBus.getDefault().register(this); //Uri uri = Uri.parse(path); //simple.setImageURI(uri); //simple图片点击事件 simple.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,ErActivity.class); startActivity(intent); } }); } @Subscribe(threadMode = ThreadMode.MainThread, sticky = true) public void getMsg(MyEvent msg){ mainName.setText(msg.getName()); String img = msg.getImg(); simple.setImageURI(img); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
### App页面
public class MyAppliaction extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
### ITouView 接口
public interface ITouView extends BaseView<TouPresenter>{ void success(List<Tou> list); void failed(String string);
### TouAdapter
public class TouAdpater extends RecyclerView.Adapter<TouHolder>{ private List<Tou> list; private Context context; //TODO 声明自定义接口listener ItemListener listener; //实例化 public TouAdpater() { this.list = new ArrayList<>(); } public void setListener(ItemListener listener) { this.listener = listener; } //TODO 自定义set方法 public void addData(List<Tou> data){ list.addAll(data); notifyDataSetChanged(); } @NonNull @Override public TouHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new TouHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tou_item,parent,false)); } @Override public void onBindViewHolder(@NonNull TouHolder holder, int position) { final Tou tou = list.get(position); holder.tou_who.setText(tou.who); String url = tou.url; Uri uri = Uri.parse(url); holder.tou_url.setImageURI(uri); //自定义设置监听 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.ItemClick(tou); } }); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } //TODO 自定义条目点击事件的接口 public interface ItemListener{ void ItemClick(Tou tou); }
### ErActivity
public class ErActivity extends AppCompatActivity implements ITouView{ //注入P层 TouPresenter presenter; //声明 RecyclerView recyclerView; TouAdpater adpater; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_er); presenter = new TouPresenter(this); presenter.getListP(); //获取资源ID recyclerView = findViewById(R.id.recycler_view); //设置布局 recyclerView.setLayoutManager(new LinearLayoutManager(this)); //添加自条目之间的分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL)); //创建适配器 adpater = new TouAdpater(); recyclerView.setAdapter(adpater); //TODO 自定义自条目的监听事件 adpater.setListener(new TouAdpater.ItemListener() { @Override public void ItemClick(Tou tou) { //跳转 Intent intent = new Intent(ErActivity.this,MainActivity.class); //拿取数据 String who = tou.who; String img = tou.url; //发送 EventBus.getDefault().postSticky(new MyEvent(who,img)); startActivity(intent); finish(); } }); } @Override public void success(List<Tou> list) { adpater.addData(list); } @Override public void failed(String string) { } @Override public void setP(TouPresenter P) { this.presenter = P; }
### OkCallBack
public abstract class OkCallBack implements Callback{ //成功 public abstract void success(String json); //失败 public abstract void failed(String json); @Override public void onFailure(Call call, final IOException e) { Okutils.getHandler().post(new Runnable() { @Override public void run() { failed(e.getMessage()); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String json = response.body().string(); Okutils.getHandler().post(new Runnable() { @Override public void run() { success(json); } });