这是我称之为回收者视图的片段:
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视图从顶部项目开始显示,而不是从中间项目开始显示。有人能告诉我我应该对我的代码进行哪些更改吗?提前感谢。
我的意思是说,它是这样的:
我希望它是这样的:
您应该将回收视图的布局宽度和高度设置为match_parent
。
问题的实际原因可能是您的线性布局管理器的backseLayout。这具有向下滚动回收器视图的效果。
改用这个:new LinearLayoutManager(getContext(), Recycliview.VERTAL,false)
尝试添加
android:descendantFocusability="blocksDescendants"
到回收视图父布局
编辑:
我认为这不是一个问题与回收视图
相反,如果你使用一个协调器布局作为根布局,尝试给你的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>
将此添加到您的代码中
mRecyclerView.smoothScrollToPosition(0);