我试过这个,但是错误的列表被绑定到了recyclerview。我也尝试过SparseArray,即使这样也行不通。当我启动Mainactivity时,getItem()被调用两次。我如何处理返回的位置? 我尝试返回viewpager currentItem,即使这不起作用。
MainActivity
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(android.support.v4.app.FragmentManager fragmentManager, int tabCount)
{
super(fragmentManager);
this.mNumOfTabs = tabCount;
}
@Override
public Fragment getItem(int position) {
return Categories_Fragment.newInstance(position);
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Categories_Fragment
public class Categories_Fragment extends Fragment
{
private RecyclerView lvMessages;
private int fromPage;
public static Categories_Fragment newInstance(int num) {
Categories_Fragment f = new Categories_Fragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("from", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
fromPage = getArguments().getInt("from");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.categories_filter, container, false);
lvMessages = (RecyclerView)rootView.findViewById(R.id.lv_categories_filter);
LinearLayoutManager x = new LinearLayoutManager(getActivity());
x.setOrientation(LinearLayoutManager.VERTICAL);
lvMessages.setLayoutManager(x);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// bind different data to the RecyclerView based on fromPage value.
}
}
答案 0 :(得分:2)
这可能是踢球者......
@Override
public void setUserVisibleHint(boolean isVisible) {
super.setUserVisibleHint(isVisible);
if (isVisible) {
// bind to stuff using value of fromPage from here
}
}
public void setUserVisibleHint (boolean isVisibleToUser):向系统提供有关此片段的UI当前是否对用户可见的提示。此提示默认为true,并且在片段实例状态保存和恢复期间保持不变。
可能是什么问题(如果我正确地理解你的问题)是寻呼机正在传递它的“最后已知”位置,这并不一定意味着当前正在显示的片段的位置设备屏幕。
更新:这是一个完整的示例,其中ViewPager处理相同的片段但传递给它的数据不同:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private JSONArray itemsArray;
private int fragmentId;
public ViewPagerAdapter(JSONArray itemsArray, int fragmentId, FragmentManager manager) {
super(manager);
this.itemsArray = itemsArray;
this.fragmentId = fragmentId;
}
@Override
public Fragment getItem(int position) {
JSONObject itemObject = new Listing(itemsArray, position).getObject();
Bundle bundle = new Bundle();
bundle.putString("item_object", itemObject.toString());
bundle.putInt("item_position", position);
bundle.putInt("total_items", getCount());
bundle.putInt("fragment_Id", fragmentId);
Fragment fragment = new ListingFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return itemsArray.length();
}
}
这是清单片段(减去不重要的位):
public class ListingFragment extends Fragment {
private Listing listing;
private String itemObject;
private int itemPosition;
private int totalItems;
private int fragmentId;
public ListingFragment() { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
itemObject = bundle.getString("item_object");
itemPosition = bundle.getInt("item_position");
totalItems = bundle.getInt("total_items");
fragmentId = bundle.getInt("fragment_Id");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = ...
// nothing special to see here, just the usual stuff
return view;
}
@Override
public void setUserVisibleHint(boolean isVisible) {
super.setUserVisibleHint(isVisible);
if (isVisible) {
// send fragmentID to wherever you want to use it
// such as your recyclerview/adapter from here
}
}
}
我希望这个例子有所启发,我知道这对我有用。
答案 1 :(得分:0)
您怀疑是否创建了一个名为 PagerAdapter 的类,这是一个Android抽象类。将其重命名为MyPagerAdapter等唯一名称。
答案 2 :(得分:0)
两次调用getItem()的原因是因为 ViewPager 中分配给 offScreenPageLimit 的默认值。
您可以使用 ViewPager 类的 setOffscreenPageLimit(int limit)方法将 offScreenPageLimit 设置为1或0,这样就可以解决您的问题两次调用getItem()。
因此,如果 getItem()被调用一次,那么您将在片段中获得正确的位置值,您的问题将得到解决。
签出方法定义和描述的链接
答案 3 :(得分:0)
使用FragmentStatePagerAdapter
,HashMap<Integer,Demo_Fragment>
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
List<String> mList;
public ViewPagerAdapter(FragmentManager fm, List<String> list) {
super(fm);
mList = list;
}
@Override
public CharSequence getPageTitle(int position) {
String title = mList.get(position);
return title;
}
public int getCount() {
return mList.size();
}
public Fragment getItem(int position) {
Demo_Fragment fragment = new Demo_Fragment();
Bundle bundle = new Bundle();
bundle.putString("text",mList.get(position));
fragment.setArguments(bundle);
fragment.setTags(position);
mPageReferenceMap.put(position, fragment);
return fragment;
}
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
mPageReferenceMap.remove(position);
}
public int getItemPosition(Object item) {
Demo_Fragment fragment = (Demo_Fragment) item;
int position = fragment.getTags();
if (position >= 0) {
return position;
} else {
return POSITION_NONE;
}
}
}
获取片段
public Demo_Fragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
获取addOnPageChangeListener ViewPager
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
final Demo_Fragment demo_Fragment = getFragment(mViewPager.getCurrentItem());
final String str = mArrString.get(mViewPager.getCurrentItem());
demo_Fragment.updateView(str);
showDialog();
mRunnable = new Runnable() {
@Override
public void run() {
randomGenerator = new Random();
randomInt = randomGenerator.nextInt(10000);
demo_Fragment.updateView(str +"\n" +randomInt+"-Updated Value");
hideDialog();
}
};
mHandler.postDelayed(mRunnable, 2000);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
工作示例 - Click Here