Recycler View Not Showing
Asked Answered
J

11

33

I have been making an app that uses a recycler view in a navigation drawer. Why the contents of the recycler view are not showing up. The view is definitely there as I can see the scroll shadows. I am not sure what I have done wrong, as the app does not crash when it is run.

Navigation Drawer Fragment:

public class NavigationDrawerFragment extends android.support.v4.app.Fragment {

    public static final String PREF_FILE_NAME = "testPref";
    public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
    private RecyclerView mRecyclerView;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private View containerView;
    private Boolean mUserLearnedDrawer;
    private Boolean mFromSavedInstanceState;
    private DrawerAdapter adapter;

    public NavigationDrawerFragment() {
        mFromSavedInstanceState = false;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
        if (savedInstanceState != null){
            mFromSavedInstanceState = true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        mRecyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
        adapter = new DrawerAdapter(getActivity(), getData());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return layout;
    }

    public static List<DrawerRow> getData(){
        List<DrawerRow> data = new ArrayList<>();
        int icons[] = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
        String titles[] = {"Link 1","Link 2","Link 3","Link 4"};
        for (int i = 0 ; i < titles.length && i < icons.length; i++){
            DrawerRow current = new DrawerRow();
            current.iconId = icons[i];
            current.title = titles[i];
            data.add(current);
        }

        return data;
    }

    public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
        containerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClosed){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if(!mUserLearnedDrawer){
                    mUserLearnedDrawer = true;
                    saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer+"");
                }
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }
        };

        if(!mUserLearnedDrawer && !mFromSavedInstanceState){
            mDrawerLayout.openDrawer(containerView);
        }

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });
    }

    public void saveToPreferences(Context context, String prefName, String prefValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = sharedPreferences.edit();
        mEditor.putString(prefName, prefValue);
        mEditor.apply();
    }

    public static String readFromPreferences(Context context, String prefName, String defaultValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(prefName,defaultValue);
    }
}

Adapter:

public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.MyViewHolder>{

    private LayoutInflater inflator;
    List<DrawerRow> data = Collections.EMPTY_LIST;

    public DrawerAdapter(FragmentActivity activity, List<DrawerRow> data) {
        inflator = LayoutInflater.from(activity);
        this.data = data;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflator.inflate(R.layout.nav_drawer_row , viewGroup,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int i) {
        DrawerRow current = data.get(i);
        viewHolder.title.setText(current.title);
        viewHolder.icon.setImageResource(current.iconId);
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView icon;
        TextView title;

        public MyViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.list_icon);
            title = (TextView) itemView.findViewById(R.id.list_text);
        }
    }
}

Drawer Row:

public class DrawerRow {
    int iconId;
    String title;
}

Layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout">

    <!--Main Content -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text" />

    </LinearLayout>

    <!--Nav Drawer -->
    <fragment
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="uk.co.clickcomputing.hhscweatherstation.NavigationDrawerFragment"
        android:id="@+id/fragment_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

Drawer Row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:padding="@dimen/list_padding"
        android:id="@+id/list_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:padding="@dimen/list_padding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="New Text"
        android:id="@+id/list_text" />
</LinearLayout>

Navigation Drawer Fragment Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_marginBottom="8dp"
            android:contentDescription="@string/nav_drawer_img_desciption"
            android:scaleType="centerCrop"
            android:src="@drawable/nav_drawer" />
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/drawerList">
    
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
Jural answered 30/12, 2014 at 12:55 Comment(5)
Can you share the layout file that includes the RecyclerView? My guess is that you've set width or height to wrap content, which is not yet supported by the default layout managers.Chemisorb
@Chemisorb updated with the layoutJural
so as I guessed, you have android:layout_height="wrap_content" on the recycler view which is not yet supported by the default layout managers. You can either change it to match_parent or override onMeasure in your layout manager and do the measurement yourself.Chemisorb
@Chemisorb still nothing is displayedJural
that doesn't make sense. Do you have a sample app that you can attach? APK should be enough to debug but if you can attach w/ sources, that would be awesome.Chemisorb
P
85

The issue is that you are returning the item count as 0 which tells that there is no rows to show. You should return the size of the List as shown below.

    @Override
    public int getItemCount() {
        return data.size();
    }
Paphlagonia answered 31/12, 2014 at 12:19 Comment(1)
I had this issue when I update my dataList dataList=getMyItems(). instead I use dataList.addAll(getMyItems());Oversupply
N
37

Replace following line

 mRecyclerView.setAdapter(adapter);
 mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

with

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));    
mRecyclerView.setAdapter(adapter);
Naranjo answered 14/5, 2018 at 9:59 Comment(2)
Hey @mangal can you explain the reasoning for the above logic.Overstrung
it's the same....? no difference...Amena
A
9

I agree that you need to return the correct item number but I think you might have a problem with setting the recycler view's height to wrap_content as well. I found this link very helpful. Hope it helps.

Attainder answered 23/5, 2015 at 6:25 Comment(0)
D
9

Adding app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" to the RelativeLayout solved my problem.

<androidx.recyclerview.widget.RecyclerView
    ...
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    ...
/>
Diaper answered 19/1, 2023 at 8:45 Comment(0)
K
3

Make sure RecyclerView parent container width not set as WrapContent

Kissner answered 7/9, 2020 at 13:44 Comment(1)
As simple as this, excellent answer. I changed to match_parent the recycler and now it appearsPyrenees
V
1
@Override
public int getItemCount() {
    return data.size();
}

and layout height in your row is android:layout_height="match_parent" but must be android:layout_height="wrap_content"

Varini answered 7/12, 2021 at 10:5 Comment(0)
R
1

The common problem is that you might have forgotten to set your recyclerview with adapter

example in kotlin

adapter= CartAdapter(this)
    val linearLayoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
    binding.productCartRecycler.layoutManager = linearLayoutManager
    binding.productCartRecycler.adapter = adapter
Representational answered 15/11, 2022 at 13:0 Comment(0)
B
0

Check for your RecyclerView's width and height attributes, if they are set to wrap content, change it to match parent or constraint your view in some specific area. Also check for Parent view's width attribute, if it's set to wrap content change it to match parent as well.

Bravar answered 16/8, 2021 at 19:25 Comment(0)
M
0

I have given static height and match_parent. It works for my Example I haved added it in nested recyclerview.

<androidx.recyclerview.widget.RecyclerView
                     android:id="@+id/new_store_page_phoneNumber_rv"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:scrollIndicators="bottom"
                        android:fadeScrollbars="false"
                        android:layout_gravity="center_vertical"
                        app:adapter="@{adapter}"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:clickable="true"
                        android:scrollbarAlwaysDrawHorizontalTrack="true"
                        android:scrollbars="none"
                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        android:orientation="horizontal" />
Memoirs answered 28/12, 2021 at 16:46 Comment(0)
C
0

If you are fetching data from the network, make sure you are reloading/refreshing your adapter in the UI thread. This was the solution for me.

Cloddish answered 23/4, 2022 at 5:18 Comment(0)
L
0

Please make sure you use data.size() and wrap_content to the height of the parent layout of the Recycler View.

Libertinism answered 25/9, 2023 at 14:21 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewBlayne

© 2022 - 2024 — McMap. All rights reserved.