MVP模式框架的搭建

先分三个包 分别是 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;
    }
}
}

猜你喜欢

转载自blog.csdn.net/qq_43143981/article/details/83661172