Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android viewPager image slide right to left

I want to add an image slide. But cannot make it slide from right to left. (for languages like Arabic or Hebrew)

I checked nearly all replies in stackoverflow, but can not find a clear solution.

I write here the whole code.

Please write me clearly, I am not a professional

mainActivity;

package com.manishkpr.viewpagerimagegallery;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);


  }


}

here is the ImageAdapter.java;

package com.manishkpr.viewpagerimagegallery;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);


      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }

and here is the layout file;

<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"
    tools:context=".MainActivity" >

          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</RelativeLayout>
like image 385
İsmail Avatar asked Jun 11 '16 08:06

İsmail


People also ask

How to make auto Image Slider with ViewPager in android studio?

After creating the Adapter for the ViewPager, reference the ViewPager from the XML and set the adapter to it in the MainActivity. java file. Create an array of integer which contains the images which we will show in the ViewPager. Below is the complete code for the MainActivity.

What is ViewPager2 in android?

ViewPager2 uses FragmentStateAdapter objects as a supply for new pages to display, so the FragmentStateAdapter will use the fragment class that you created earlier. Create an activity that does the following things: Sets the content view to be the layout with the ViewPager2 .


1 Answers

Create a Layout file pager_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView" />
</LinearLayout>

Change your PagerAdapter like this:

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
    R.drawable.one,
    R.drawable.two,
    R.drawable.three
};

LayoutInflater mLayoutInflater;

ImageAdapter(Context context){
    this.context=context;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
  return GalImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
  return view == ((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
    imageView.setImageResource(GalImages[position]);

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((LinearLayout)object);
}
}

EDIT 1:

A trick :

public class MainActivity extends Activity {


@Override
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(adapter.getCount()-1);

}

I hope this helps :)

like image 98
Janki Gadhiya Avatar answered Oct 08 '22 23:10

Janki Gadhiya