简体   繁体   English

如何设置具有动态跨度计数的android交错水平回收器视图

[英]How to set android staggered horizontal recycler view with dynamic span count

I need this kind of recycler view .我需要这种回收器视图 But when I use horizontal staggered view , I have to define the row count.但是当我使用水平交错视图时,我必须定义行数。

在此处输入图片说明

I need to solve these problems below.我需要在下面解决这些问题。

  1. There should not be a horizontal scroll bar or scroll view.不应有水平滚动条或滚动视图。
  2. when the first row is filled, the view must start filling the next row.当第一行被填充时,视图必须开始填充下一行。
  3. component count per row can be dynamic.每行的组件计数可以是动态的。

This is my code这是我的代码

DeviceProfileFragment.java设备配置文件片段.java

public class DeviceProfileFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    RecyclerView moduleList;
    List<String> modules;
    ModuleRowViewAdapter adapter;

    public DeviceProfileFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DeviceProfileFragment newInstance(String param1, String param2) {
        DeviceProfileFragment fragment = new DeviceProfileFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_device_profile, container, false);
        moduleList = (RecyclerView) view.findViewById(R.id.moduleList);

        modules = new ArrayList<>();
        modules.add("IR Module");
        modules.add("x2 Motor Driver");
        modules.add("x2 Motor Driver");
        modules.add("Display");
        modules.add("Battery");

        adapter = new ModuleRowViewAdapter(this.getContext(),modules);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL);
        moduleList.setLayoutManager(staggeredGridLayoutManager);
        moduleList.setAdapter(adapter);
        return view;
    }
}

This is Adapter class of the fragment这是片段的适配器类

ModuleRowViewAdapter.java ModuleRowViewAdapter.java

public class ModuleRowViewAdapter extends RecyclerView.Adapter<ModuleRowViewAdapter.ViewHolder> {

    List<String> modules;
    LayoutInflater inflater;

    public ModuleRowViewAdapter(Context ctx, List<String> modules){
        this.modules=modules;
        this.inflater=LayoutInflater.from(ctx);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item_module,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.moduleName.setText(modules.get(position));
    }

    @Override
    public int getItemCount() {

        return modules.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

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

Use FlexboxLayoutManager as the layout manager for your recyclerview, from https://github.com/google/flexbox-layout .使用FlexboxLayoutManager作为 recyclerview 的布局管理器,来自https://github.com/google/flexbox-layout

I have project that has this exact UI/requirements and uses FlexboxLayoutManager(context, FlexDirection.ROW, FlexWrap.WRAP) to achieve it.我有一个具有这个确切 UI/要求的项目,并使用FlexboxLayoutManager(context, FlexDirection.ROW, FlexWrap.WRAP)来实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM