gpt4 book ai didi

java - Android - 在抽屉导航的Fragment中调用RecyclerView

转载 作者:行者123 更新时间:2023-12-02 03:03:58 28 4
gpt4 key购买 nike

我的麻烦是当我想将 RecyclerView 调用到 NavigationDrawerFragment 以及通过 RecyclerView 调用的项目(数组)时 没有被调用,但是当我不放入 Fragment 时它可以被调用。这是我的代码。

这是我的Fragment.java,它在recyclerView.setLayoutManager(layoutManager); 上显示错误

public class fragment_1bakso extends Fragment {

private AppCompatActivity compat;
private RecyclerView recyclerView;
private MakananAdapter adapter;
public fragment_1bakso() {
// Required empty public constructor
}

ArrayList<Makanan> mList = new ArrayList<>();
MakananAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
/*View v = inflater.inflate(R.layout.fragment_1bakso, container, false);
return v;*/
View rootview = inflater.inflate(R.layout.activity_calling, container, false);
return rootview;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mClass.setContentView(R.layout.activity_calling);
//return new fragment_1bakso(R.layout.activity_calling, this);


GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);

mAdapter = new MakananAdapter(mList);
recyclerView.setAdapter(mAdapter);


fillData();
}

private void fillData() {
Resources resources = getResources();
String [] arJudul = resources.getStringArray(R.array.bakso);
String [] arHarga = resources.getStringArray(R.array.bakso_price);
String [] arDeskripsi = resources.getStringArray(R.array.bakso_desc);
String [] arLokasi = resources.getStringArray(R.array.bakso_locations);
String [] arDetail = resources.getStringArray(R.array.bakso_details);
TypedArray a = resources.obtainTypedArray(R.array.bakso_pict);
Drawable[] arFoto = new Drawable[a.length()];

for (int i = 0; i < arFoto.length; i++)
{
arFoto[i] = a.getDrawable(i);
}
a.recycle();

for (int i = 0; i < arJudul.length; i++)
{
mList.add(new Makanan(arJudul[i],arHarga[i],arFoto[i],arDeskripsi[i],arDetail[i],arLokasi[i]));
}
mAdapter.notifyDataSetChanged();

}}

这是我的Fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="id.sch.smktelkom_mlg.learn.sideview.fragment_1bakso">

<!-- TODO: Update blank fragment layout -->
<include
layout="@layout/activity_calling"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

这是我的 Activity_calling.xml,用于调用数组中的项目 (RecyclerView)

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:scrollbars="vertical"
tools:context="id.sch.smktelkom_mlg.learn.sideview.activity_1bakso" />

帮帮我吧,先谢谢了。

最佳答案

您尚未初始化您的recyclerView。

recyclerView = (RecyclerView) view.findViewById(R.id.your_id);

还将代码从 onCreate() 移至 onViewCreated();

关于java - Android - 在抽屉导航的Fragment中调用RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42016248/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com