I have a ViewPager
with 12 fragments
. I need to get data from API and update the fragments when scrolling. How do I update the contents of fragment, with only one fragment updating for 12 pages?
My fragment layout fragment_item:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/surface">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/khaire"
android:gravity="center"
android:text="Overal Working Hour"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="15dp"
android:text="January"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
My main activity code:
public class MainActivity extends AppCompatActivity {
private static final int num_pages = 12;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = findViewById(R.id.pager);
mPagerAdapter = new ScreenAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
private class ScreenAdapter extends FragmentStatePagerAdapter {
public ScreenAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return new SliderFragment();
}
@Override
public int getCount() {
return num_pages;
}
}
My SlideFragment class:
public class SliderFragment extends Fragment {
Toolbar toolbar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_item,container,false);
return rootView;
}
Now I want to have same fragment while scrolling the page but with diff texts how could I do that... And if possible use RecyclerView
also.
You could create a new Class (UpdatingFragment) with public abstract updateView();
that extends Fragment.
Create your SliderFragment
by extending UpdatingFragment
instead of Fragment
In the SliderFragment
in onCreateView()
at the end call updateView()
.
Write an overwrite method in your SliderFragment
for what you need to update (anything you want to show on the UI)
Adjust your screenAdapter
to return and take UpdatingFragment
's instead of Fragments.
Now whenever your fragments onCreateView
gets called all their UI data gets updated, and if you create a list of SliderFragments
and pass that into your adapter instead of creating new ones, you can just run a for loop over that list and call updateView()
for each fragment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.