我正在使用随viewPager附加的tabLayout。我使用FragmentStatePager适配器实现了viewPager适配器,因为tabLayout列表很大。
ViewPager创建每个片段的新实例,在片段内部,我使用了recyclelerView来处理数据。
现在,在启动viewPager之前,我正在调用服务器,并且在成功响应中,我是第一次启动viewPager。然后viewPager调用它的getItem()回调,它获取第一个位置的数据初始化片段及它的recyclelerView。
它还为下一个位置调用getItem(),因为我已将offScreenLimit()设置为2,这很好并且运行良好。
问题是当它调用getItem()并创建片段时,那时我正在检查我是否有数据,如果没有,那么我正在进行回调以从服务器获取数据。
如果用户访问该位置,我想更新片段recyclerView。
这里,我已经传递了片段中主对象的引用,所以在请求成功时,它会更新主对象,更新后的对象将出现在片段列表中。
因为,它是一个异步调用,我想显示一个加载器,然后更新片段。
这就是我的问题所在。我无法更新片段recyclerView。它正在更新,但需要时间。
我曾尝试使用返回 POSITION_NONE 但效果不错,但这不是一个好方法,因为它每次都会创建一个新片段。
这是我的viewPagerAdapter。
public class ShowTimeViewPageAdapter extends FragmentStatePagerAdapter {
private static final String TAG = ShowTimeViewPageAdapter.class.getSimpleName();
private Context mContext;
private List<String> mDateStringList;
private FragmentManager fragmentManager;
private IShowTimeActivity mIShowTimeActivity;
private List<String> mDateCodes;
private LinkedHashMap<String, List<Venue>> mVenueHashMap;
public ShowTimeViewPageAdapter(FragmentManager fm, Context context, IShowTimeActivity showTimeActivity, List<String> createdDates, List<String> dateCodes, LinkedHashMap<String, List<Venue>> venueHashMap) {
super(fm);
this.mContext = context;
this.mIShowTimeActivity = showTimeActivity;
this.fragmentManager = fm;
this.mDateCodes = dateCodes;
this.mVenueHashMap = venueHashMap;
this.mDateStringList = createdDates;
}
private String getDateCode(int pos) {
String dateCode = null;
try {
dateCode = mDateCodes.get(pos);
} catch (IndexOutOfBoundsException e) {
Logger.d(TAG, "Index out of bound");
}
return dateCode;
}
private List<Venue> getVenuesList(String dateCode) {
return mVenueHashMap.get(dateCode);
}
@Override
public Fragment getItem(int position) {
if (getDateCode(position) != null) {
if (getVenuesList(getDateCode(position)) == null) {
if(position!=0){
mIShowTimeActivity.requestForTheVenueListByDateCode(getDateCode(position), position);
}
}
}
ShowTimeFragment showTimeFragment = ShowTimeFragment.newInstance(mDateStringList.get(position).split(";")[2]);
fragmentManager.beginTransaction().add(showTimeFragment,""+position);
return showTimeFragment;
}
@Override
public int getCount() {
return mDateStringList.size();
}
public View getTabView(int position) {
String[] dateStr = mDateStringList.get(position).split(";");
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View tabView = layoutInflater.inflate(R.layout.show_time_tab_layout, null, false);
TextViewRoboto day = (TextViewRoboto) tabView.findViewById(R.id.show_time_tab_day);
TextViewRoboto date = (TextViewRoboto) tabView.findViewById(R.id.show_time_tab_date);
day.setText(dateStr[0]);
date.setText(dateStr[1]);
return tabView;
}
}
这就是碎片。
public class ShowTimeFragment extends Fragment {
@Bind(R.id.show_time_fragment_recycler_view) RecyclerView mShowTimeRecyclerView;
@Bind(R.id.show_time_fragment_no_data_text_view) TextViewRoboto mShowTimeNoDataTextView;
private static LinkedHashMap<String, List<Venue>> mVenueList = ShowTimeActivity.mVenueHashMap;
private ShowTimeRecyclerViewAdapter mShowTimeRecyclerViewAdapter;
private static final String KEY_CODE = "date_key";
public ShowTimeFragment() {
}
public static ShowTimeFragment newInstance(String dateCode) {
Bundle bundle = new Bundle();
bundle.putString(KEY_CODE, dateCode);
ShowTimeFragment showTimeFragment = new ShowTimeFragment();
showTimeFragment.setArguments(bundle);
return showTimeFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.show_time_fragment, container, false);
ButterKnife.bind(this, view);
String code = getArguments().getString(KEY_CODE);
List<Venue> venueList = mVenueList.get(code);
if (venueList != null && venueList.size() > 0) {
mShowTimeRecyclerView.setVisibility(View.VISIBLE);
mShowTimeNoDataTextView.setVisibility(View.GONE);
mShowTimeRecyclerViewAdapter = new ShowTimeRecyclerViewAdapter(venueList, getContext());
mShowTimeRecyclerView.setAdapter(mShowTimeRecyclerViewAdapter);
CustomLinearLayoutManager cl = new CustomLinearLayoutManager(getActivity(), 1, false);
mShowTimeRecyclerView.setLayoutManager(cl);
RecyclerView.OnItemTouchListener disabler = new RecyclerViewDisabler();
mShowTimeRecyclerView.addOnItemTouchListener(disabler); // disables scrolling
} else {
mShowTimeNoDataTextView.setVisibility(View.VISIBLE);
mShowTimeRecyclerView.setVisibility(View.GONE);
mShowTimeNoDataTextView.setText("NO Data Available");
}
return view;
}
public class RecyclerViewDisabler implements RecyclerView.OnItemTouchListener {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return true;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
protected void addFragment(int containerViewId, Fragment fragment, String fragmentTag) {
getActivity().getSupportFragmentManager().beginTransaction().add(containerViewId, fragment, fragmentTag).disallowAddToBackStack().commit();
}
public void onDestroyView() {
super.onDestroyView();
}
}
在上面的片段中
private static LinkedHashMap<String, List<Venue>> mVenueList = ShowTimeActivity.mVenueHashMap;
这个hashMap是对原始hashMap的引用,它在每个请求上都会更新。
任何形式的帮助或建议都将受到赞赏。