ViewPager + Framgnent + TabLayout如何动态更新Fragment数据

时间:2018-09-01 02:01:12

标签: android android-fragments

当我使用ViewPager + TabLaout + Fragment更新Fragment的数据时,它不起作用。   我已经向适配器添加了InstantiateItem()和getItemPosition()函数,但是它根本没有任何作用。   这个问题困扰了我很长时间,希望我能得到您的帮助。

这是适配器的代码。

public class SportTimeLinePagerAdapter extends FragmentPagerAdapter {


    private ArrayList<String> titleList;
    private ArrayList<Fragment> fragmentList;


    public SportTimeLinePagerAdapter(FragmentManager fm, ArrayList<String> titleList, ArrayList<Fragment> fragmentList) {
        super(fm);
        this.titleList = titleList;
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        switch (position){
            case 0:
                DayFragment dayFragment = (DayFragment) super.instantiateItem(container, position);
                dayFragment.updataArgument(dayFragment.sporttype);
                return dayFragment;
            case 1:
                WeekFragment weekFragment = (WeekFragment) super.instantiateItem(container,position);
                weekFragment.updataArgument(weekFragment.sporttype);
                return weekFragment;
            case 2:
                MonthFragment monthFragment = (MonthFragment) super.instantiateItem(container,position);
                monthFragment.updataArgument(monthFragment.sporttype);
                return monthFragment;
            case 3:
                TotalFragment totalFragment = (TotalFragment) super.instantiateItem(container,position);
                totalFragment.updataArgument(totalFragment.sporttype);
                return totalFragment;
        }

       return null;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return PagerAdapter.POSITION_NONE;
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

这是活动的代码。我认为主要问题应该在此和适配器中。

public class SportTimeLineAllActivity extends AppCompatActivity implements View.OnClickListener {


    @BindView(R.id.tv_sport_type)
    NormalTextView tvSportType;
    @BindView(R.id.iv_choiceSportType)
    ImageView ivChoiceSportType;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.tab_layout)
    XTabLayout tabLayout;
    @BindView(R.id.viewpager_date)
    ViewPager viewpagerDate;
    private Context mContext;

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rlay_All)
    LinearLayout rlayAll;

    private NormalTextView tvAll;
    private NormalTextView tvRunning;
    private NormalTextView tvTrailrun;
    private NormalTextView tvRunindoor;
    private NormalTextView tvHiking;
    private NormalTextView tvBiking;
    private NormalTextView tvWalking;
    private View view_alpha;

    private PopupWindow popupWindow;
    private Animation animation = null;

    private int isOpen = 0; //

    private int currentTab = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_timeline);
        ButterKnife.bind(this);
        mContext = this;

        initTab();
    }


    /**
     * 
     */
    private void initTab() {
        ArrayList<String> titleData = new ArrayList<>();
        titleData.add(getResources().getString(R.string.up_case_day));
        titleData.add(getResources().getString(R.string.week));
        titleData.add(getResources().getString(R.string.month));
        titleData.add(getResources().getString(R.string.total));

        ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(new DayFragment());
        fragmentArrayList.add(new WeekFragment());
        fragmentArrayList.add(new MonthFragment());
        fragmentArrayList.add(new TotalFragment());

        SportTimeLinePagerAdapter sportTimeLinePagerAdapter= new SportTimeLinePagerAdapter(
                getSupportFragmentManager(),titleData,fragmentArrayList);

        viewpagerDate.setAdapter(sportTimeLinePagerAdapter);
        tabLayout.setupWithViewPager(viewpagerDate);



    }

    @OnClick({R.id.iv_back, R.id.rlay_All, R.id.iv_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rlay_All:

                initPopupWindow();
                choiceSportType();
                break;

        }
    }


    /**
     * 
     */
    private void choiceSportType() {
        if (isOpen == 0) {
            ivChoiceSportType.setImageResource(R.drawable.icon_takeup);
            isOpen = 1;
        } else if (isOpen == 1) {
            ivChoiceSportType.setImageResource(R.drawable.icon_open);
            isOpen = 0;
        }
    }


    /**
     *
     */
    private void initPopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_popup_sport_all
                , null, false);

        tvAll = view.findViewById(R.id.tv_all);
        tvRunning = view.findViewById(R.id.tv_running);
        tvTrailrun = view.findViewById(R.id.tv_trailrun);
        tvRunindoor = view.findViewById(R.id.tv_runindoor);
        tvHiking = view.findViewById(R.id.tv_hiking);
        tvBiking = view.findViewById(R.id.tv_biking);
        tvWalking = view.findViewById(R.id.tv_walking);
        view_alpha = view.findViewById(R.id.view_alpha);

        tvAll.setOnClickListener(this);
        tvRunning.setOnClickListener(this);
        tvTrailrun.setOnClickListener(this);
        tvRunindoor.setOnClickListener(this);
        tvHiking.setOnClickListener(this);
        tvBiking.setOnClickListener(this);
        tvWalking.setOnClickListener(this);
        view_alpha.setOnClickListener(this);

        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.showAsDropDown(rlayAll);
        popupWindow.setClippingEnabled(true);
        popupWindow.update();
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_all:
                popupWindow.dismiss();
                tvSportType.setText("All");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("All");

                break;
            case R.id.tv_running:
                popupWindow.dismiss();
                tvSportType.setText("running");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("running");
                initTab();
                break;
            case R.id.tv_trailrun:
                popupWindow.dismiss();
                tvSportType.setText("trilrun");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("trilrun");

                break;
            case R.id.tv_runindoor:
                initTab();
                popupWindow.dismiss();
                tvSportType.setText("runnin door");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("runindoor");

                break;
            case R.id.tv_hiking:

                popupWindow.dismiss();
                tvSportType.setText("hiking");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("hiking");
                initTab();
                break;
            case R.id.tv_biking:

                popupWindow.dismiss();
                tvSportType.setText("biking");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("hiking");
                initTab();
                break;
            case R.id.tv_walking:

                popupWindow.dismiss();
                tvSportType.setText("walking");
                isOpen = 1;
                choiceSportType();
                DayFragment.getInstance("walking");
                initTab();
                break;
            case R.id.view_alpha:
                popupWindow.dismiss();
                isOpen = 1;
                choiceSportType();
                break;
        }
    }


}

