android viewpager2 页面按钮控制器

android viewpager2 page buton controller

如果页面加载了,按钮是激活的,如果没有,我该如何做被动控制?

我想做的是,如果加载了 Viewpager 页面,则按钮处于活动状态,否则,按钮是被动的。但我什么也做不了。 有没有人可以帮忙?

如果安装了viewpager2 Button enabled,如果没有安装viewpager2 button false;

    List<Movie> movieList = new ArrayList<>();
    private ImageButton btn_down, btn_fav;
    private Button btn_setter;
    private DatabaseReference movies;
    private IFirebaseLoadDone iFirebaseLoadDone;

    public CartoonFragment() {
        // 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_cartoon, container, false);

        movies = FirebaseDatabase.getInstance().getReference("Sports");
        movies.keepSynced(true);
        iFirebaseLoadDone = this;
        loadMovie();

        viewPager2 = view.findViewById(R.id.viewPager2_);

        btn_down = view.findViewById(R.id.btn_down);
        btn_setter = view.findViewById(R.id.btn_setter);
        btn_fav = view.findViewById(R.id.btn_fav);


        btn_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             SaveToGallery();
            }

        });

        btn_setter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }

        });
        btn_fav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        return view;
    }

    private void loadMovie() {
        movies.addValueEventListener(this);
    }

    @Override
    public void onFirebaseLoadSuccess(List<Movie> movieList) {
        viewPagerAdapter = new ViewPagerAdapter(getContext(), movieList, viewPager2);
        viewPager2.setAdapter(viewPagerAdapter);
    }

    @Override
    public void onFirebasLoadFailed(String message) {
    }

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot moviesSnapShot : dataSnapshot.getChildren())
            movieList.add(moviesSnapShot.getValue(Movie.class));
        Collections.reverse(movieList);
        iFirebaseLoadDone.onFirebaseLoadSuccess(movieList);

        viewPagerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        iFirebaseLoadDone.onFirebasLoadFailed(databaseError.getMessage());
    }

    @Override
    public void onDestroy() {
        movies.removeEventListener(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        movies.removeEventListener(this);
        super.onStop();
    }
}

用我的方法替换你的方法。

private void loadMovie() {
        movies.addValueEventListener(this);
        btn_down.setEnabled(false);
        btn_fav.setEnabled(false);
        btn_setter.setEnabled(false);
    }

Firebase 方法

@Override
    public void onFirebaseLoadSuccess(List<Movie> movieList) {
        viewPagerAdapter = new ViewPagerAdapter(getContext(), movieList, viewPager2);
        viewPager2.setAdapter(viewPagerAdapter);
        if(movieList!=null && !movieList.isEmpty()){
            btn_down.setEnabled(true);
            btn_fav.setEnabled(true);
            btn_setter.setEnabled(true);
        }

    }

    @Override
    public void onFirebasLoadFailed(String message) {
         btn_down.setEnabled(false);
        btn_fav.setEnabled(false);
        btn_setter.setEnabled(false);
    }

加载时出错

private void loadMovie() {
        movies.addValueEventListener(this);
        btn_down.setEnabled(false);
        btn_fav.setEnabled(false);
        btn_setter.setEnabled(false);
    }

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setEnabled(boolean)' on a null object reference
        at com.example.duvarlar.Fragment.CartoonFragment.loadMovie(CartoonFragment.java:386)
        at com.example.duvarlar.Fragment.CartoonFragment.onCreateView(CartoonFragment.java:90)