如何通过Android底部导航在现有片段上打开新片段

时间:2019-09-09 17:11:11

标签: android android-fragments android-bottomnav

正如您在下面看到的,我的底部导航器具有这种Fragment布局:

enter image description here

因此,当应用程序用户从回收视图中单击卡片项目时,我想在此搜索片段上打开另一个片段。我正在尝试这样做,但是发生此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: mobhair.com.br, PID: 538
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
    at android.support.v7.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:4101)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3835)

我真的不知道是哪个问题。苏可以请你帮我吗?

按照我到目前为止所做的事情:

搜索片段:

public class SearchFragment extends Fragment {

    private SearchViewAdataper searchViewAdataper;
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_screen_fragment, container, false);
        final ProgressDialog progressDialog = new ProgressDialog(view.getContext());

        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setTitle(R.string.title_search);

        progressDialog.setMessage(getString(R.string.loading_data));
        recyclerView = (RecyclerView) view.findViewById(R.id.search_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));

        SearchManager searchManager = new SearchManager(view.getContext());
        searchManager.getAllProfessions(new SearchServiceListener() {
            @Override
            public void onSuccess(List<SearchDataModel> searchDataModels) {
                searchViewAdataper = new SearchViewAdataper(getContext(), searchDataModels);
                recyclerView.setAdapter(searchViewAdataper);
                progressDialog.dismiss();
            }

            @Override
            public void onFail(Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(getContext(), R.string.communication_server_problem, Toast.LENGTH_LONG).show();
            }
        });

        return view;
    }
}

SearchSectionAdapter适配器:在这里,它管理所有卡片项目并执行操作,单击卡片项目中的“侦听器”以调用新片段

注意:我将只设置setOnClickListener实现。如有必要,请告知所有实现方式,然后我可以更新此问题。

holder.mSearchItemImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {

            ProfessionalManager professionalManager = new ProfessionalManager(v.getContext());
            professionalManager.getAllProfessionalByProfessionDescription(singleItem.getDescription(), new ProfessionalServiceListener() {
                @Override
                public void onSuccess(List<Professional> professionals) {
                    /*Intent intent = new Intent(v.getContext(), ProfessionalListActivity.class);
                    v.getContext().startActivity(intent);*/
                    Fragment fragment = new ProfessionalListFragment();
                    FragmentTransaction ft = ((FragmentActivity)context)
                            .getSupportFragmentManager()
                            .beginTransaction();
                    ft.replace(R.id.search_recyclerView, fragment);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.addToBackStack(null);
                    ft.commit();
                }

                @Override
                public void onFail(Throwable t) {

                }
            });
        }
    });

1 个答案:

答案 0 :(得分:0)

您在这里使用recyclerView参考

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

再次使用recyclerView替换出现问题的片段

ft.replace(R.id.search_recyclerView, fragment);

您需要<fragment>布局来替换它。