Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView doesn't scroll

I am new to android development and I was trying to make a log using RecyclerView and CardView. But the problem that I am facing is that the RecyclerView won't scroll. I did a little research on this issue, but yet couldn't find a way to solve the problem.

Screenshot

This is the code for my RecyclerView,

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.vishistvarugeese.ongc_app.AdminActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/white"
            app:headerLayout="@layout/admin_nav_header"
            app:itemIconTint="@color/black"
            app:itemTextColor="@color/colorAccent"
            app:menu="@menu/admin_menu">

        </android.support.design.widget.NavigationView>

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

</RelativeLayout>

And the code for my CardView is,

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

    <android.support.v7.widget.CardView
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/recentLoginTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/recentLoginCpf"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="40dp"
                android:text="Time"
                android:textColor="@color/black"
                android:textSize="23sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/recentLoginName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="Name"
                android:textColor="@color/colorAccent"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/recentLoginCpf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/recentLoginName"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="16dp"
                android:text="Reg Number"
                android:textColor="@color/pink"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLoginType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/recentLoginCpf"
                android:layout_alignStart="@+id/recentLoginCpf"
                android:layout_below="@+id/recentLoginTime"
                android:layout_marginBottom="10dp"
                android:text="Type"
                android:textColor="@color/pink"
                android:textSize="18sp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>


</LinearLayout>

The java code for RecyclerView,

    private RecyclerView recyclerView;
    private RecyclerView.Adapter recyclerViewAdapter;
    private List<ListItem_RecyclerView_Admin> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        //Recycler View
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        listItems = new ArrayList<>();

        for(int i=0;i<10;i++){
            ListItem_RecyclerView_Admin listItem = new ListItem_RecyclerView_Admin(
                    "9:30",
                    "Name",
                    "808821",
                    "Admin"
            );
            listItems.add(listItem);
       }
        recyclerViewAdapter = new RecyclerViewAdapter(listItems,this);
        recyclerView.setAdapter(recyclerViewAdapter);

The code for the adapter,

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private List<ListItem_RecyclerView_Admin> listItems;
    private Context context;

    public RecyclerViewAdapter(List<ListItem_RecyclerView_Admin> listitems, Context context) {
        this.listItems = listitems;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_items, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListItem_RecyclerView_Admin listItem = listItems.get(position);

        holder.recentLoginName.setText(listItem.getRecentLoginName());
        holder.recentLoginTime.setText(listItem.getRecentLoginTime());
        holder.recentLoginCpf.setText(listItem.getRecentLoginCpf());
        holder.recentLoginType.setText(listItem.getRecentLoginType());

    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView recentLoginName;
        private TextView recentLoginTime;
        private TextView recentLoginCpf;
        private TextView recentLoginType;

        public ViewHolder(View itemView) {
            super(itemView);
            recentLoginName = (TextView) itemView.findViewById(R.id.recentLoginName);
            recentLoginTime = (TextView) itemView.findViewById(R.id.recentLoginTime);
            recentLoginCpf = (TextView) itemView.findViewById(R.id.recentLoginCpf);
            recentLoginType = (TextView) itemView.findViewById(R.id.recentLoginType);
        }
    }
}

Also, I wanted the Date and Recent Logins header to be static and not scroll. I tried to do this by putting them inside a Linear Layout. But I don't think this is the right way to do it. enter image description here

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

Can someone please help me with this issue?

like image 825
Vishist Varugeese Avatar asked Dec 21 '17 10:12

Vishist Varugeese


People also ask

How do you make a recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

Are RecyclerView scrollable?

More about RecyclerView could be found at RecyclerView in Android with Example. RecyclerView lets the users scroll up and down and left and right by setting appropriate orientation via attributes.

How can show all data in RecyclerView without scrolling?

In RecyclerView use android:nestedSrollingEnabled="false" and use NestedScrollView as a parent Scroll View. This was the only answer that worked for me - I had a ScrollView wrapping two RecyclerViews.

Is nestedScrollingEnabled false?

Bug: android:nestedScrollingEnabled="false" causes crash on RecyclerView, yet setNestedScrollingEnabled(false) doesn't.


1 Answers

Put the content of your layout within DrawerLayout

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

        </ FrameLayout>

        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/white"
            app:headerLayout="@layout/admin_nav_header"
            app:itemIconTint="@color/black"
            app:itemTextColor="@color/colorAccent"
            app:menu="@menu/admin_menu">

        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>
like image 176
Paresh P. Avatar answered Oct 17 '22 11:10

Paresh P.