I have a Fragment pager, I built a button on the first fragment. Now I want to make it swipe to the second fragment when I click on that button. I built this so far:
public class HowTo extends Fragment {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.how_to, container, false);
((TextView) android.findViewById(R.id.howTo)).setText("howto");
Button next_frag = (Button) android.findViewById(R.id.next_frag);
next_frag.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View v) {
// Swipe to page`?
View android = inflater.inflate(R.layout.how_to, container, false);
}
});
return android;
}
}
It did not work. Does anyone know how to solve this issue.
EDIT: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And xml with my button that is going to change fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/how_to_gradient">
<Button
android:id="@+id/next_frag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:gravity="center"
android:layout_weight="1"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textSize="21sp"
android:textStyle="bold"
android:text="test"
android:background="@drawable/border_inner_orange"/>
</LinearLayout>
Try using this on Button Click:
viewPager.setCurrentItem(nextposition);
or if you want a smooth scroll you can use
viewPager.setCurrentItem(position,true);
Edit:
You can get the Current page of View pager by :
viewPager.getCurrentItem();
get the number & add +1 to put the value in nextposition
EDITED:
MainActivity:
inside MainActivity initialize a static instance of MainActivity:
public static MainActivity mInstance = null;
then inside onCreate method of MainActivity do like this:
mInstance = this;
these above are two additions in your MainActivity..
Now Your Fragment
public class HowTo extends Fragment {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.how_to, container, false);
((TextView) android.findViewById(R.id.howTo)).setText("howto");
ViewPager viewPager = (ViewPager) MainActivity.mInstance.findViewById(R.id.pager);
int nextFragment = viewPager.getCurrentItem() + 1;
Button next_frag = (Button) android.findViewById(R.id.next_frag);
next_frag.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View v) {
// Swipe to page`?
viewPager.setCurrentItem(nextFragment);
}
});
return android;
}
}
To Make Fragments on Swipe you can use ViewPager
or
Try this to Swipe on Click
http://developer.android.com/training/implementing-navigation/lateral.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.