这是我的片段代码之一。当我调试时,在onCreate函数中获得的包是空的。产生的运动类型也为空。

public class DayFragment extends Fragment {


    @BindView(R.id.tv_currentValue)
    NormalTextView tvCurrentValue;
    @BindView(R.id.iv_tendency)
    ImageView ivTendency;
    @BindView(R.id.tv_currentUnit)
    NormalTextView tvCurrentUnit;
    @BindView(R.id.tv_firstValue)
    NormalTextView tvFirstValue;
    @BindView(R.id.tv_firstUnit)
    NormalTextView tvFirstUnit;
    @BindView(R.id.tv_secondValue)
    NormalTextView tvSecondValue;
    @BindView(R.id.tv_secondUnit)
    NormalTextView tvSecondUnit;
    @BindView(R.id.rlay_second)
    RelativeLayout rlaySecond;
    @BindView(R.id.tv_calorie)
    NormalTextView tvCalorie;
    @BindView(R.id.rlv_timeLine)
    RecyclerView rlvTimeLine;

    public String sporttype;
    private List<SportTimelineBean> sportTimelineBeanList = new ArrayList<>();
    private SportTimeLineAdapter sportTimeLineAdapter;
    private LinearLayoutManager linearLayoutManager;


    SportTimelineBean sportTimelineBeanWalk = new SportTimelineBean("walking", " ", "12:00-13:00");
    SportTimelineBean sportTimelineBeanRun = new SportTimelineBean("running", "17/12", "14:09-15:40");
    SportTimelineBean sportTimelineBeanHik = new SportTimelineBean("hiking", "12/12", "12:00-13:00");


    public DayFragment() {

    }

    public static DayFragment getInstance(String sporttype) {
        DayFragment dayFragment = new DayFragment();
        Bundle bundle = new Bundle();
        if (bundle != null) {
            bundle.putString("sporttype", sporttype);
        }
        dayFragment.setArguments(bundle);
        return dayFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            sporttype = bundle.getString("sporttype");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_date, container, false);
        ButterKnife.bind(this, view);

        sportTimelineBeanList.clear();
        sportTimelineBeanList.add(sportTimelineBeanWalk);
        initData();

        linearLayoutManager = new LinearLayoutManager(getActivity());
        rlvTimeLine.setLayoutManager(linearLayoutManager);
        rlvTimeLine.setHasFixedSize(true);
        sportTimeLineAdapter = new SportTimeLineAdapter(sportTimelineBeanList);
        rlvTimeLine.setAdapter(sportTimeLineAdapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    public void updataArgument(String sporttype) {
        this.sporttype = sporttype;
        Bundle args = getArguments();
        if (args != null) {
            args.putString("sporttype", sporttype);
        }
    }


    private void initData() {
        if (!StringUtils.isEmpty(sporttype)) {
            sportTimelineBeanList.clear();
            if (sporttype.equals("running")) {
                sportTimelineBeanList.clear();
                sportTimelineBeanList.add(sportTimelineBeanRun);
                sportTimeLineAdapter.notifyDataSetChanged();
            } else if (sporttype.equals("walking")) {
                sportTimelineBeanList.clear();
                sportTimelineBeanList.add(sportTimelineBeanWalk);
                sportTimeLineAdapter.notifyDataSetChanged();
            } else if (sporttype.equals("hiking")) {
                sportTimelineBeanList.clear();
                sportTimelineBeanList.add(sportTimelineBeanHik);
                sportTimeLineAdapter.notifyDataSetChanged();
            }
        }
    }
}

希望您能帮助我,我非常感谢。

0 个答案:

没有答案