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

n3h0vuf2  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(296)

我需要这种回收的观点。但是当我使用水平交错视图时,我必须定义行计数。

我需要在下面解决这些问题。
不应有水平滚动条或滚动视图。
填充第一行后,视图必须开始填充下一行。
每行的组件计数可以是动态的。
这是我的密码
deviceprofilefragment.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;
    }
}

这是片段的适配器类
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);
       }
   }
}
u5i3ibmn

u5i3ibmn1#

使用 FlexboxLayoutManager 作为您的recyclerview的布局管理器https://github.com/google/flexbox-layout.
我有一个项目,有这个确切的用户界面/需求和用途 FlexboxLayoutManager(context, FlexDirection.ROW, FlexWrap.WRAP) 为了实现它。

相关问题