Android viewPager图像从右向左滑动

Android viewPager图像从右向左滑动

问题描述:

我想添加图像幻灯片。但不能从右向左滑动。 (用于阿拉伯语或希伯来语等语言)Android viewPager图像从右向左滑动

我检查了几乎所有的答案在stackoverflow,但无法找到一个明确的解决方案。

我在这里写下整个代码。

请写信给我清楚,我不是一个专业的

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


    } 


} 

这里 是 的 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); 
    } 
    } 

这里是布局文件;

<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> 
+0

只需从阵列 –

+0

删除访问的网页索引哦!我很抱歉!,你想要从阿拉伯文左右改变滑动位置,我很想理解,所以请忽略以前的评论,向你寻找答案! –

+0

@Survivor,我不是专业人士..你能解释一下吗? – ali

创建版式文件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> 

更改PagerAdapter这样的:

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); 
} 
} 

编辑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); 

} 

我希望这有助于:)

+0

非常感谢。但我没有PagerAdapter? 我必须添加一个名为PagerAdapter.java的新java文件吗? – ali

+0

错误:(34,25)错误:无法找到符号变量mLayoutInflater – ali

+0

请参阅我已编辑的代码我编辑了答案..并定义了'mLayoutInflater'。使用以上代码的适配器。 –

ViewPager不支持RTL。

您必须为阿拉伯语和其他语言的右向左滑动创建自定义ViewPager。

如果你不想做代码,那么有很多库可用,你可以使用其中的一个。

1. RtlViewPager

2. RtlViewPager

+0

非常感谢你 –

而不是使用ViewPager的,我建议你使用SwipeDeck因为随着Niranj帕特尔说ViewPager不支持从右到左。

<?xml version="1.0" encoding="utf-8"?> 
<com.daprlabs.cardstack.SwipeFrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:swipedeck="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <com.daprlabs.cardstack.SwipeDeck 
     android:id="@+id/swipe_deck" 
     android:layout_width="match_parent" 
     android:layout_height="480dp" 
     android:padding="20dp" 
     swipedeck:card_spacing="10dp" 
     swipedeck:max_visible="3" 
     swipedeck:render_above="true" 
     swipedeck:rotation_degrees="15" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="Button" /> 

</com.daprlabs.cardstack.SwipeFrameLayout> 

只是smiple解决方案,但它不是专业

写在XML的VeiwPager

android:rotationY="180" 

,并在veiw_item

  android:rotationY="180".