视图寻呼机出错,在所有标签中显示相同的数据?

时间:2015-09-06 12:56:54

标签: android android-fragments android-viewpager

我正在使用此库:https://github.com/florent37/MaterialViewPager

我正在使用View Pager片段在周一到周五的不同标签中显示我的大学的时间表。

当在NavDrawer列表中单击某个项目时,

我的视图寻呼机处于片段中。所以我从数据库(sql)获取数据并创建5 List<>并获取相应的数据,并在片段加载时:

我得到的错误是,当我打开片段时,我会在所有标签中获得星期五的时间表。

FragmentClass:

public class MaterialVPFrag extends Fragment {
private MaterialViewPager mViewPager;
List<Model_Daywise> todayslist_m;
List<Model_Daywise> todayslist_t;
List<Model_Daywise> todayslist_w;
List<Model_Daywise> todayslist_th;
List<Model_Daywise> todayslist_fr;

public static MaterialVPFrag newInstance() {
    return new MaterialVPFrag();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.mvp_layout, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    todayslist_m = new MTWTHgetset(getActivity(), "monday").getAllCredentials();
    todayslist_t = new MTWTHgetset(getActivity(), "tuesday").getAllCredentials();
    todayslist_w = new MTWTHgetset(getActivity(), "wednesday").getAllCredentials();
    todayslist_th = new MTWTHgetset(getActivity(), "thursday").getAllCredentials();
    todayslist_fr = new MTWTHgetset(getActivity(), "friday").getAllCredentials();
    mViewPager = (MaterialViewPager) view.findViewById(R.id.materialViewPager);
    mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    Log.d("c0", "");
                    return new RecyclerViewFragment(todayslist_t);
                case 1:
                    Log.d("c1", "");
                    return new RecyclerViewFragment(todayslist_t);
                case 2:
                    Log.d("c2", "");
                    return new RecyclerViewFragment(todayslist_w);
                case 3:
                    Log.d("c3", "");
                    return new RecyclerViewFragment(todayslist_th);
                case 4:
                    Log.d("c4", "");
                    return new RecyclerViewFragment(todayslist_fr);
                default:
                    Log.d("cdef", "");
                    return new RecyclerViewFragment(todayslist_m);
            }
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Monday";
                case 1:
                    return "Tuesday";
                case 2:
                    return "Wednesday";
                case 3:
                    return "Thursday";
                case 4:
                    return "Friday";
            }
            return "";
        }
    });
    mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
        @Override
        public HeaderDesign getHeaderDesign(int page) {
            switch (page) {
                case 0:
                    return HeaderDesign.fromColorResAndUrl(
                            R.color.green,
                            "https://fs01.androidpit.info/a/63/0e/android-l-wallpapers-630ea6-h900.jpg");
                case 1:
                    return HeaderDesign.fromColorResAndUrl(
                            R.color.blue,
                            "http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2014/06/wallpaper_51.jpg");
                case 2:
                    return HeaderDesign.fromColorResAndUrl(
                            R.color.cyan,
                            "http://www.droid-life.com/wp-content/uploads/2014/10/lollipop-wallpapers10.jpg");
                case 3:
                    return HeaderDesign.fromColorResAndUrl(
                            R.color.red,
                            "http://www.tothemobile.com/wp-content/uploads/2014/07/original.jpg");
            }

            //execute others actions if needed (ex : modify your header logo)

            return null;
        }
    });

    mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount()); //it works without it don't know why!
    mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());

    View logo = view.findViewById(R.id.logo_white);
    if (logo != null)
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.notifyHeaderChanged();
                Toast.makeText(getActivity(), "Yes, the title is clickable", Toast.LENGTH_SHORT).show();
            }
        });

}
}

RecyclerViewFragment.java

public class RecyclerViewFragment extends Fragment {

private RecyclerView mRecyclerView;
public static RecyclerView.Adapter mAdapter;
List<Model_Daywise> list;

public RecyclerViewFragment() {
}

public RecyclerViewFragment(List<Model_Daywise> lis) {
    list = lis;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);
    mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter_Daywise(list, getActivity()));
    mRecyclerView.setAdapter(mAdapter);
    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
}

CardAdapter_Daywise.java

public class CardAdapter_Daywise extends   RecyclerView.Adapter<CardAdapter_Daywise.CardViewHolderDaywise> {
private static List<Model_Daywise> cardList;
private Context context = null;

public CardAdapter_Daywise(List<Model_Daywise> list, Context context) {
    this.cardList = list;
    this.context = context;
}


@Override
public CardViewHolderDaywise onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout_daywise, viewGroup, false);
    return (new CardViewHolderDaywise(itemView));

}

@Override
public void onBindViewHolder(final CardAdapter_Daywise.CardViewHolderDaywise cardViewHolder, int i) {
    final Model_Daywise cl = cardList.get(i);
    cardViewHolder.subname.setText(cl.getSubname());
    cardViewHolder.subcode.setText(cl.getSubcode());
    cardViewHolder.slot.setText(cl.getSubslot());
    cardViewHolder.timings.setText(cl.getSubtimings());
    cardViewHolder.teacher.setText(cl.getSubteacher());
    cardViewHolder.venue.setText(cl.getVenue());
}

@Override
public int getItemCount() {
    return cardList.size();
}


public class CardViewHolderDaywise extends RecyclerView.ViewHolder {
    TextView subname, subcode, slot, venue, teacher, timings;

    public CardViewHolderDaywise(View v) {
        super(v);
        subname = (TextView) v.findViewById(R.id.subject_name_daywise);
        subcode = (TextView) v.findViewById(R.id.sub_code_daywise);
        slot = (TextView) v.findViewById(R.id.slot_daywise);
        venue = (TextView) v.findViewById(R.id.venue_daywise);
        teacher = (TextView) v.findViewById(R.id.teacher_daywise);
        timings = (TextView) v.findViewById(R.id.timings_daywise);
    }


  }
  }

1 个答案:

答案 0 :(得分:0)

RecyclerViewFragment中,您已将list字段声明为静态,这意味着每个RecyclerViewFragment实例将共享最后放入其中的数据。

例如,如果您首先显示星期一的RecyclerViewFragment,则会在“列表”参考中加载todayslist_m列表。但是,ViewPager还将为下一页(在您的情况下为星期二)构建片段以提高性能,这意味着将创建新的RecyclerViewFragment,这次是指向todayslist_m的静态list引用。通过这种方式,您最终会在星期一和星期二将RecyclerViewFragment指向同一个数据列表(RecyclerViewFragment上的最后一个数据集)。

解决方案是让list中的RecyclerViewFragment引用为而不是是静态的,如果您使用静态作为引用{{1}的平均值,则重新考虑您的方法}来自list片段之外。