刷新选项卡上的片段视图

时间:2016-09-28 08:58:18

标签: android android-fragments android-tablayout

您好我正在使用单个片段实现动态TabLayout,到目前为止我已设法创建选项卡,但使用单个片段时出现问题。当我更改选项卡时,片段视图不会重复更新,尽管我会根据选项卡选择更新片段的onCreateView方法中的数据。我在该片段中有一个recyclerview,它应根据选项卡选择进行更改。任何人都可以帮助我,或者有人有更好的方法吗?

TabView活动

public class SubCategory extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
Activity a;
String url;
List<CategoryEncapsulation> categories = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub_category);
    a=this;
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    categories=((MyshinzGlobal)getApplicationContext()).getCategoryList();
    setupViewPager(viewPager);
    ((MyshinzGlobal)getApplicationContext()).setCategory_id(categories.get(0).getCategory_id());
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    try {

        for (int i = 0; i < categories.size(); i++) {
            try {
                adapter.addFragment(new ListFragment(), categories.get(i).getCategory_name());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
        ((MyshinzGlobal)getApplicationContext()).setCategory_id(categories.get(position).getCategory_id());
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}


class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    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(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

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

片段类

public class ListFragment extends Fragment{

String category_id;
Activity a;
RecyclerView rv;
SubCategoryAdapter sca;
List<ParentListItem> parentListItems;
List<CategoryEncapsulation> categories = new ArrayList<>();


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =inflater.inflate(R.layout.sub_category_fragment, container, false);
    a=getActivity();
    categories=((MyshinzGlobal)a.getApplicationContext()).getCategoryList();
    category_id=((MyshinzGlobal)a.getApplicationContext()).getCategory_id();
    parentListItems= new ArrayList<>();
    Toast.makeText(getActivity(), String.valueOf(category_id), Toast.LENGTH_SHORT).show();
    rv= (RecyclerView) view.findViewById(R.id.recycler_view);

    for (int i=0; i<categories.size();i++){
        if(categories.get(i).getCategory_id().equals(category_id)){
            parentListItems=categories.get(i).getParentListItems();
        }
    }
    sca=new SubCategoryAdapter(a,parentListItems);
    rv.setAdapter(sca);
    rv.setLayoutManager(new LinearLayoutManager(a));
    //new GetAllSubCategoryTask(a,url,"1001").execute();
    return view;
}


 }

2 个答案:

答案 0 :(得分:2)

我有两个解决方案。

1)使您的视图寻呼机偏移限制 0 。哪个默认 1 。如果要切换另一个不在偏移量中的片段,则重新创建该片段。

TOKEN_QUERY|TOKEN_IMPERSONATE|TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY

2)告诉您的适配器,片段已被更改。因此,您的适配器将再次重新创建其视图。

viewPager.setOffscreenPageLimit(0);

我希望这些可以解决你的问题。

答案 1 :(得分:2)

首先,当您使用ViewPager时,只有在调用FragmentPagerAdapter的getItem()时才会调用Fragment的onCreateView()。

如果需要在Fragment中创建一个方法,它将调用RecyclerView.notifyDataSetChanged()

查看两个类的更新代码

TabView活动

public class SubCategory extends AppCompatActivity {

    //....

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    }

    private void setupViewPager(ViewPager viewPager) {
        //...
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            ((MyshinzGlobal)getApplicationContext()).setCategory_id(categories.get(position).getCategory_id());
            adapter.getItem(position).listUpdated();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<ListFragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

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


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

片段类

public class ListFragment extends Fragment {

        //...

        public void listUpdated() {
            for (int i = 0; i < categories.size(); i++) {
                if (categories.get(i).getCategory_id().equals(category_id)) {
                    parentListItems.clear();
                    parentListItems.addAll(categories.get(i).getParentListItems());
                }
            }
            sca.notifyDataSetChanged();
        }
    }