我有一个由相同片段组成的ViewPager RecyclerView只是在ViewPager的一个片段中加载项目

时间:2018-11-04 11:04:31

标签: java android android-recyclerview

当我想在RecyclerView中加载项目时,我有一个带有片段的viewPager,这些项目仅在RecyclerView的第一个片段中加载,而在其他片段中不加载,但是textview在所有片段中都加载,当我回到上一个项目时再次加载在该片段的recyclerView中

我看到了一些解决方案,并且使我所有的recyclerView Adapters视图都不是静态的,但再次无法正常工作

我用其recyclerView用不同的名称,不同的布局名称和不同的ID制作了另一个片段,它可以工作,但是我有很多片段,这对我来说不是解决方案):

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活动

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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

  

将以下行代码移动到片段的“ onResume:”方法。

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();