Recycler in Fragment не отображается - PullRequest
0 голосов
/ 10 февраля 2020

Я делаю приложение, которое использует представление переработчика во фрагменте. как содержимое представления переработчика не показывалось. Я не уверен, что сделал неправильно, поскольку приложение не запускает sh при запуске. Пожалуйста, скажите мне, что мне нужно сделать.

фрагмент_дома. xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"`enter code here`
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".Fragments.Home_Fragment">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cetagory_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:elevation="3dp" />

    </FrameLayout>

Home_Fragment. java

        private RecyclerView categoryRecyclerView;
        private CategoryAdapter categoryAdapter;

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

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

            categoryRecyclerView = view.findViewById(R.id.cetagory_recyclerview);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            categoryRecyclerView.setLayoutManager(layoutManager);

            List<CategoryModel> categoryModelList = new ArrayList<CategoryModel>();
            categoryModelList.add(new CategoryModel("Link","Home"));
            categoryModelList.add(new CategoryModel("Link","Electronicse"));
            categoryModelList.add(new CategoryModel("Link","Garments"));
            categoryModelList.add(new CategoryModel("Link","Fashion"));
            categoryModelList.add(new CategoryModel("Link","Toys"));
            categoryModelList.add(new CategoryModel("Link","Sporte"));
            categoryModelList.add(new CategoryModel("Link","Furniture"));
            categoryModelList.add(new CategoryModel("Link","Wall Art"));
            categoryModelList.add(new CategoryModel("Link","Appliance"));

            categoryAdapter = new CategoryAdapter(categoryModelList);
            categoryRecyclerView.setAdapter(categoryAdapter);
            categoryAdapter.notifyDataSetChanged();

            return view;
        }

CategoryModel. java

        private String categoryItemIconLink;
        private String categoryName;

        public CategoryModel(String categoryItemIconLink, String categoryName) {
            this.categoryItemIconLink = categoryItemIconLink;
            this.categoryName = categoryName;
        }

        public String getCategoryItemIconLink() {
            return categoryItemIconLink;
        }

        public void setCategoryItemIconLink(String categoryItemIconLink) {
            this.categoryItemIconLink = categoryItemIconLink;
        }

        public String getCategoryName() {
            return categoryName;
        }

        public void setCategoryName(String categoryName) {
            this.categoryName = categoryName;
        }

CategoryAdapter. java

        Context context;
        private List<CategoryModel> categoryModelList = new ArrayList<CategoryModel>();

        public CategoryAdapter(List<CategoryModel> categoryModerList) {
            this.context = context;
            this.categoryModelList = categoryModelList;
        }
        @NonNull
        @Override
        public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {

            String icon = categoryModelList.get(position).getCategoryItemIconLink();
            String name = categoryModelList.get(position).getCategoryName();
            holder.setCategoryName(name);
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {

            private ImageView categoryIcon;
            private TextView categoryName;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);

                categoryIcon = itemView.findViewById(R.id.category_item_icon);
                categoryName = itemView.findViewById(R.id.category_item_name);
            }

            private void setCategoryIcon() {
                //Todo: set categoryIcone here
            }

            private void  setCategoryName(String name) {
                categoryName.setText(name);
            }
        }

1 Ответ

0 голосов
/ 10 февраля 2020

Вы не инициализировали свои переменные внутри вашего адаптера

Замените это ...

 public CategoryAdapter(List<CategoryModel> categoryModerList) {
            this.context = context;
            this.categoryModelList = categoryModelList;
        }

Этим ..

  public CategoryAdapter(Context context,List<CategoryModel> categoryModelList) {
        this.context = context;
        this.categoryModelList = categoryModelList;
    }

затем внутри вашего фрагмента, где вы вызываете адаптеры конструктора add, измените эту строку. от ..

categoryAdapter = new CategoryAdapter(categoryModelList);

до ..

categoryAdapter = new CategoryAdapter(getContext(),categoryModelList);
...