我有ViewPager
来阅读数据内容。我在上次使用的项目位置和数据的滚动标记。当我打开MainActivity
时,我将ViewPager
设置为我使用的最后一个位置项。但我不知道如何为RecyclerView
setScrollY滚动上次使用。
如何从RecyclerView
ReadingDataFragment
setScrollY
到MainActivity
获取MainActivity
?
在...
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.layout_main_activity);
List<Data> list = new ArrayList<Data>();
list = ...;
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
CustomDataAdapter adapter = new CustomDataAdapter(getSupportFragmentManager(), list, getApplicationContext());
viewPager.setAdapter(adapter);
//check bookmark data
int positionBookmark = getPositionBookmark();
viewPager.setCurrentItem(positionBookmark);
//check scroll bookmark
int scrollYBookmark = getScrollYBookmark();
//Here, How to get RecyclerView in ReadingDataFragment and set position.
}
...
中(我扩展了AppCompatActivity):
CustomDataAdapter
在...
private List<Data> list;
private Context context;
public CustomDataAdapter(FragmentManager fm, List<Data> list, Context context) {
super(fm);
this.list = list;
this.context = context;
}
@Override
public Fragment getItem(int position) {
return new ReadingDataFragment(context, list.get(position));
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
}
@Override
public int getCount() {
return list.size();
}
...
中(扩展FragmentStatePagerAdapter):
ReadingDataFragment
在...
private Data data;
private Context context;
public ReadingDataFragment(Context context, Data data) {
this.data = data;
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_item_view_data, container, false);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_item);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
rv.setLayoutManager(linearLayoutManager);
ReadingDataAdapter adapter = ....
rv.setAdapter(adapter);
}
...
(我扩展片段):
car_id - car_name
1 - Mercedes C Class
2 - Audi
3 - Volkswagen