Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - I have a ViewPager by same fragments RecyclerView just load items in one fragment of ViewPager

i have a viewPager with fragments when i want to load items in RecyclerView ,the items just load in first fragments RecyclerView and not loading in other but the textviews are loading in all fragments and when i go back to previous item again items load in recyclerView of that fragment

I see some of the solutions and i make all my recyclerView Adapters view not static but again it not worked

and i made other fragment by different name and different layout name and different id for its recyclerView and it work but i have a large number of fragments and it is not the solution for me ):

public class ShowInfo extends Fragment {

 private static ViewPager mPager;
private static int currentPage = 0;
View view;
RecyclerView rv;
RecyclerView rv1;
ImageView lastImage;
ImageView nextImage;
TextView imageStatus;
TextView title;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_item_scrolling, container, false);
    String strtext = getArguments().getString("title","");
    title = v.findViewById(R.id.textView);
    title.setText(strtext);
    return v;
}
private static final Integer[] XMEN= {R.drawable.testimage,R.drawable.slide,R.drawable.testimage,R.drawable.slide};
private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    init();
    RVAdapter_item rvAdapter = new RVAdapter_item(initializeData());
    rv.setAdapter(rvAdapter);
    RVAdapter1_item rvAdapter1 = new RVAdapter1_item(initializeData());
    rv1.setAdapter(rvAdapter1);
    imageStatus.setText(getTextString(0));
    rvAdapter.notifyDataSetChanged();
    rvAdapter1.notifyDataSetChanged();
}
private void init() {
    LinearLayout linearLayout = getActivity().findViewById(R.id.linear);
    rv = (RecyclerView) linearLayout.getChildAt(2);
    imageStatus = getActivity().findViewById(R.id.imageStatus);
    lastImage = getActivity().findViewById(R.id.lastImage);
    nextImage = getActivity().findViewById(R.id.nextImage);
    rv.setHasFixedSize(true);
    rv.setLayoutManager( new GridLayoutManager(getActivity(),1));
    rv1 = getActivity().findViewById(R.id.rv1);
    rv1.setHasFixedSize(true);
    rv1.setLayoutManager( new GridLayoutManager(getActivity(),2));
}

private ArrayList<Data> initializeData() {
    ArrayList<Data> qorans = new ArrayList<>();
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    qorans.add(new Data("","",""));
    return qorans;
}

public String getTextString(int i){
    if (i==0){
        lastImage.setVisibility(View.INVISIBLE);
    }else {
        lastImage.setVisibility(View.VISIBLE);
    }
    if (i+1==XMENArray.size()){
        nextImage.setVisibility(View.INVISIBLE);
    }else {
        nextImage.setVisibility(View.VISIBLE);
    }
    return "??? "+" "+Integer.toString(i+1)+" "+"??"+" "+Integer.toString(XMENArray.size());
}

}

RecyclerAdaptor

public class RVAdapter_item extends RecyclerView.Adapter<RVAdapter_item.QoranViewHolder> {
private SpringyAdapterAnimator mAnimator;
 class QoranViewHolder extends RecyclerView.ViewHolder {
    QoranViewHolder(final View itemView) {

        super(itemView);
        final Context context;



    }
}

List<Data> qoranList;

public RVAdapter_item(List<Data> qoren){
    this.qoranList = qoren;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    mAnimator = new SpringyAdapterAnimator(recyclerView);
    mAnimator.setSpringAnimationType(SpringyAdapterAnimationType.SCALE);
    mAnimator.addConfig(85,15);
}
@Override
public QoranViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_info, viewGroup, false);
   QoranViewHolder pvh = new QoranViewHolder(v);
    mAnimator.onSpringItemCreate(v);
    return pvh;
}
@Override
public void onBindViewHolder(QoranViewHolder personViewHolder, int i) {
    mAnimator.onSpringItemBind(personViewHolder.itemView, i);
}
@Override
public int getItemCount() {
    return  qoranList.size();
}

}

ViewPager Activity

public class ShowInfoViewPager extends AppCompatActivity {

List<Fragment> fragments = new ArrayList<>();
ArrayList<Data> list;
int num = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_showinfo_viewpager);
    list = MainActivity.initializeData();
    for (int i=0;i<list.size();i++) {
        Bundle bundle = new Bundle();
        bundle.putString("title", list.get(i).category_title);
        ShowInfo firstFragment = new ShowInfo();
        firstFragment.setArguments(bundle);
        fragments.add(firstFragment);
    }
    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    ZoomOutTransformation zoomOutTransformation = new ZoomOutTransformation();
    pager.setPageTransformer(true, zoomOutTransformation);
    final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(adapter);
}

private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
        return fragments.get(pos);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

move below lines code to "onResume:" method of the fragment.

RVAdapter_item rvAdapter = new RVAdapter_item(initializeData());
rv.setAdapter(rvAdapter);
RVAdapter1_item rvAdapter1 = new RVAdapter1_item(initializeData());
rv1.setAdapter(rvAdapter1);
imageStatus.setText(getTextString(0));
rvAdapter.notifyDataSetChanged();
rvAdapter1.notifyDataSetChanged();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...