I am creating a Slide using ViewPager which I can change views by sliding my finger, but the help I am looking for with this question is to know how I can generate the movement automatically and if possible guide me on how to add some animation to this transition like when we configure the animations of some slides for presentation in PowerPoint.
Here is the code, I appreciate the help.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tecnologias.uniagustapp.viewpagerlobelo.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
></android.support.v4.view.ViewPager>
</LinearLayout>
swipe_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/image_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="25dp"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"/>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
CustomSwipeAadapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.view_pager);
adapter = new CustomSwipeAadapter(this);
viewPager.setAdapter(adapter);
}
}
CustomSwipeAadapter
public class CustomSwipeAadapter extends PagerAdapter {
private int[] image_resource = {R.drawable.img01,R.drawable.img02,R.drawable.img03};
private Context context;
private LayoutInflater layoutInflater;
public CustomSwipeAadapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return image_resource.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (LinearLayout)o);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resource[position]);
textView.setText("Imagen : "+(position+1));
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
I have also shared the project here: ViewPager
For now I have added a timer that allows me to make a transition between each view, however it is not the solution I am looking for since what I am looking for is to generate some kind of animation between the transitions, I share the code below.
MainActivity
ViewPager has a method called
setCurrentItem(index, hacerTrancisionSuave);
that allows you from the code to select the page you want by indicating the index to which you want the ViewPager to navigate:In this example the ViewPager navigates to the page that is at index 1 by default. You would only have to adapt your code so that it does it in a programmed way. The last parameter indicates if you want to make the transition between pages with effect or not.