Xml文件
<ListView
android:id="@+id/left_listview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ListView
android:id="@+id/right_listview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
left_xml---------------------------------------------
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_item_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:text="商品"
android:textSize="20sp">
right_xml------------------------------------------------------
<TextView
android:id="@+id/right_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="20sp" />
<com.example.administrator.fenlei.view.MyGridView
android:id="@+id/right_gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"></com.example.administrator.fenlei.view.MyGridView>
gv_xlm---------------------------------------------------
<ImageView
android:id="@+id/gv_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/gv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称"
android:textSize="15sp" />
MainActivity主页面------------------------------------------------
public class MainActivity extends AppCompatActivity implements IMainView{
private static final String TAG = "MainActivity";
private ListView left_listview;
private IPresenterImpl iPresenter;
private List<LeftBean.DataBean> list;
private String cid;
private ListView right_listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDate();
initView();
}
private void initView() {
left_listview = findViewById(R.id.left_listview);
right_listview = findViewById(R.id.right_listview);
left_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.this.cid=list.get(position).getCid()+"";
iPresenter.RightDate(new IModeImpl(),MainActivity.this);
}
});
}
private void initDate() {
iPresenter = new IPresenterImpl();
iPresenter.LeftDate(new IModeImpl(),this);
}
@Override
public void showLeft(List<LeftBean.DataBean> list) {
this.list=list;
Log.d(TAG, "showLeft: "+list.size());
LeftAdapter leftAdapter=new LeftAdapter(list,MainActivity.this);
left_listview.setAdapter(leftAdapter);
int cid = list.get(0).getCid();
MainActivity.this.cid=cid+"";
iPresenter.RightDate(new IModeImpl(),MainActivity.this);
}
@Override
public void showRight(List<RightBean.DataBean> list) {
Log.d(TAG, "showRight: "+list.size());
RightAdapter rightAdapter=new RightAdapter(list,this);
right_listview.setAdapter(rightAdapter);
}
@Override
public String getCid() {
return this.cid;
}
}
p层----------------------------------------------------------------
public class IPresenterImpl implements IPresenter {
private static final String TAG = "IPresenterImpl";
@Override
public void LeftDate(IModel iModel, final IMainView iMainView) {
Map<String,String> map=new HashMap<>();
iModel.GetLeft(Httpfig.left_url, map, new GetLeftListener() {
@Override
public void getLeftSuccess(String json) {
Gson gson=new Gson();
LeftBean leftBean = gson.fromJson(json, LeftBean.class);
List<LeftBean.DataBean> data = leftBean.getData();
iMainView.showLeft(data);
}
@Override
public void getLeftError(String error) {
Log.d(TAG, "getLeftError: 失败");
}
});
}
@Override
public void RightDate(IModel iModel, final IMainView iMainView) {
Map<String,String> map=new HashMap<>();
map.put("cid",iMainView.getCid());
iModel.GetRight(Httpfig.right_url, map, new GetRightListener() {
@Override
public void getRightSuccess(String json) {
Gson gson=new Gson();
RightBean rightBean = gson.fromJson(json, RightBean.class);
List<RightBean.DataBean> data = rightBean.getData();
iMainView.showRight(data);
}
@Override
public void getRightError(String error) {
Log.d(TAG, "getLeftError: 失败");
}
});
}
}
model层------------------------------------------------------------
public class IModeImpl implements IModel{
@Override
public void GetLeft(String url, Map<String, String> map, final GetLeftListener getLeftListener) {
OKHttpUtils okHttpUtils=new OKHttpUtils();
okHttpUtils.okPost(url,map);
okHttpUtils.setOnLoginListener(new OKHttpUtils.LoginListener() {
@Override
public void loginSuccess(String json) {
getLeftListener.getLeftSuccess(json);
}
@Override
public void loginError(String error) {
getLeftListener.getLeftError(error);
}
});
}
@Override
public void GetRight(String url, Map<String, String> map, final GetRightListener getRightListener) {
OKHttpUtils okHttpUtils=new OKHttpUtils();
okHttpUtils.okPost(url,map);
okHttpUtils.setOnLoginListener(new OKHttpUtils.LoginListener() {
@Override
public void loginSuccess(String json) {
getRightListener.getRightSuccess(json);
}
@Override
public void loginError(String error) {
getRightListener.getRightError(error);
}
});
}
-------------------------------------------------------------------
MyGridView---------------------
public class MyGridView extends GridView {
public MyGridView(Context context, List<RightBean.DataBean.ListBean> list) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, i);
}
}