我有Fragment
RecyclerView
。加载屏幕后,它会自动滚动到RecyclerView
的末尾。如何禁用它?
代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sleep_month, container, false);
tfSegoeui = Typeface.createFromAsset(getResources().getAssets(), "fonts/segoeui.ttf");
tfSegoeuiBold = Typeface.createFromAsset(getResources().getAssets(), "fonts/SEGOEUIB.TTF");
fabAdd = (FloatingActionButton) rootView.findViewById(R.id.fab);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), AddSleepActivity.class);
getActivity().startActivityForResult(i, REQUEST_CODE);
}
});
recyclerCategoryList = new ArrayList<>();
recyclerView = (RecyclerView)rootView.findViewById(R.id.recycler_view);
mAdapter = new RecyclerSleepAdapter(recyclerCategoryList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareListData();
}
private void prepareListData() {
//recyclerCategoryList = new ArrayList<>();
SleepDataModel listData = new SleepDataModel("Fri,Jan 7 11.28PM - 7.00AM",75,"7 Hrs 32 Mins");
recyclerCategoryList.add(listData);
listData = new SleepDataModel("Thu,Jan 6 11.00PM - 5.45AM",30,"6 Hrs 45 Mins");
recyclerCategoryList.add(listData);
listData = new SleepDataModel("Wed,Jan 5 10.50PM - 7.00AM", 60,"8 Hrs 10 Mins");
recyclerCategoryList.add(listData);
listData = new SleepDataModel("Tue,Jan 4 10.00PM - 5.00AM",50, "5 Hrs 30 Mins" );
recyclerCategoryList.add(listData);
listData = new SleepDataModel("Mon,Jan 3 9.50PM - 6.00AM",50, "7 Hrs 15 Mins" );
recyclerCategoryList.add(listData);
mAdapter.notifyDataSetChanged();
}
答案 0 :(得分:1)
这是因为您在设置adaper之后填充了您的datalist,并且recyclelerview正在显示最后添加的行。尝试在prepareListData()
mAdapter = new RecyclerSleepAdapter(recyclerCategoryList);
当然从prepareListData():mAdaper.notifyDataSetChanged
中删除此行,因为此时适配器为空
答案 1 :(得分:0)
在致电recyclerView.smoothScrollToPosition(0);
后尝试添加notifyDataSetChanged()
。