gpt4 book ai didi

android - 漏洞 : Slider Appearing Every time using viewpager

转载 作者:行者123 更新时间:2023-11-30 00:29:23 27 4
gpt4 key购买 nike

我正在制作一个 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]);
}

}
}

slider 演练:

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);
}
}

最佳答案

您需要在演练完成后将 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();
}

关于android - 漏洞 : Slider Appearing Every time using viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44735671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com