在Fragment

时间:2018-03-02 14:30:05

标签: android bottomnavigationview

我试图在滚动时隐藏底部导航(在片段内)。

我正在使用此代码段进行隐藏显示/隐藏功能 我在这里找到https://stackoverflow.com/a/44779186/8611488

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            if (dy > 0 ||dy<0 && bottomNav.isShown())
            {
                bottomNav.setVisibility(View.GONE);
            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState)
        {
            if (newState == RecyclerView.SCROLL_STATE_IDLE)
            {
                bottomNav.setVisibility(View.VISIBLE);
            }

            super.onScrollStateChanged(recyclerView, newState);
        }
    });

我已经在Fragment中声明了我的Bottomnavigation:

BottomNavigationView mBottomNav = rootview.findViewById(R.id.bottom_navigation);

所以Fragment可以从Mainactivity访问bottomnav变量 - &gt;

BottomNavigationView mBottomNav = findViewById(R.id.bottom_navigation);

应用只是在滚动时崩溃。

Logcat崩溃:

Attempt to invoke virtual method 'boolean android.support.design.widget.BottomNavigationView.isShown()' on a null object reference – 

任何帮助都适用

编辑:

这是我的片段类(相关内容):

public class SearchFragment extends Fragment implements SearchItemAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private SearchItemAdapter mExampleAdapter;
    private ArrayList<SearchItem> mExampleList;
    private RequestQueue mRequestQueue;


    private BottomNavigationView mBottomNav ;


    public static final String EXTRA_URL = "imageUrl";
    public static final String EXTRA_CREATOR = "creatorName";
    public static final String EXTRA_LIKES = "likeCount";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_search, container, false);


        EditText editText = rootView.findViewById(R.id.edittext);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });


      mBottomNav = rootView.findViewById(R.id.bottom_navigation);

        mRecyclerView = rootView.findViewById(R.id.recycler_view);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mExampleList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(getActivity());
        parseJSON();







        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0 && mBottomNav.isShown()) { // <<mBottomNav causes crash ( why is it null?)
                    mBottomNav.setVisibility(View.GONE);
//                    Toast.makeText(getActivity(),"WORKS ",Toast.LENGTH_SHORT).show();
                } else if (dy < 0 ) {
                    mBottomNav.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
            }
        });


        return rootView;
    }

0 个答案:

没有答案