Reputation: 13
My trouble is when I want to call RecyclerView
into Fragment
of NavigationDrawer
and items (arrays) that I call via RecyclerView
not called, but when I do not put into Fragment
it could be called. So Here is my code.
Here is my Fragment.java
and it says error on 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();
}}
Here is my 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>
Here is my Activity_calling.xml
that used to call the item in array (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" />
Help me guys, thanks in advance.
Upvotes: 0
Views: 3046
Reputation: 2949
You did not initialise your recyclerview which can be done as :
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
And define grid layout manager as :
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
recyclerView .setHasFixedSize(true);
recyclerView .setItemAnimator(new DefaultItemAnimator());
recyclerView .setLayoutManager(gridLayoutManager);
And yes move your code from onCreate to onViewCreated as :
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
Hope it helps.. happy coding :)
Upvotes: 0
Reputation: 181
You haven't initialized your recyclerView.
recyclerView = (RecyclerView) view.findViewById(R.id.your_id);
Also move the code from onCreate() to onViewCreated();
Upvotes: 2
Reputation: 62
final RecyclerView.LayoutManager mLayoutManager =new GridLayoutManager(getContext(), 1);
recyclerView_drawer.setLayoutManager(mLayoutManager);
recyclerView_drawer.setItemAnimator(new DefaultItemAnimator());
recyclerView_drawer.setAdapter(drawerAdapter);
Hope this code will help you.you can replace frame layout with Relative Layout also,as you said in frame layout its not working.
Upvotes: 0
Reputation: 1170
If you need grid layout manager you can try this out
GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
Or you can go for a horizontal linear layout manager like so
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
Or you can go for a vertical linear layout manager like so
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
Upvotes: 0