activity布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.MainActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
适配器布局 左
android:id="@+id/left_adapater"
android:layout_width="match_parent"
android:layout_height="50dp">
android:layout_width="match_parent"
android:layout_height="50dp" />
右
android:layout_width="match_parent"
android:layout_height="50dp">
android:layout_width="match_parent"
android:layout_height="50dp" />
适配器 右
public class RightAdapater extends BaseQuickAdapter
public RightAdapater(int layoutResId, @Nullable List
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, TwoBean.ResultBean item) {
helper.setText(R.id.right_tv,item.getName());
}
}
左
public class LeftAdapater extends BaseQuickAdapter
private double mPosition;
public LeftAdapater(int layoutResId, @Nullable List
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, OneBean.ResultBean item) {
helper.setText(R.id.left_tv,item.getName());
View view = helper.getView(R.id.left_adapater);
if (mPosition!=helper.getAdapterPosition()){
view.setBackgroundResource(R.color.colorAccent);
}else {
view.setBackgroundResource(0);
}
}
public void setCurrentPosition(int Position) {
mPosition=Position;
notifyDataSetChanged();
}
}
MVP
契约类
public interface HomeContract {
public interface HomeModel{
void HomeData(Context context,HomeBack homeBack);
void HomeTwoData(String firstCategoryId,HomeTwoBack homeTwoBack);
interface HomeBack{
void getData(OneBean oneBean);
}
interface HomeTwoBack{
void getTwoData(TwoBean twoBean);
}
}
interface HomeView{
void showData(OneBean oneBean);
void showTwoData(TwoBean twoBean);
}
interface HomePresenter{
void onAttachView();
void onDetachView();
}
}
M层
public class HomeModel implements HomeContract.HomeModel {
private HttpUtils util;
@Override
public void HomeData(Context context, final HomeBack homeBack) {
util = HttpUtils.getUtil();
boolean work = util.NetWork(context);
if (work){
ApiService apiService = util.requestData(ApiService.class);
Observable
onedata.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer
@Override
public void accept(OneBean oneBean) throws Exception {
homeBack.getData(oneBean);
}
});
}else {
Toast.makeText(context, "无网络连接", Toast.LENGTH_SHORT).show();
}
}
@Override
public void HomeTwoData(String firstCategoryId, final HomeTwoBack homeTwoBack) {
ApiService apiService = util.requestData(ApiService.class);
Observable
twodata.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer
@Override
public void accept(TwoBean twoBean) throws Exception {
homeTwoBack.getTwoData(twoBean);
}
});
}
}
P层
public class HomePresenter
private final HomeModel model;
public HomePresenter(){
model = new HomeModel();
}
public void getData(Context context){
model.HomeData(context, new HomeContract.HomeModel.HomeBack() {
@Override
public void getData(OneBean oneBean) {
getView().showData(oneBean);
}
});
}
public void getTwoData( String firstCategoryId){
model.HomeTwoData( firstCategoryId, new HomeContract.HomeModel.HomeTwoBack() {
@Override
public void getTwoData(TwoBean twoBean) {
getView().showTwoData(twoBean);
}
});
}
}
Activity代码
public class MainActivity extends BaseActivity
@BindView(R.id.main_right)
RecyclerView mainRight;
@BindView(R.id.main_left)
RecyclerView mainLeft;
private RightAdapater rightAdapater;
private LeftAdapater leftAdapater;
private HttpUtils util;
@Override
protected void initData() {
presenter.getData(this);
}
@Override
protected HomePresenter
return new HomePresenter<>();
}
@Override
protected int getLayout() {
return R.layout.activity_main;
}
@Override
public void showData(final OneBean oneBean) {
mainLeft.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
leftAdapater = new LeftAdapater(R.layout.main_left, oneBean.getResult());
mainLeft.setAdapter(leftAdapater);
leftAdapater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
leftAdapater.setCurrentPosition(position);
String id = oneBean.getResult().get(position).getId();
presenter.getTwoData(id);
}
});
}
@Override
public void showTwoData(TwoBean twoBean) {
mainRight.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
rightAdapater = new RightAdapater(R.layout.main_right, twoBean.getResult());
mainRight.setAdapter(rightAdapater);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: add setContentView(...) invocation
ButterKnife.bind(this);
}
}
一个借口的联动
Activity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.rv_zuo)
RecyclerView mRvZuo;
@BindView(R.id.rv_you)
RecyclerView mRvYou;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
RetrofitUtil.getInstance().setRetrofitClass(new RetrofitUtil.RetrofitClass() {
@Override
public void getData(String resqonBody) {
Gson gson = new Gson();
final Bean bean = gson.fromJson(resqonBody, Bean.class);
final List
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
mRvZuo.setLayoutManager(layoutManager);
ZuoAdapter zuoAdapter = new ZuoAdapter(R.layout.zuo, result);
mRvZuo.setAdapter(zuoAdapter);
zuoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
List
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
mRvYou.setLayoutManager(gridLayoutManager);
YouAdapter youAdapter = new YouAdapter(R.layout.you, secondCategoryVo);
mRvYou.setAdapter(youAdapter);
}
});
}
});
}
}
适配器
public class ZuoAdapter extends BaseQuickAdapter
public ZuoAdapter(int layoutResId, @Nullable List
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, Bean.ResultBean item) {
helper.setText(R.id.tv_zuo,item.getName());
}
}