Recyclerview从中间项开始打开而不是从顶部开始。

3
这是我的片段,我在其中调用了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);

        }


    }

}

这是我的xml文件:

<?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>

我希望我的Recycler视图从顶部项开始显示,而不是从中间项开始。有人能告诉我应该在我的代码中做哪些更改吗?提前感谢。
我的意思是这样的:

enter image description here

我希望它是这样的:

enter image description here


“opens with the middle item” 是什么意思?你能包含一张截图吗? - Ryan M
RecyclerView是否在NestedScrollView中? - Jayanth
@Jayanth 不,不是。 - user13006617
@RyanMentley,请检查我的编辑。 - user13006617
3个回答

1
尝试添加


android:descendantFocusability="blocksDescendants"

到recyclerview的父布局

编辑:

我认为问题不在于RecyclerView,而是如果您使用cordinator布局作为根布局,请尝试将您的FrameLayout的边距设置为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

1

您应该将recyclerview的布局宽度和高度设置为match_parent

您的问题实际原因可能是线性布局管理器的reverseLayout。这会导致向下滚动recyclerview。

请改用以下代码:new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false)


你能帮我尝试解决这个问题吗?https://stackoverflow.com/questions/60735312/how-can-i-add-a-search-bar-to-my-recyclerview/60735564?noredirect=1#comment107465829_60735564 - user13006617

0
将此代码添加到您的程序中。
mRecyclerView.smoothScrollToPosition(0);

尝试这个:https://dev59.com/310Z5IYBdhLWcg3wpRhW - Amit pandey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接