У меня есть ViewPager
с 12 fragments
.Мне нужно получить данные из API и обновить фрагменты при прокрутке.Как обновить содержимое фрагмента, обновив только один фрагмент для 12 страниц?
Макет моего фрагмента фрагмента:
<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" />
Код моей основной деятельности:
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;
}
}
Мой класс SlideFragment:
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;
}
Теперь я хочу иметь тот же фрагмент при прокрутке страницы, но с разными текстами, как я могу это сделать ... И, если возможно, использовать RecyclerView
.