欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Xc_二级联动

程序员文章站 2024-02-22 11:17:16
...

----------首页的展示二级联动

public class Fragment_One extends Fragment {
private RecyclerView recycler_view1;
private RecyclerView recycler_view2;
private Presenter presenter;
private LeftAdapter leftAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
recycler_view1 = view.findViewById(R.id.recycler_view1);
recycler_view2 = view.findViewById(R.id.recycler_view2);
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
recycler_view1.setLayoutManager(linearLayoutManager1);
recycler_view2.setLayoutManager(linearLayoutManager2);
presenter = new Presenter(this);
presenter.getLeft();
return view;
}

public void getLeft(final String data) {
    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Gson gson =new Gson();
            final LeftBean leftBean = gson.fromJson(data, LeftBean.class);
            List<LeftBean.ResultEntity> result = leftBean.getResult();
            leftAdapter = new LeftAdapter(getActivity(),result);
            recycler_view1.setAdapter(leftAdapter);
            leftAdapter.setClick(new LeftAdapter.Click() {
                @Override
                public void click(View view, int position) {
                    String id = leftBean.getResult().get(position).getId();
                    presenter.getRight(id);
                }
            });
        }
    });
}

public void getRight(final String data) {
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Gson gson =new Gson();
            RightBean rightBean = gson.fromJson(data, RightBean.class);
            List<RightBean.ResultEntity> list = rightBean.getResult();
            RightAdapter rightAdapter =new RightAdapter(getActivity(),list);
            recycler_view2.setAdapter(rightAdapter);
        }
    });
}

}

—适配器

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.ViewHolder> {
Context context;
List<LeftBean.ResultEntity> list;

public LeftAdapter(Context context, List<LeftBean.ResultEntity> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(viewGroup.getContext(), R.layout.left_layout,null);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
    viewHolder.name.setText(list.get(i).getName());
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int layoutPosition = viewHolder.getLayoutPosition();
            View itemView = viewHolder.itemView;
            if (click!=null){
                click.click(itemView,layoutPosition);
            }
        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private final TextView name;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.left_text);
    }
}

public interface Click{
    void click(View view,int position);
}
public Click click;

public void setClick(Click click){
    this.click = click;
}

}

相关标签: xx