Contract
//V层 public interface Zuo_View_interface{ //数据刷新 public void showData(String message); public void show_RightData(String message); public void show_GoodData(String message2); } //P层 public interface Zuo_Presenter_interface<Zuo_View_interface>{ //绑定 public void attahView(Zuo_View_interface zuo_view_interface); //解绑 public void deathView(Zuo_View_interface zuo_view_interface); //M层交互 public void requestData(); public void request_RightData(String id); public void request_GoodData(String id); } //M层 public interface Zuo_Model_interface{ //网络请求数据--左侧 public void getJson(Callback_Zuo callback_zuo); //接口回调 public interface Callback_Zuo{ //数据回传 public void responseData(String message); } //网络请求数据--右侧 public void getRightJson(String id, Callback_Right callback_right); //接口回调 public interface Callback_Right{ //数据回传 public void response_RightData(String message); } //网络请求数据--商品 public void getGoodJson(String id, Callback_Good callback_good); //接口回调 public interface Callback_Good{ //数据回传 public void response_GoodData(String message2); } }
presenter
public class Presenter_Zuo implements Contract_Zuo.Zuo_Presenter_interface<Contract_Zuo.Zuo_View_interface> { Contract_Zuo.Zuo_View_interface zuo_view_interface; private Model_Zuo model_zuo; private SoftReference<Model_Zuo> reference; @Override public void attahView(Contract_Zuo.Zuo_View_interface zuo_view_interface) { this.zuo_view_interface=zuo_view_interface; model_zuo = new Model_Zuo(); reference = new SoftReference<>( model_zuo ); } @Override public void deathView(Contract_Zuo.Zuo_View_interface zuo_view_interface) { reference.clear(); } @Override public void requestData() { model_zuo.getJson( new Contract_Zuo.Zuo_Model_interface.Callback_Zuo() { @Override public void responseData(String message) { zuo_view_interface.showData(message); } } ); } @Override public void request_RightData(String id) { model_zuo.getRightJson( id,new Contract_Zuo.Zuo_Model_interface.Callback_Right() { @Override public void response_RightData(String message1) { zuo_view_interface.show_RightData(message1); } } ); } @Override public void request_GoodData(String id1) { model_zuo.getGoodJson( id1, new Contract_Zuo.Zuo_Model_interface.Callback_Good() { @Override public void response_GoodData(String message2) { zuo_view_interface.show_GoodData(message2); } } ); } }
model
public class Model_Zuo implements Contract_Zuo.Zuo_Model_interface { @Override public void getJson(final Callback_Zuo callback_zuo) { OkHttpUtil.getGetJson( Constant.ZUOCE_URL, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String message = response.body().string(); callback_zuo.responseData(message); Log.i( "zjm",message ); } } ); } @Override public void getRightJson(String id, final Callback_Right callback_right) { OkHttpUtil.getGetJson( Constant.YOUCE_URL+"?firstCategoryId="+id, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String message1 = response.body().string(); callback_right.response_RightData(message1); } } ); } @Override public void getGoodJson(String id1, final Callback_Good callback_good) { OkHttpUtil.getGetJson( Constant.GOOD_URL + "?categoryId=" + id1 + "&page=" + 1 + "&count=" + 5, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String message2 = response.body().string(); callback_good.response_GoodData(message2); } } ); } }
MainActivity
//创建fragment shouFragment = new ShouFragment(); mineFragment = new MineFragment(); transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.frame, shouFragment); transaction.add(R.id.frame, mineFragment); //show..hide transaction.show(shouFragment).hide(mineFragment).commit(); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //创建对象 FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); switch (checkedId){ case R.id.shou: transaction.show(shouFragment).hide(mineFragment); break; case R.id.mine: transaction.show(mineFragment).hide(shouFragment); break; } transaction.commit(); } }); }
shouFragment
public class ShouFragment extends Fragment implements Contract_Zuo.Zuo_View_interface { private Presenter_Zuo presenter_zuo; private RecyclerView rv_left; private RecyclerView rv_right; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_shou, container, false); //新建P层 rv_left = view.findViewById(R.id.rv_left); rv_right = view.findViewById(R.id.rv_right); presenter_zuo = new Presenter_Zuo(); presenter_zuo.attahView(this); presenter_zuo.requestData();//请求接口--左侧数据 if (Build.VERSION.SDK_INT >= 23) { String[] mPermissionList = new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS, Manifest.permission.CAMERA}; ActivityCompat.requestPermissions(getActivity(), mPermissionList, 123); } return view; } @Override public void showData(final String message) { getActivity().runOnUiThread( new Runnable() { @Override public void run() { //Toast.makeText( MainActivity.this, ""+message, Toast.LENGTH_SHORT ).show(); Gson gson = new Gson(); Bean_Zuo bean_zuo = gson.fromJson( message, Bean_Zuo.class ); final List<Bean_Zuo.ResultBean> list_zuo = bean_zuo.getResult(); Adapter_Zuo adapter_zuo = new Adapter_Zuo( R.layout.item_left, list_zuo ); LinearLayoutManager linearLayoutManager = new LinearLayoutManager( getActivity(), LinearLayoutManager.VERTICAL, false ); //rv_left.setLayoutManager(linearLayoutManager); rv_left.setLayoutManager( linearLayoutManager ); rv_left.setAdapter( adapter_zuo ); //适配器点击事件 adapter_zuo.setOnItemClickListener( new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { long itemId = adapter.getItemId( position );//下标 String id = list_zuo.get( (int) itemId ).getId();//集合的id //Toast.makeText( MainActivity.this, ""+id, Toast.LENGTH_SHORT ).show(); presenter_zuo.request_RightData(id);//请求接口--右侧数据 } } ); } } ); } @Override public void show_RightData(final String message) { getActivity().runOnUiThread( new Runnable() { @Override public void run() { //Toast.makeText( MainActivity.this, ""+message1, Toast.LENGTH_SHORT ).show(); Gson gson = new Gson(); Bean_You bean_you = gson.fromJson( message, Bean_You.class ); final List<Bean_You.ResultBean> list_you = bean_you.getResult(); Adapter_You adapter_you = new Adapter_You( R.layout.item_right, list_you ); LinearLayoutManager linearLayoutManager = new LinearLayoutManager( getActivity(), LinearLayoutManager.VERTICAL, false ); rv_right.setLayoutManager( linearLayoutManager ); rv_right.setAdapter( adapter_you ); } } ); } @Override public void show_GoodData(String message) { getActivity().runOnUiThread( new Runnable() { @Override public void run() { //Toast.makeText( MainActivity.this, ""+message2, Toast.LENGTH_SHORT ).show(); /*Gson gson = new Gson(); Bean_Good bean_good = gson.fromJson( message2, Bean_Good.class ); List<Bean_Good.ResultBean> list_good = bean_good.getResult(); Adapter_Good adapter_good = new Adapter_Good( R.layout.item_good, list_good ); GridLayoutManager gridLayoutManager = new GridLayoutManager( MainActivity.this, 3 ); rv_good.setLayoutManager( gridLayoutManager ); rv_good.setAdapter( adapter_good );*/ } } ); } @Override public void onDestroyView() { super.onDestroyView(); } }
Adapter_Zuo
public class Adapter_Zuo extends BaseQuickAdapter<Bean_Zuo.ResultBean,BaseViewHolder> { public Adapter_Zuo(int layoutResId, @Nullable List<Bean_Zuo.ResultBean> data) { super( layoutResId, data ); } @Override protected void convert(BaseViewHolder helper, Bean_Zuo.ResultBean item) { helper.setText( R.id.tv_left_name,item.getName() ); } }
Adapter_You
public class Adapter_You extends BaseQuickAdapter<Bean_You.ResultBean,BaseViewHolder> { private List<Bean_Good.ResultBean> list_good; public Adapter_You(int layoutResId, @Nullable List<Bean_You.ResultBean> data) { super( layoutResId, data ); } @Override protected void convert(BaseViewHolder helper, Bean_You.ResultBean item) { helper.setText( R.id.tv_you_name,item.getName() ); String id = item.getId();//得到第二条数据的id Toast.makeText( mContext, ""+id, Toast.LENGTH_SHORT ).show(); final RecyclerView rv_good = helper.getView( R.id.rv_good ); OkHttpUtil.getGetJson( Constant.GOOD_URL + "?categoryId=" + id + "&page=" + 1 + "&count=" + 5, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String message = response.body().string(); //解析数据 Gson gson = new Gson(); Bean_Good bean_good = gson.fromJson( message, Bean_Good.class ); list_good = bean_good.getResult(); } } ); //给商品rv设置适配器 //Toast.makeText( mContext, ""+list_good, Toast.LENGTH_SHORT ).show(); GridLayoutManager gridLayoutManager = new GridLayoutManager( mContext, 3 ); rv_good.setLayoutManager( gridLayoutManager ); Adapter_Good adapter_good = new Adapter_Good( R.layout.item_good, list_good ); rv_good.setAdapter( adapter_good ); } }
Adapter_Goods public class Adapter_Good extends BaseQuickAdapter<Bean_Good.ResultBean,BaseViewHolder> { public Adapter_Good(int layoutResId, @Nullable List<Bean_Good.ResultBean> data) { super( layoutResId, data ); } @Override protected void convert(BaseViewHolder helper, Bean_Good.ResultBean item) { ImageView img_good = helper.getView( R.id.img_good ); Glide.with( mContext ).load( item.getMasterPic() ).into( img_good ); helper.setText( R.id.tv_good_name,item.getCommodityName() ); } }
activity_shou
//左侧 <android.support.v7.widget.RecyclerView android:id="@+id/rv_left" android:layout_width="0dp" android:layout_weight="2" android:background="#CCCCCC" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> //右侧 <android.support.v7.widget.RecyclerView android:id="@+id/rv_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="8"> </android.support.v7.widget.RecyclerView>
item_left
TextView android:id="@+id/tv_left_name" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="商品名称"/>
item_right
<TextView android:id="@+id/tv_you_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30dp" android:textColor="#f00" android:text="商品名称"/> <android.support.v7.widget.RecyclerView android:id="@+id/rv_good" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView>
item_goods
<android.support.v7.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" app:cardCornerRadius="10dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:orientation="vertical"> //图片 <ImageView android:id="@+id/img_good" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher"/> //name <TextView android:id="@+id/tv_good_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:ellipsize="end" android:singleLine="true" android:layout_marginTop="3dp" android:text="namee"/> </LinearLayout> </android.support.v7.widget.CardView>