MVP模式框架的搭建
阿新 • • 發佈:2018-12-19
先分三個包 分別是 model、persenter、view
先在view包下面新建一個介面
public interface IDelegate {
//初始化資料
void initData();
//建立佈局的方法
void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle);
//獲取view的方法
View getRootView();
//獲取上下文
void initContext(Context context);
}
繼續寫一個實現類 實現上面寫的介面
public abstract class AppDelegater implements IDelegate { private View view; //初始化資料的方法 @Override public void initData() {} //載入佈局 @Override public void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) { view = inflater.inflate(getLayoutid(), viewGroup, false); } //返回view @Override public View getRootView() { return view; } //傳遞layout的方法 public abstract int getLayoutid(); //封裝一個存viewid的集合 private SparseArray<View> views = new SparseArray<>(); //定義一個找id的方法 public <T extends View> T get(int id){ //從集合裡取這個id 如果是空 就重新找 View view1 = views.get(id); //判斷是否為空 是的話重新找id再存入 if(view==null){ view1=view.findViewById(id); views.put(id,view1); } return this.view.findViewById(id); } //定義一個 點選事件的方法 public void setClick (View.OnClickListener listener,int...ids){ //判斷是否為空 為空直接結束 if(ids==null){ return; } //遍歷ids for(int id:ids){ //設定監聽 get(id).setOnClickListener(listener); } } }
去persenter包下寫Activity和Fragent的父類
Baseactivity
public abstract class BaseActivityPresenter<T extends AppDelegater> extends AppCompatActivity { public T delegate; public abstract Class<T> getClassDelegater(); public BaseActivityPresenter() { try { //引入 delegate = getClassDelegater().newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); delegate.create(getLayoutInflater(), null, savedInstanceState); setContentView(delegate.getRootView()); delegate.initContext(this); delegate.initData(); } }
BaseFragment
public abstract class BaseFragmentPresenter<T extends AppDelegater> extends Fragment { private T delegate; public abstract Class<T> getClassDelegater(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { //引入 delegate = getClassDelegater().newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (java.lang.InstantiationException e) { e.printStackTrace(); } } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { delegate.create(inflater,container,savedInstanceState); return delegate.getRootView(); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); delegate.initContext(getActivity()); delegate.initData(); } }
主頁面Activity
public class MainActivity extends BaseActivityPresenter<MainActivityPersenter> {
@Override
public Class<MainActivityPersenter> getClassDelegater() {
return MainActivityPersenter.class;
}
}
主頁面Activity的persenter 這裡寫邏輯
public class MainActivityPersenter extends AppDelegater implements View.OnClickListener {
private ListView listview;
private XListView xlistview;
private Context context;
private GoodsListLeftAdapter leftAdapter;
private GoodsListLeftAdapter2 leftAdapter2;
private GoodsListRightAdapter rightAdapter;
private GoodList goodList;
String url = "http://ftp6252741.host709.zhujiwu.me/goods/goods_all.txt";
private GoodsListRightTopAdapterAdapter topAdapterAdapter;
private GoodsAdapter goodsAdapter;
@Override
public int getLayoutid() {
return R.layout.activity_main;
}
@Override
public void initData() {
super.initData();
//找id 直接呼叫接口裡的方法
listview = (ListView) get(R.id.listview);
xlistview = (XListView) get(R.id.xlistview);
//設定點選事件
setClick(this, R.id.te_sousuo);
//設定左邊的介面卡
leftAdapter = new GoodsListLeftAdapter(context);
leftAdapter2 = new GoodsListLeftAdapter2(context);
goodsAdapter = new GoodsAdapter(context);
listview.setAdapter(leftAdapter2);
//設定右邊的介面卡
rightAdapter = new GoodsListRightAdapter(context);
xlistview.setAdapter(rightAdapter);
xlistview.setPullLoadEnable(true);
xlistview.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
dohttp2(url);
}
@Override
public void onLoadMore() {
dohttp2(url);
}
});
//請求網路資料
sohttp();
dohttp2(url);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
url = goodList.getDatas().get(i).getLink();
dohttp2(url);
}
});
//六張圖片的介面卡
View view = View.inflate(context, R.layout.goods_list_right_top, null);
GridView gridview = (GridView) view.findViewById(R.id.grid_view);
/*topAdapterAdapter = new GoodsListRightTopAdapterAdapter(context);*/
gridview.setAdapter(goodsAdapter);
xlistview.addHeaderView(view);
}
//獲取右邊商品
private void dohttp2(String url) {
new HttpHpler().get(url).result(new HttpHpler.HttpLintenter() {
@Override
public void success(String data) {
GoodAll goodAll = new Gson().fromJson(data, GoodAll.class);
goodsAdapter.setData(goodAll.getDatas());
rightAdapter.setList(goodAll.getGooslist());
/*topAdapterAdapter.setList(goodAll.getDatas());*/
xlistview.stopRefresh();
xlistview.stopLoadMore();
}
});
}
@Override
public void initContext(Context context) {
this.context = context;
}
//獲取嘴邊
private void sohttp() {
String url = "http://ftp6252741.host709.zhujiwu.me/goods/goods_list.txt";
new HttpHpler().get(url).result(new HttpHpler.HttpLintenter() {
@Override
public void success(String data) {
goodList = new Gson().fromJson(data, GoodList.class);
leftAdapter2.setList(goodList.getDatas());
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.te_sousuo:
//搜尋頁面 這裡要帶上上下文
context.startActivity(new Intent(context, SousuoActivity.class));
break;
}
}
//萬能介面卡
private class GoodsAdapter extends CommonAdapter<GoodAll.DatasBean> {
public GoodsAdapter(Context mContext) {
super(mContext);
}
@Override
public void convert(ViewHoler holer, GoodAll.DatasBean datasBean) {
holer.setUrlImage(R.id.image_top, datasBean.getPic());
}
@Override
public int getLayoutId() {
return R.layout.goods_list_right_top_adapter;
}
}
}