fragmentstatepageradapter

FragmentPagerAdapter.notifyDataSetChanged()不能更新问题?

落花浮王杯 提交于 2019-12-05 23:33:04
在一个 Android 应用中,我使用 FragmentPagerAdapter 来处理多 Fragment 页面的横向滑动。不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我希望能够通过调用 mAdapter.notifyDataSetChanged() 来触发 Fragment 页面使用新的数据调整或重新生成其内容,可是当我调用 notifyDataSetChanged() 后,发现什么都没发生。 搜索之后发现不止我一个人碰到这个问题,大家给出的解决办法五花八门,有些确实解决了问题,但是我总感觉问题没搞清楚。于是我决定搞明白这个问题到底是怎么回事,以及正确的用法到底如何。要搞明白这个问题,仅仅阅读文档并不足够,还需要阅读相关几个类的相关方法的实现,搞懂其设计意图。下面就是通过阅读源代码搞明白的内容。 【ViewPager】 ViewPager 如其名所述,是负责翻页的一个 View。准确说是一个 ViewGroup ,包含多个 View 页,在手指横向滑动屏幕时,其负责对 View 进行切换。为了生成这些 View 页,需要提供一个 PagerAdapter 来进行和数据绑定以及生成最终的 View 页。 setAdapter() ViewPager 通过 setAdapter() 来建立与 PagerAdapter 的联系。这个联系是双向的,一方面

Dynamically skip pages in ViewPager

≡放荡痞女 提交于 2019-12-05 16:04:07
问题 I got a ViewPager which holds Fragment s via FragmentStatePagerAdapter . Let's say the pager initially holds the following pages ( Fragment s): A - B - C - D When the user swipes, he can move from A to B , B to C etc. But there are cases when the user changes some options on the A page, he can move not to B , but C : A - C - D Then the user goes back to A , modifies something and that re-enables B : A - B - C - D How can i achieve this very dynamic behavior? I cannot add Fragment s any time

FragmentManager is already executing transactions

那年仲夏 提交于 2019-12-05 11:26:42
So I have this problem when using the ViewPager with a FragmentStatePagerAdapter AND then I'm trying to select the current page (i.e. show the second page to the user instead of the first one) that I get this exception from time to time. The issue seems to be concurrency, as the viewpager is still adding/initializing the fragments i.e. currently running a fragment transaction while I'm trying to set the current page. Something like this: pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager()); viewPager.setAdapter(pagerAdapter); pagerAdapter.addItems(...) //Addings ID's here for

Fragement no longer exists for key f0: index 1

ぐ巨炮叔叔 提交于 2019-12-05 09:39:23
I had a nice a pretty ViewPager/FragmentStatePagerAdapter working on my app but after manually adding a Fragment to the FragmentManager I started getting this exception when rotating the screen TWICE . StackTrace: 07-29 19:10:22.360: E/FragmentManager(712): Fragement no longer exists for key f0: index 1 07-29 19:10:22.360: E/FragmentManager(712): Activity state: 07-29 19:10:22.407: D/FragmentManager(712): Active Fragments in 4608be98: 07-29 19:10:22.410: D/FragmentManager(712): #0: RESTFragment{46005fb0 #0 RESTFragment} 07-29 19:10:22.410: D/FragmentManager(712): mFragmentId=#0 mContainerId=#0

ViewPager not getting updated using FragmentStatePagerAdapter

纵然是瞬间 提交于 2019-12-05 07:24:14
I've a two fragments , Fragment A and Fragment B . Fragment A lists all products and Fragment B shows detail about the product and its images. Fragment A calls Fragment B and Fragment B fetches data from web service and sets to ViewPager using Adapter . Initially it displays correct image but thereafter it always have the same image. If we fire adapter.notifyDataSetChanged() from any click event in Fragment B then it works perfectly but we have to display as soon as Fragment B is visible Please look at the code. public class ImageAdapter extends FragmentStatePagerAdapter { private List<String>

How to create a carousel using viewpager and fragmentStatePager Adapter

十年热恋 提交于 2019-12-05 03:42:17
问题 I am new to android and i am trying to create a carousel in android. My class structure are as follows public class PageViewActivity extends FragmentActivity { ViewPager pager; ... public void onCreate(Bundle savedInstanceState) { final ViewPager pager = (ViewPager) findViewById(R.id.pager); MyPageAdapter pageAdapter = new MyPageAdapter(getSupportFragmentManager(),getBaseContext()); pager.setAdapter(pageAdapter); } ... public class MyPageAdapter extends FragmentStatePagerAdapter { @Override

ViewPager first fragment shown is always wrong with FragmentStatePager

拜拜、爱过 提交于 2019-12-05 03:17:36
I am trying to have the same view pager + tabs design as the PlayStore 5.1.x. Here is my layout : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal" android:orientation="vertical"> <com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/background_tabs" />

ViewPager inside RecyclerView as row item

笑着哭i 提交于 2019-12-04 11:44:47
问题 I need to have an ViewPager (similar to an horizontal gallery) inside an RecyclerView which display an list vertically. Each row of the RecyclerView will have an ViewPager which will allow to swipe between some images. The ViewPager will also support click events which will be propagated to the parent RecyclerView. Currently, I have the following implementation: List adapter: @Override public void onBindViewHolder(MyHolder holder, int position) { super.onBindViewHolder(holder, position); Item

Changing background image of current fragment in viewpager

江枫思渺然 提交于 2019-12-04 09:58:41
I have 4 fragments in a viewpager. And FragmentStatePagerAdapter inside the activity where my viewpager is. FragmentStatePagerAdapter's newInstance() method takes as parameter layout id, therefore each fragment has it's own layout: ViewPager pager; public class MainActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.viewPager); pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));} private static class MyPagerAdapter

Android - Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference

妖精的绣舞 提交于 2019-12-04 05:40:52
Here is the full stack trace of the error: http://pastebin.com/5dQHkUw3 The error started to appear only after I started using fragments in the project, because I want to give JSON feeds. It was fine before when I was creating Tabs, NavigationDrawer etc. MainActivity.java public class MainActivity extends ActionBarActivity implements MaterialTabListener{ private Toolbar mToolbar; private NavigationDrawerFragment mDrawerFragment; private DrawerLayout mDrawerLayout; private MaterialTabHost mTabHost; private ViewPager mViewPager; public static final int SERVICES_ALL = 1; public static final int