如何从使用视图分页器创建的选项卡片段转移到另一个片段

时间:2018-10-31 12:30:29

标签: android android-fragments android-viewpager android-tabs

我使用视图分页器创建了具有三个片段的三个选项卡。我想单击类别片段中定义的列表项(使用视图分页器创建的制表符片段)后跳转到鼓舞人心的片段。当我单击列表项时发生错误。想要从类别fragment(在view pager中定义的fragment)类别中跳转到鼓舞人心的fragment。 类别(使用查看寻呼机创建的标签片段)

 public class Categories extends Fragment {

    private RecyclerView recyclerView;
    private List<CategoriesDataModel> list;
    private String[] categories={"Inspiring","Feelings","Strength","Hard Work","Success"};

    public Categories() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_categories, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.categoriesList_Id);

        list = new ArrayList<>();

        for (int i = 0; i < categories.length; i++) {
            CategoriesDataModel dataModel = new CategoriesDataModel();

            dataModel.cat_name = categories[i];

            list.add(dataModel);
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        CategoryRecyclerViewAdapter adapter = new CategoryRecyclerViewAdapter(list,getContext());
        adapter.setOnItemClickListener(new CategoryRecyclerViewAdapter.ClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                switch (position){
                    case 0:
                        getFragmentManager().beginTransaction().replace(R.id.frameLayout_inspiring,new Inspiring()).addToBackStack(null).commit();

                }
            }
        });
        recyclerView.setAdapter(adapter);
        return view;
    }
}

鼓舞人心的:-

public class Inspiring extends Fragment {


    public Inspiring() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_inspiring, container, false);
    }
}

Pager适配器:-

public class Pager extends FragmentStatePagerAdapter {
    int tabCount=0;

    public Pager(FragmentManager fm,int tabCount) {
        super(fm);
        this.tabCount=tabCount;
    }


    //this will return tab selected
    @Override
    public Fragment getItem(int i) {
        switch(i) {
            case 0:
                return new Recents();

            case 1:
                return new Top();

            case 2:
                return new Categories();

            default:
                return null;
        }
    }

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

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可能会发现使用AndroidViewModel

您正在做的是尝试维护应用程序不同部分之间的状态。

如果您在活动生命周期中附加了AndroidViewModel,则可以在“活动”中观察该状态,并向FragmentManager进行交易以表示您的选择。

一个例子

ViewModel

此ViewModel包含您所在的导航项的状态数据(在这种情况下,该状态数据用整数表示Fragment,并使用整数表示灵感行的索引。

public class NavigationViewModel extends AndroidViewModel {
    private MutableLiveData<Integer> navigationLiveData = new MutableLiveData<>();
    private MutableLiveData<Integer> inspirationLiveData = new MutableLiveData<>();

    public NavigationViewModel(@NonNull Application application) {
        super(application);
    }

    public LiveData<Integer> getNavigation() {
        return navigationLiveData;
    }

    public void setNavigation(Integer id) {
        navigationLiveData.postValue(id);
    }

    public LiveData<Integer> getInspiration() {
        return inspirationLiveData;
    }

    public void setInspiration(Integer id) {
        inspirationLiveData.postValue(id);
    }
}

活动

Activity将遵守我们对LiveData的实现所提供的导航AndroidViewModel。这将在进行导航更改后立即通知它。

public class NavigationActivity extends AppCompatActivity {

    private NavigationViewModel navigationViewModel;

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

        navigationViewModel = ViewModelProviders.of(this).get(NavigationViewModel.class);

        navigationViewModel.getNavigation().observe(this, id -> {
            switch(id) {
                case R.id.recents:
                    // TODO: Load recent fragment here with a transaction
                    break;
                case R.id.top:
                    // TODO: Load top fragment here with a transaction
                    break;
                case R.id.categories:
                    // TODO: Load categories fragment here with a transaction
                    break;
                case R.id.inspiring:
                    // TODO: Load inspiring fragment here with a transaction
                    break;
            }
        });
    }
}

灵感片段

Fragment将遵守我们的AndroidViewModel实施提供的灵感指数。这样就可以知道需要显示哪些内容。可以从任何地方设置。

public class InspiringFragment extends Fragment {

    private NavigationViewModel navigationViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_inspiring, container, false);

        navigationViewModel = ViewModelProviders.of(this).get(NavigationViewModel.class);

        navigationViewModel.getInspiration().observe(getViewLifecycleOwner(), inspiration -> {
            // TODO: Update the root view UI with data gleaned using the inspiration index given here
        });

        return root;
    }
}

设置

一旦有了这些,您所要做的就是打电话:

navigationViewModel.setInspiration(1);
navigationViewModel.setNavigation(R.id.inspiration);

这应该为您提供一个良好的基础。