Android viewPager image slide right to left

前端 未结 4 2032
旧巷少年郎
旧巷少年郎 2021-01-05 02:21

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 c

4条回答
  •  有刺的猬
    2021-01-05 02:43

    Create a Layout file pager_item.xml:

    
    
    
    
    
    
    

    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 :)

提交回复
热议问题