效果图
我的想法:
1.创建布局里放进两个fragment
左边的fragment是一级展示,右边的fragment展示是解析第二个之后再解析第三个
在布局这个activity中
// 创建管理者
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragoo fragoo = new Fragoo();
Fragtt fragtt = new Fragtt();
// 添加两个fragment
transaction.add(R.id.recyyj, fragoo);
transaction.add(R.id.recyrj, fragtt);
transaction.commit();
.第一个fragment 解析展示略过同平常的一致
.在第一个recyclerview适配器中设置点击事件
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewHolder.getLayoutPosition();
if (onFenYiListener!=null){
onFenYiListener.onfen(position);
}
}
});
下面写接口方法
// 创建接口
public interface OnFenYiListener {
void onfen(int position);
}
//声明接口
public OnFenYiListener onFenYiListener;
// 创建方法
public void setOnFenYiListener(OnFenYiListener onFenYiListener) {
this.onFenYiListener = onFenYiListener;
}
返回写一级页面中在写点击事件
// 点击事件
myFenYiAdapter.setOnFenYiListener(new MyFenYiAdapter.OnFenYiListener() {
@Override
public void onfen(int position) {
Toast.makeText(getActivity(),"123",Toast.LENGTH_SHORT).show();
// 解析
try {
JSONObject jsonObject = result.getJSONObject(position);
String id = jsonObject.getString("id");
onFragoo.onOo(id);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
在下面写接口
// 接口回调
public interface OnFragoo {
void onOo(String id);
}
//声明
public OnFragoo onFragoo;
// 方法
public void getOnFragoo(OnFragoo onFragoo) {
this.onFragoo = onFragoo;
}
第二个fragment–她是从第一个oo(getOnFragoo)连接
// 找控件
recyrj = view.findViewById(R.id.recyrj);
// 布局管理器
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
recyrj.setLayoutManager(manager);
// 创建管理者
FragmentManager manager1 = getActivity().getSupportFragmentManager();
Fragoo oo = (Fragoo) manager1.findFragmentById(R.id.recyyj);
oo.getOnFragoo(this);
//
return view;
}
@Override
public void onOo(String id) {
fenRjPresenter = new FenRjPresenter(this);
fenRjPresenter.fenrj(id);
}
//url + "?firstCategoryId=" + id 拼接接口
之后一致
rj适配器
String url = "http://172.17.8.100/small/commodity/v1/findCommodityByCategory";
Context context;
JSONArray result;
private Handler handler = new Handler();
public MyFenRjAdapter(Context context, JSONArray result) {
this.context = context;
this.result = result;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.fenrj, null, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
if (viewHolder instanceof ViewHolder) {
// 解析
try {
final JSONObject jsonObject = result.getJSONObject(i);
String name = jsonObject.getString("name");
String id = jsonObject.getString("id");
// Log.i("ername", name);
// 赋值
viewHolder.rjtext.setText(name);
viewHolder.rjrecy.setLayoutManager(new GridLayoutManager(context, 4));
OkHttpUntil.getInstance().doGet(url + "?categoryId=" + id + "&page=1&count=8", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
String string = null;
try {
string = response.body().string();
JSONObject jsonObject1 = new JSONObject(string);
JSONArray result = jsonObject1.getJSONArray("result");
// Log.i("sanhandler", result.toString());
SanAdapter sanAdapter = new SanAdapter(context, result);
viewHolder.rjrecy.setAdapter(sanAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public int getItemCount() {
return result.length();
}
class ViewHolder extends RecyclerView.ViewHolder {
private final TextView rjtext;
private final RecyclerView rjrecy;
public ViewHolder(@NonNull View itemView) {
super(itemView);
rjtext = itemView.findViewById(R.id.rjtext);
rjrecy = itemView.findViewById(R.id.rjrecy);
}
}