Issue
I made a Slide Activity and there are two Buttons. One to get to the next Layer and another one to get to the previous layer. But they only work when touched 2 times. When touched one time it just happens nothing. I want that they work when touched 1 time. Like its normally.
ACTIVITY:
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);
}
}
Thanks for the help.
Solution
Change
mSlideViewPager.setCurrentItem(mCurrentPage++);
to mSlideViewPager.setCurrentItem(++mCurrentPage);
and
mSlideViewPager.setCurrentItem(mCurrentPage--);
to mSlideViewPager.setCurrentItem(--mCurrentPage);
With mCurrentPage++
you call setCurrentItem(0)
and only then change mCurrentPage
to 1. Next time you click a button it will call setCurrentItem(1)
and set mCurrentPage
to 2. mSlideViewPager
starts on page no. 0 so setCurrentItem(0)
does nothing and only the second click triggers the change of the page from 0 to 1.
With ++mCurrentPage
you first set mCurrentPage
to 1 and only then call setCurrentItem(1)
. Next time you click a button it will set mCurrentPage
to 2 and call setCurrentItem(2)
.
Answered By - haaawk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.