单击侦听器不起作用

时间:2017-12-14 13:50:13

标签: java android android-viewpager onclicklistener

我的ViewPager出了问题,我的计算机上有一个OnClickListener所有标签点击我的模拟器,但是在我的手机上它有点怪异,只有中间标签点击,其他2没有一点都不我甚至把日志放在那里,他们没有记录任何东西。更糟糕的是,没有错误,它只是没有做任何事情。

Small Part
    vpPager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (vpPager.getCurrentItem() == 0) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 1) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 2) {
                zmanimListPopUp();
            }

        }


    });

请帮助我!

谢谢!

更新

大部分

 vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            prefs.edit().putInt("currentFragmentItem", position).commit();
            Log.d("MainActivity", "Position: " + prefs.getInt("currentFragmentItem", 1));
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    vpPager.setCurrentItem(prefs.getInt("currentFragmentItem", 1));
    Log.d("MainActivity", "Position on launch: " + prefs.getInt("currentFragmentItem", 1));


    DotsIndicator dotsIndicator = (DotsIndicator) findViewById(R.id.dots_indicator);
    dotsIndicator.setViewPager(vpPager);
    dotsIndicator.setPointsColor(Color.rgb(142, 96, 97));

    onClickListener();

}

private void onClickListener() {


    vpPager.setOnTouchListener(
            new View.OnTouchListener() {
                private boolean moved;

                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        moved = false;
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                        moved = true;
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        if (!moved) {
                            view.performClick();
                        }
                    }

                    return false;
                }
            }
    );

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

            if (vpPager.getCurrentItem() == 0) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 1) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 2) {
                zmanimListPopUp();
            }

        }


    });


}

所做的改变:

    vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {


        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            vpPager.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (vpPager.getCurrentItem() == 0) {
                        zmanimListPopUp();
                    } else if (vpPager.getCurrentItem() == 1) {
                        zmanimListPopUp();
                    } else if (vpPager.getCurrentItem() == 2) {
                        zmanimListPopUp();
                    }

                }


            });
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });

2 个答案:

答案 0 :(得分:1)

从内部尝试vpPager.setOnPageChangeListener .....和public void onPageSelected(int position){}来监听所​​选页面并返回页面位置并相应地采取行动,就像你编码一样。

 tvPager.setOnPageChangeListener(new OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                    zmanimListPopUp();
                } else if (position == 1) {
                    zmanimListPopUp();
                } else if (position == 2) {
                    zmanimListPopUp();
                }

        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // Code goes here
        }

        // Called when the scroll state changes: 
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });

答案 1 :(得分:0)

我们已经面临在我们的Android应用程序中实现选项卡期间自动加载上一个/下一个选项卡的数据。但是我们可以通过添加“setUserVisibleHint”方法为Material Tabs或PageSlidingTabStrip Tabs停止此操作。将此方法添加到所有选项卡的片段,并使用此方法加载所有操作。此方法仅在选中其选项卡时加载片段数据,以便每次加载一个选项卡。

// initialize boolean to know tab is already loaded or load first time
private boolean isFragmentLoaded=false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser && !isFragmentLoaded ) {
        // Load your data here or do network operations here
        isFragmentLoaded = true;
    }
}