具体代码:
https://github.com/sunli0828/Month_Demo/blob/master/sunli1220/src/main/java/com/sunli/test1220/LevelLinkActivity.java
//主页面
public class LevelLinkActivity extends AppCompatActivity implements IView, View.OnClickListener{
private IPresenterImpl iPresenter;
private RecyclerView recycler_parent;
private RecyclerView recycler_children;
private boolean flag = true;
private ParentAdapter parentAdapter;
private int index;
private ChildrenAdapter childrenAdapter;
private List carBeanData;
private Button btn_animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_link);
iPresenter = new IPresenterImpl(this);
iPresenter.startRequest(ApiUtils.URL_SHOPCAR_INFO, null, CarBean.class);
recycler_parent = findViewById(R.id.level_recycler_parent);
recycler_children = findViewById(R.id.level_recycler_children);
btn_animator = findViewById(R.id.level_btn_animator_code);
btn_animator.setOnClickListener(this);
recycler_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iPresenter.startRequest(ApiUtils.URL_SHOPCAR_INFO, null, CarBean.class);
}
});
showParent();
parentAdapter.setListener(new ParentAdapter.ParentCallbackListener() {
@Override
public void parentCallbackListener(int i) {
index = i;
flag = false;
showChildren();
}
});
}
//父
private void showParent() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LevelLinkActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler_parent.setLayoutManager(linearLayoutManager);
parentAdapter = new ParentAdapter(this);
recycler_parent.setAdapter(this.parentAdapter);
recycler_parent.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
}
//子
private void showChildren() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler_children.setLayoutManager(linearLayoutManager);
flag = false;
childrenAdapter = new ChildrenAdapter(this);
recycler_children.setAdapter(childrenAdapter);
recycler_children.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
iPresenter.startRequest(ApiUtils.URL_SHOPCAR_INFO, null, CarBean.class);
childrenAdapter.setListBeanList(carBeanData.get(index).getList());
}
@Override
public void showResponseData(Object data) {
CarBean carBean = (CarBean) data;
carBeanData = carBean.getData();
if (carBeanData != null) {
carBeanData.remove(0);
parentAdapter.setDataBeanList(carBeanData);
}
}
@Override
public void showResponseFail(Exception e) {
}
@Override
protected void onDestroy() {
super.onDestroy();
iPresenter.onDetach();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.level_btn_animator_code:
Intent intentAnimator = new Intent(LevelLinkActivity.this, AnimatorOrCodeActivity.class);
startActivity(intentAnimator);
break;
default:
break;
}
}
}
//父RecyclerView的适配器
public class ParentAdapter extends RecyclerView.Adapter {
private List dataBeanList = new ArrayList<>();
private Context dataContext;
public ParentAdapter(Context dataContext) {
this.dataContext = dataContext;
}
public void setDataBeanList(List dataBeanList) {
this.dataBeanList = dataBeanList;
notifyDataSetChanged();
}
@NonNull
@Override
public ParentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(dataContext, R.layout.adapter_item_parent, null);
ParentViewHolder parentViewHolder = new ParentViewHolder(view);
return parentViewHolder;
}
@Override
public void onBindViewHolder(@NonNull ParentViewHolder parentViewHolder, final int i) {
parentViewHolder.sellerName.setText(dataBeanList.get(i).getSellerName());
parentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (parentCallbackListener != null) {
parentCallbackListener.parentCallbackListener(i);
}
}
});
}
@Override
public int getItemCount() {
return dataBeanList.size();
}
public class ParentViewHolder extends RecyclerView.ViewHolder {
TextView sellerName;
public ParentViewHolder(@NonNull View itemView) {
super(itemView);
sellerName = itemView.findViewById(R.id.item_parent_text);
}
}
public interface ParentCallbackListener{
void parentCallbackListener(int i);
}
ParentCallbackListener parentCallbackListener;
public void setListener(ParentCallbackListener listener) {
this.parentCallbackListener = listener;
}
}
//子RecyclerView的适配器
public class ChildrenAdapter extends RecyclerView.Adapter {
private List listBeanList;
private Context listContext;
public ChildrenAdapter(Context listContext) {
this.listContext = listContext;
}
public void setListBeanList(List listBeanList) {
this.listBeanList = listBeanList;
notifyDataSetChanged();
}
@NonNull
@Override
public ChilderenViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(listContext, R.layout.adapter_item_children, null);
return new ChilderenViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ChilderenViewHolder childerenViewHolder, final int i) {
String url = listBeanList.get(i).getImages().split("\\|")[0].replace("https", "http");
Glide.with(listContext).load(url).into(childerenViewHolder.childrenIcon);
childerenViewHolder.childrenTitle.setText(listBeanList.get(i).getTitle());
childerenViewHolder.childrenPrice.setText(listBeanList.get(i).getPrice() + "");
childerenViewHolder.chilerenNum.setText(listBeanList.get(i).getNum() + "");
}
@Override
public int getItemCount() {
return listBeanList.size();
}
public class ChilderenViewHolder extends RecyclerView.ViewHolder {
ImageView childrenIcon;
TextView childrenPrice, childrenTitle, chilerenNum;
public ChilderenViewHolder(@NonNull View itemView) {
super(itemView);
childrenIcon = itemView.findViewById(R.id.item_children_icon);
childrenTitle = itemView.findViewById(R.id.item_children_title);
childrenPrice = itemView.findViewById(R.id.item_children_price);
chilerenNum = itemView.findViewById(R.id.item_children_num);
}
}
}
xml文件
① activity
② parent
③ children