适配器中相同片段的Android View分页器问题

时间:2019-06-30 09:22:02

标签: java android

在对我的语法错误感到抱歉之前。 在我的项目中,我必须从Web服务获取一些数据,然后使用包含相同片段名称“列表片段”的选项卡和视图分页器创建选项卡布局。 视图分页器中的任何List片段都具有特定的字段名称“ letter search param”,并且在listfrag中调用搜索Web服务并获取基于字母搜索参数的数据库列表。 但是问题是视图分页器仅运行第一个选项卡,如果我切换到另一个选项卡,则它在选项卡1上是钢结构,并显示日志。我该如何解决? :(

在仪表板片段中:

@Override
public void setTabLayout() {

    getListFragmentDataBean().makeEmpty();

    tabLayout.setupWithViewPager(viewPager);
    viewPager.setOffscreenPageLimit(2);
    adapter = new ViewPagerAdapter(getChildFragmentManager());



    unreadCount.clear();
    tabTitle.clear();

    if(navDrawerBean.getChildren().size()!=0){   //document has child



        for(int i=0;i<navDrawerBean.getChildren().size();i++){

                tabTitle.add(navDrawerBean.getChildren().get(i).getName());
                unreadCount.add(navDrawerBean.getChildren().get(i).getCount());

            adapter.addFragment(new ListFragment(navDrawerBean.getChildren().get(i).getLetterSearchParam()));


        }
    }
    else {      //document has NO child

        tabLayout.setVisibility(View.GONE);

        adapter.addFragment(new ListFragment(navDrawerBean.getLetterSearchParam()));

    }



    viewPager.setAdapter(adapter);

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        //noinspection ConstantConditions
        tabLayout.getTabAt(i).setCustomView(prepareTabView(i));
    }

}

并查看寻呼机:

  private class ViewPagerAdapter extends FragmentStatePagerAdapter {


    private  List<ListFragment> mFragmentList = new ArrayList<>();
    ListFragment listFragment;

    public void createList(int size){
        for(int i=0;i<size;i++){
            mFragmentList.add(new ListFragment());
        }
    }

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFragment(ListFragment fragment) {

        mFragmentList.add(fragment);
    }

    @Override
    public int getItemPosition(Object object) {
        ListFragment f = (ListFragment ) object;
        if (f != null) {
            f.refresh();
        }
        return super.getItemPosition(object);
    }
}

和在列表片段中:

@SuppressLint("ValidFragment")
public ListFragment(LetterSearchParam letterSearchParam) {

   this.letterSearchParam=letterSearchParam;
}

@Override
protected void onViewReady() {
    super.onViewReady();
    listPresenter.onAttach(this);
    ButterKnife.bind(this, getGeneratedView());

    listPresenter.onLoad();
    swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            postItems.clear();
            pageNumber=1;
            listPresenter.onLoad();

        }
    });


    lettersList.setLayoutManager(lm);
    lettersList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
                isScrolling=true;
            }
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            currentItems=lm.getChildCount();
            totalItems=lm.getItemCount();
            scrollOutItem=lm.findFirstVisibleItemPosition();

            if(isScrolling && (responseLoadedItems < responseTotalItems) && ((currentItems + scrollOutItem)==totalItems)){

                isScrolling=false;
                listPresenter.onLoad();
            }
        }
    });

    lettersListAdapter = new LettersListAdapter(getContext(), postItems, new LettersItemOnClick() {
        @Override
        public void OpenEnvelopePage(Long receiverId,int inOut,Long assignId,Long letterId) {
            listPresenter.onEnvelopeClicked(receiverId,assignId,inOut,letterId);
            Log.w("getFullParametter","receiverId = " + receiverId + ". assignId = " +assignId + ". letterId = " + letterId);
        }
    });

    lettersList.setAdapter(lettersListAdapter);

}


@Override
public void createLetterSearchParams() {

    progress.setVisibility(View.VISIBLE);

    letterSearchParam.setPageNumber(pageNumber++);
    letterSearchParam.setPageSize(10);
    letterSearchParam.setSearchStrCartable(searchText);


    if(getUserVisibleHint()) {
        Log.w("isVisible","yes it's visible :) for  : " + letterSearchParam.getId());
        listPresenter.callSearch(letterSearchParam);
    }else {
        Log.w("isVisible","no it's not  visible :( for  : " + letterSearchParam.getId());
    }



}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if(isVisibleToUser && (getView() != null)){
        listPresenter.onLoad();
    }

}

0 个答案:

没有答案