乞丐版MVP之展示列表
阿新 • • 發佈:2018-12-09
列表檢視 Data是最裡面的展示資料
我的Bean類有兩個方法Root是外面的,Data是裡面的核心展示列表資料
public interface ListDataView {
void onSuccess(List<Data> result);
void onFailer(String msg);
}
列表模型 Root是最外面的驗證
public class ListDataModel { public static Root getListData() { String data = Utils.get("http://www.xieast.com/api/news/news.php"); Gson gson = new Gson(); Root root = gson.fromJson(data, Root.class); return root; } }
列表Persenter
public class ListDataPersenter { private ListDataView listDataView; public ListDataPersenter(ListDataView listDataView) { this.listDataView = listDataView; } public void getListData() { new Thread(new Runnable() { @Override public void run() { Root data = ListDataModel.getListData(); Message message = Message.obtain(); message.obj = data; handler.sendMessage(message); } }).start(); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Root data = (Root) msg.obj; listDataView.onSuccess(data.getData()); } }; }
相當於Activity 一定要implements ListDataView的兩個方法
public class HomePage extends Fragment implements ListDataView { private View view; private ListView mListview; private ListDataPersenter persenter; private List<Data> mList; private MyAdapter adapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.fragment_homepage, null); initView(view); return view; } private void initView(View view) { mListview = (ListView) view.findViewById(R.id.listview); mList = new ArrayList<>(); persenter = new ListDataPersenter(this); persenter.getListData(); adapter = new MyAdapter(mList,getActivity()); mListview.setAdapter(adapter); } @Override public void onSuccess(List<Data> result) { if (result!=null) { mList.clear(); mList.addAll(result); adapter.notifyDataSetChanged(); } } @Override public void onFailer(String msg) { Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show(); } }
介面卡
public class MyAdapter extends BaseAdapter {
private List<Data> mList;
private Context context;
public MyAdapter(List<Data> mList, Context context) {
this.mList = mList;
this.context = context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view==null) {
viewHolder = new ViewHolder();
view = View.inflate(context,R.layout.item_goods,null);
viewHolder.title = view.findViewById(R.id.text_title);
viewHolder.image = view.findViewById(R.id.image);
viewHolder.image1 = view.findViewById(R.id.image1);
viewHolder.image2 = view.findViewById(R.id.image2);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.title.setText(mList.get(i).getTitle());
Picasso.with(context).load(mList.get(i).getThumbnail_pic_s()).into(viewHolder.image);
Picasso.with(context).load(mList.get(i).getThumbnail_pic_s02()).into(viewHolder.image1);
Picasso.with(context).load(mList.get(i).getThumbnail_pic_s03()).into(viewHolder.image2);
return view;
}
class ViewHolder {
public TextView title;
public ImageView image;
public ImageView image1;
public ImageView image2;
}
}