请帮帮我解决?
我有Activity
ViewPager
和RecyclerViewFragment
个,RecyclerFragment
每个都包含动态CardView
。
ViewPager
适配器的代码:
public class CustomFragmentPagerAdapter extends FragmentStatePagerAdapter {
private Board board;
private long boardID;
public CustomFragmentPagerAdapter(FragmentManager fm, Board board, long id) {
super(fm);
this.board = board;
this.boardID = id;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return board.getMyLists().get(position).getName();
}
//Here I define fragment, int position - is a position of Fragment in ViewPager
@Override
public Fragment getFragment( int position) {
**return RecyclerViewFragment.newInstance(boardID, position);**
}
}
接下来是片段的新实例:
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
args.putInt(BUNDLE_LIST_POSITION, listPosition);
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
我通过Bundle存储 Fragment的位置 - listPosition
然后我通过getArguments()
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
所以问题是,当我从getArguments()获得int-Position时,它有下一个序列 - 1,2,3,4,0。但它按此顺序写入 - 0,1,2,3,4
您可能认为这并不重要,但Realm ORM确实可以逆转。当我从DB获得有关动态CardViews的信息时,Realm从第0个片段开始,而getArguments()从1开始。那个东西抛出
ArrayIndexOutOfBoundsException异常
[更新] 我将Log.d添加到newInstance和OnCreateView
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
Log.d(TAG, listPosition + " Before put");
args.putInt(BUNDLE_LIST_POSITION, listPosition);
Log.d(TAG, listPosition + " After put");
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
tinyDB = new TinyDB(getContext());
boardPosition = tinyDB.getInt("boardPosition");
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
Log.d(TAG, listPosition + " Get ");
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
在LogCat中我接下来:
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 After put
06-12 00:37:46.190 25265-25265/com.treggo.flexible D/mLogs: 1 Get
06-12 00:37:46.266 25265-25265/com.treggo.flexible D/mLogs: 2 Get
06-12 00:37:46.322 25265-25265/com.treggo.flexible D/mLogs: 3 Get
06-12 00:37:46.397 25265-25265/com.treggo.flexible D/mLogs: 4 Get
06-12 00:37:46.452 25265-25265/com.treggo.flexible D/mLogs: 0 Get
P.S。为了绕过Exception,我设置了9个CardViews,而不是从Realm
中读取它答案 0 :(得分:0)
虽然我不知道如何实现列表位置的索引,但我认为它从1开始,而不是从0开始。
如果是这种情况,请在newInstance()
方法中替换以下行:
args.putInt(BUNDLE_LIST_POSITION, listPosition);
用这个:
args.putInt(BUNDLE_LIST_POSITION, listPosition - 1);