3

This is my fragment where I have called the recycler view:

public class sgpa_frag extends Fragment {
    //View view;
    RecyclerView recyclerview;
    adapter_sgpa ac;


    ArrayList<POJO> sgpaArrayList;

    public sgpa_frag() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sgpa_frag, container, false);
        recyclerview= (RecyclerView) view.findViewById(R.id.rc1);
        sgpaArrayList= new ArrayList<>();
        ac= new adapter_sgpa(sgpaArrayList);
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,true));
        recyclerview.setAdapter(ac);

        Fetchdata1();

        return view;
    }

    private void Fetchdata1()
    {
        dbmanager db= new dbmanager(getContext());

        Cursor cursor= db.fetch_data1();

        if (cursor!= null){

           // cursor.moveToFirst();

            while (cursor.moveToNext()){

                POJO pj= new POJO();
                pj.setSname(cursor.getString(0));
                pj.setSemester(cursor.getString(1));
                pj.setSgpa(cursor.getString(2));
                pj.setPercent(cursor.getString(3));
                pj.setSchemes(cursor.getString(4));
                sgpaArrayList.add(pj);
            }

            ac= new adapter_sgpa(sgpaArrayList);

        }


    }

}

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<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=".drawernav.bottom_navi.recycler_view.sgpa_frag">

    <!-- TODO: Update blank fragment layout -->

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rc1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="60dp">
    </androidx.recyclerview.widget.RecyclerView>

</FrameLayout>

I want my Recycler view to start displaying from the top item and not from the middle item . Can anyone tell me what changes should I make in my code ? Thanks in advance .

I mean to say that, it is like this:

enter image description here

And I want it to be like this:

enter image description here

3 Answers3

1

Try adding

android:descendantFocusability="blocksDescendants"

to the recyclerview parent layout

Edit:

I think it is not a problem with RecyclerView instead if you are using a cordinator layout as root layout, try giving margin to your FrameLayout of 60

<!-- TODO: Update blank fragment layout -->

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rc1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop ="60dp" //approx for toolbar height
    android:paddingBottom="60dp">
</androidx.recyclerview.widget.RecyclerView>

Jayanth
  • 5,954
  • 3
  • 21
  • 38
1

You should set layout width and height of the recyclerview to match_parent.

The actual reason for your issue is probably the reverseLayout of your your linear layout manager. This has the effect as scrolling down the recyclerview.

Use this instead: new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false)

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Can you please try helping me with this as well? https://stackoverflow.com/questions/60735312/how-can-i-add-a-search-bar-to-my-recyclerview/60735564?noredirect=1#comment107465829_60735564 –  Mar 18 '20 at 13:40
0

Add this into you code

mRecyclerView.smoothScrollToPosition(0);
Amit pandey
  • 1,149
  • 1
  • 4
  • 15
  • try this https://stackoverflow.com/questions/31235183/recyclerview-how-to-smooth-scroll-to-top-of-item-on-a-certain-position – Amit pandey Mar 18 '20 at 07:09