错误:滑块出现每次使用viewpager

时间:2017-06-24 11:00:00

标签: android android-viewpager slider

我正在制作一个滑块演练,当用户点击应用程序时,它会在第一个项目的应用程序中引入....当用户再次打开它时它就消失了。

像这样:https://www.youtube.com/watch?v=va2IRW_e7_w

问题是,每当我打开应用程序时,我会一次又一次地重新获得它,这是一个小错误,我无法找到它。

Main2Activity:

    public class Main2Activity extends AppCompatActivity {

        Button next, skip;
        private ViewPagerAdapter adapter;
        private ViewPager viewPager;
        private SliderWalkthrough sliderWalkthrough;
        private int[] layouts;
        private TextView[] dots;
        private LinearLayout dotsLayout;

ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            addButtonDots(position);
            if (position == layouts.length - 1) {

                next.setText("GET STARTED");
                skip.setVisibility(View.GONE);
            } else {
                next.setText("NEXT");
                skip.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            sliderWalkthrough = new SliderWalkthrough(this);
            if (!sliderWalkthrough.Check()) {
                sliderWalkthrough.setFirst(false);
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            if (Build.VERSION.SDK_INT >= 21) {

                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

            }
            setContentView(R.layout.activity_main2);

            viewPager = (ViewPager) findViewById(R.id.id_WalkThroughViewPager);
            dotsLayout = (LinearLayout) findViewById(R.id.layout_dots);

            layouts = new int[]{R.layout.slider_walkthrough_screen1, R.layout.slider_walkthrough_screen2, R.layout.slider_walkthrough_screen3};
            addButtonDots(0);

            adapter = new ViewPagerAdapter();
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(listener);

        }

        private void addButtonDots(int position) {

            dots = new TextView[layouts.length];
            int[] colorActive = getResources().getIntArray(R.array.active_dot);
            int[] colorInactive = getResources().getIntArray(R.array.inactive_dot);
            dotsLayout.removeAllViews();
            for (int i = 0; i < dots.length; i++) {
                dots[i] = new TextView(this);
                dots[i].setTextSize(30);
                dots[i].setTextColor(colorInactive[position]);
                dotsLayout.addView(dots[i]);

            }
            if (dots.length > 0) {
                dots[position].setTextColor(colorActive[position]);
            }

        }
    }

SliderWalkthrough:

public class SliderWalkthrough {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Context context;

    public SliderWalkthrough(Context context) {
        this.context = context;
        sharedPreferences=context.getSharedPreferences("first",0);
        editor=sharedPreferences.edit();
     }

    public void setFirst(Boolean firstTime){
        editor.putBoolean("check",firstTime);
        editor.commit();
    }

    public boolean Check()
    {
        return sharedPreferences.getBoolean("check",true);
    }
}

1 个答案:

答案 0 :(得分:1)

一旦您的演练结束,您需要将preference值更改为false,否则if (!sliderWalkthrough.Check()) {永远不会被执行,并且每次都会显示演练

    @Override
    public void onPageSelected(int position) {

        addButtonDots(position);
        if (position == layouts.length - 1) {
            next.setText("GET STARTED");
            skip.setVisibility(View.GONE);
            sliderWalkthrough.setFirst(false);
            //^^^^^^ add this case
        } 
         else {
            next.setText("NEXT");
            skip.setVisibility(View.VISIBLE);
        }
    }

注意:删除sliderWalkthrough.setFirst(false);,不再需要

        if (!sliderWalkthrough.Check()) {
            Intent intent = new Intent(Main2Activity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }