所以我有主要的底部导航活动,共有三个部分,其中之一是仪表板。我想在那里建立一个类别网格。我一直在尝试遵循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>
答案 0 :(得分:0)
嗨,我不知道这是否是您解决问题的方法,但是您在扩大视图时不使用任何上下文
List