2016-06-11 169 views
3

我想添加圖像幻燈片。但不能從右向左滑動。 (用於阿拉伯語或希伯來語等語言)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

回答

3

創建版式文件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'。使用以上代碼的適配器。 –

5

ViewPager不支持RTL。

您必須爲阿拉伯語和其他語言的右向左滑動創建自定義ViewPager。

如果你不想做代碼,那麼有很多庫可用,你可以使用其中的一個。

1. RtlViewPager

2. RtlViewPager

+0

非常感謝你 –

1

而不是使用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> 
0

只是smiple解決方案,但它不是專業

寫在XML的VeiwPager

android:rotationY="180" 

,並在veiw_item

  android:rotationY="180".