【问题标题】:How to shift to another fragment from tab fragment created using view pager如何从使用视图寻呼机创建的选项卡片段转移到另一个片段
【发布时间】:2019-04-04 14:53:45
【问题描述】:

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

 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);
    }
}

寻呼机适配器:-

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;
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

    如果您有一个AndroidViewModel 附加到 Activity 生命周期,您可以在您的 Activity 中观察该状态并向FragmentManager 进行交易以代表您的选择。

    一个例子

    视图模型

    此 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 提供的灵感索引。这让它知道需要显示哪些内容。这可以从 ANYWHERE 进行设置。

    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);
    

    这应该会给你一个很好的开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多