[英]How to update fragment data using RecyclerView?
I have a ViewPager
with 12 fragments
.我有一个带有 12 个
fragments
的ViewPager
。 I need to get data from API and update the fragments when scrolling.我需要从 API 获取数据并在滚动时更新片段。 How do I update the contents of fragment, with only one fragment updating for 12 pages?
如何更新fragment的内容,12页只更新一个fragment?
My fragment layout fragment_item:我的片段布局 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:我的 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;
}
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.现在我想在滚动页面时使用相同的片段,但使用不同的文本我怎么能做到这一点......如果可能的话,也使用
RecyclerView
。
You could create a new Class (UpdatingFragment) with public abstract updateView();
您可以使用 public abstract
updateView();
创建一个新类 (UpdatingFragment updateView();
that extends Fragment.扩展片段。
Create your SliderFragment
by extending UpdatingFragment
instead of Fragment通过扩展
UpdatingFragment
而不是 Fragment 来创建您的SliderFragment
In the SliderFragment
in onCreateView()
at the end call updateView()
.在
onCreateView()
中的SliderFragment
中,最后调用updateView()
。
Write an overwrite method in your SliderFragment
for what you need to update (anything you want to show on the UI)在您的
SliderFragment
编写一个覆盖方法,用于您需要更新的内容(您想在 UI 上显示的任何内容)
Adjust your screenAdapter
to return and take UpdatingFragment
's instead of Fragments.调整您的
screenAdapter
以返回并使用UpdatingFragment
而不是 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.现在,每当您的
onCreateView
片段被调用时,它们的所有 UI 数据都会更新,并且如果您创建一个SliderFragments
列表并将其传递给您的适配器而不是创建新的,您只需在该列表上运行 for 循环并调用updateView()
for每个片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.