我做了幻灯片活动,并且有两个按钮。一个到达下一个层,另一个到达上一层。但他们仅在触摸2次时才会工作。触摸一次它什么也没发生。我希望他们在触摸一次时能够工作。就像它通常一样。
活动:
package world.cryneo.partytime.java;
public class introActivity extends AppCompatActivity {
private ViewPager mSlideViewPager;
private LinearLayout mDotLayout;
private SlideAdapter slideAdapter;
private TextView[] mDots;
private Button mNextBtn, mBackBtn;
private int mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
mSlideViewPager = (ViewPager) findViewById(R.id.slideViewPager);
mDotLayout = (LinearLayout) findViewById(R.id.dots_layout);
mNextBtn = (Button) findViewById(R.id.button3);
mBackBtn = (Button) findViewById(R.id.button2);
slideAdapter = new SlideAdapter(this);
mSlideViewPager.setAdapter(slideAdapter);
addDotsIndicator(0);
mSlideViewPager.addOnPageChangeListener(viewListener);
mNextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mNextBtn.getText().equals("Fertig")){
Intent settingsIntent = new Intent(introActivity.this, MainActivity.class);
startActivity(settingsIntent);
}
mSlideViewPager.setCurrentItem(mCurrentPage++);
}
});
mBackBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSlideViewPager.setCurrentItem(mCurrentPage--);
}
});
}
public void addDotsIndicator(int position){
mDots = new TextView[4];
mDotLayout.removeAllViews();
for (int i=0; i< mDots.length; i++){
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("•"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.white));
mDotLayout.addView(mDots[i]);
}
if (mDots.length > 0){
mDots[position].setTextColor(getResources().getColor(R.color.colorwhite));
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
addDotsIndicator(i);
mCurrentPage = i;
if (i == 0) {
mBackBtn.setVisibility(View.INVISIBLE);
mNextBtn.setText("Weiter");
mBackBtn.setText("Zurück");
} else if (i == mDots.length -1){
mBackBtn.setVisibility(View.VISIBLE);
mNextBtn.setText("Fertig");
mBackBtn.setText("Zurück");
} else {
mBackBtn.setVisibility(View.VISIBLE);
mNextBtn.setText("Weiter");
mBackBtn.setText("Zurück");
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
};
}
SLIDER ADAPTER:
package world.cryneo.partytime.java;
public class SlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SlideAdapter (Context context){
this.context = context;
}
// Array
public int[] slide_images = {
R.drawable.event,
R.drawable.hype,
R.drawable.freunde,
R.drawable.profil,
};
public String[] slide_headings = {
"Events", "Vorteile","Freunde","Profil"
};
public String[] slide_descs = {
"Betrachte alle Events und klicke auf Sie um weitere Informationen zu erhalten.",
"Klicke auf die Events und schau dir Details an. Exklusive Vorteile warten auf dich! Wenn dir das Event gefällt wirst du 3 Stunden vor Beginn benachrichtigt steigerst du das Hype-o-Meter.",
"Finde Bekanntschaften und nimm Kontakt auf.",
"OKay hallo 4"
};
@Override
public int getCount() {
return slide_headings.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == (RelativeLayout) o;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_descs[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
感谢您的帮助。
答案 0 :(得分:1)
更改
mSlideViewPager.setCurrentItem(mCurrentPage++);
至mSlideViewPager.setCurrentItem(++mCurrentPage);
和
mSlideViewPager.setCurrentItem(mCurrentPage--);
至mSlideViewPager.setCurrentItem(--mCurrentPage);
使用mCurrentPage++
,您只需拨打setCurrentItem(0)
,然后再将mCurrentPage
更改为1.下次点击按钮时,系统会调用setCurrentItem(1)
并将mCurrentPage
设置为2. mSlideViewPager
从第页开始。 0所以setCurrentItem(0)
什么都不做,只有第二次点击才会触发页面从0更改为1。
使用++mCurrentPage
,您首先将mCurrentPage
设置为1,然后再调用setCurrentItem(1)
。下次单击按钮时,它会将mCurrentPage
设置为2并调用setCurrentItem(2)
。
答案 1 :(得分:0)
请你试试这个
mSlideViewPager.setAdapter(slideAdapter);
mSlideViewPager.setCurrentItem(0);
addDotsIndicator(0);