user3432681
user3432681

Reputation: 564

Implement a RecyclerView in a Fragment

I trying to create a ListView in a Fragment within a ViewPager in a AppCompatActivity. In the AppCompatActivity are all view elements are wrappend in a CoordinatorLayout. Because I used the CoordinatorLayout. I have to use a RecylerView I am trying to follow the training from developer.android.com, but my application stopped after my log. This is my code in myFragment where the applications stopped.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_city_list, container, false)

    mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
    mLayoutManager = new LinearLayoutManager(this.getActivity());
    Log.d("debugMode", "The application stopped after this");
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecyclerAdapter(getNames());
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

//...

Upvotes: 5

Views: 24375

Answers (3)

S Kranthi Kumar
S Kranthi Kumar

Reputation: 750

public class HomeFragment extends Fragment {

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    homeViewModel =
            ViewModelProviders.of(this).get(HomeViewModel.class);
    View root = inflater.inflate(R.layout.fragment_home, container, false);
    final TextView textView = root.findViewById(R.id.text_home);
    homeViewModel.getText().observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            textView.setText(s);
        }
    });
     FragmentActivity c = getActivity();
     RecyclerView recyclerView = root.findViewById(R.id.my_recycler_view);
    recyclerView.setLayoutManager(new GridLayoutManager(c, 2));

    List<VillageModel> villageModels=new ArrayList<>();
    VillageModel villageModel =new VillageModel("",0,0,0,0,0,0);
    villageModels.add(villageModel);
    VillageModel villageModel1 =new VillageModel("",0,0,0,0,0,0);
    VillageModel villageModel2 =new VillageModel("",0,0,0,0,0,0);
    villageModels.add(villageModel1);
    villageModels.add(villageModel2);
    VillageAdapter adapter = new VillageAdapter(c,villageModels);
    recyclerView.setAdapter(adapter);
    return root;
 }
}

add the recycle adapter to the oncreateView fragment home xml structure

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    />
</RelativeLayout>

Upvotes: 0

Daniel Martin Shum
Daniel Martin Shum

Reputation: 547

did you set the android.R.id.list as the recyclerview id? otherwise you should set an id to the recyclerview in the xml file, like this:

android:id="@+id/recyclerview"

and change this line

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);

to

mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);

Upvotes: 0

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Use this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_city_list, container, false)

    // Replace 'android.R.id.list' with the 'id' of your RecyclerView
    mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
    mLayoutManager = new LinearLayoutManager(this.getActivity());
    Log.d("debugMode", "The application stopped after this");
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecyclerAdapter(getNames());
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

You should call the setLayoutManager & setAdapter methods (respectively) on the Recyclerview.

Plus,

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);

you should not use android.R.id.list, as you're not using a ListFragment. Replace it with the id of you Recyclerview (as in your XML layout).

Upvotes: 10

Related Questions