无法在BottomNavigation活动中解析布局

问题描述 投票:0回答:1

所以我有主要的底部导航活动,共有三个部分,其中之一是仪表板。我想在那里建立一个类别网格。我一直在尝试遵循youtube上有关创建GridView的一些指南,但是在此底部导航活动中似乎不起作用。主要问题在于,在我的自定义适配器的getView方法中,它无法解析R.layout.category_item,但是文件存在。感谢您提供一些解决方法或解释其发生原因的方法。

DashboardFragment类:

    public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    String[] category_names = {"one","two","three","four","five","six","seven","eight","nine","ten"};
    int[] data ={1,2,3,4,5,6,7,8,9,10};

    GridView gridView;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
            }
        });

        gridView = root.findViewById(R.id.category_grid);
        CustomAdapter customAdapter = new CustomAdapter();
        gridView.setAdapter(customAdapter);

        return root;
    }


    private class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return category_names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view1 = getLayoutInflater().inflate(R.layout.category_item,null);
            return null;
        }
    }
}

category_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.dashboard.DashboardFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
java android android-layout android-gridview
1个回答
0
投票

嗨,我不知道这是否是您解决问题的方法,但是当您增加视图时您不使用任何上下文

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // inflate the layout for each list row
        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                inflate(R.layout.layout_list_view_row_items, parent, false);
        }
       ...
}
© www.soinside.com 2019 - 2024. All rights reserved.