For changing the image on swipe can be done by implementing the ViewPager in your android app.
For implementation of ViewPager we need to follow the steps mentioned below:-
1) Create ViewPager tag in your xml file .
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img_view"
/>
</RelativeLayout>
2) Now create a "Layout" xml file for the ViewPager in your project, here I have named it as my_layout.
my_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">
<ImageView
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="450dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"/>
</LinearLayout>
3) Now create an Adapter class that extends the PagerAdapter in it and override its method.
MyAdapter.java
public class MyAdapter extends PagerAdapter {
String[] rank;
int[] main;
//int[] flag;
Context context;
LayoutInflater inflater;
public MyAdapter(Context context1,int[] mainn,String[] rankk)
{
rank=rankk;
main=mainn;
//flag=flagg;
context=context1;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return rank.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
ImageView txtrank;
TextView textView;
ImageView imgflag;
View itemView = inflater.inflate(R.layout.my_layout,container,false);
// Locate the TextViews in viewpager_item.xml
textView=(TextView)itemView.findViewById(R.id.text_view);
textView.setText(rank[position]);
//txtrank = (ImageView)itemView.findViewById(R.id.main_view);
// Capture position and set to the TextViews
// txtrank.setImageResource(main[position]);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.main_view);
// Capture position and set to the ImageView
imgflag.setImageResource(main[position]);
// Add viewpager_item.xml to ViewPager
//((ViewPager) container).addView(itemView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
//((ViewPager) container).removeView((LinearLayout) object);
container.removeView((LinearLayout)object);
}
}
4) Now initialize the ViewPager in your MainActivity , create an object of Adapter and set the ViewPager's adapter as MyAdapter.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] rank;
private int[] image;
Context context;
private ViewPager mviewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
initcontrols();
}
private void initcontrols()
{
rank=new String[] {"1", "2", "3", "4", "5"};
image=new int[] {R.drawable.mercedes,R.drawable.launcher,R.drawable.bhai2,R.drawable.bhai3,R.drawable.bhai2};
//baseimage=new int[] {R.drawable.bhai2,R.drawable.india,R.drawable.india,R.drawable.hell,R.drawable.mercedes};
mviewPager=(ViewPager)findViewById(R.id.img_view);
MyAdapter myAdapter=new MyAdapter(context,image,rank);
mviewPager.setAdapter(myAdapter);
}
0 Comment(s)