2016-03-29 12 views
0

私は画像の下の画像ビューとテキストビューだけをナビゲートするはずのビューページャを持っています。しかし、ビューページャは画面全体をスワイプにシフトします。誰かが助けてくれますか? マイ断片クラス:画像ビュー専用のAndroidビューページ

  public class ImageAdapter extends PagerAdapter { 
      Context context; 
       String[] name,values; 
      int[] GalImages; 
      LayoutInflater inflater; 

    public ImageAdapter(Context context, int[] GalImages, String[] name){ 
    this.context=context; 
    this.GalImages = GalImages; 
    this.name = name; 
} 

@Override 
public int getCount() { 

    return GalImages.length; 
} 

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

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    TextView txtname; 
    ImageView imgflag; 
    ImageView imgtop; 
    TextView topText; 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(R.layout.imageadapterlayout, container, 
      false); 
     txtname = (TextView)itemView.findViewById(R.id.tvName); 
     txtname.setText(name[position]); 
    Button b1=(Button) itemView.findViewById(R.id.Btton); 
    topText =(TextView)itemView.findViewById(R.id.TV0); 
    imgflag = (ImageView) itemView.findViewById(R.id.img); 
    imgflag.setImageResource(GalImages[position]); 
    ((ViewPager) container).addView(itemView); 

    imgtop = (ImageView)itemView.findViewById(R.id.ImageView1); 
    imgtop.setImageResource(R.drawable.maruti); 

      return itemView; 


} 

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

マイMainActivity:

public class MainActivity5 extends ActionBarActivity { 
private ImageView ImageView; 
TextView textView; 
ImageView Image; 
String[] name; 
int[] GalImages; 
ViewPager viewPager; 
PagerAdapter adapter; 
ImageButton leftNav,rightNav; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewpager_main); 

    name = new String[] {"Alto K10","Swift","Dzire"}; 
    GalImages = new int[] { 
      R.drawable.alto, 
      R.drawable.swift, 
      R.drawable.dzire 

      }; 

    viewPager = (ViewPager) findViewById(R.id.view_pager); 
    adapter = new ImageAdapter(MainActivity5.this,GalImages,name); 
    leftNav = (ImageButton) findViewById(R.id.left_nav); 
    rightNav = (ImageButton)findViewById(R.id.right_nav); 
    viewPager.setAdapter(adapter); 
    } 
    } 

フラグメントのxml:

  <RelativeLayout  
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white"> 
    <RelativeLayout 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    </RelativeLayout> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="150dp" 
    android:id="@+id/img" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="160dp"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/namelabel" 
    android:id="@+id/tvName" 
    android:layout_marginTop="350dp" 
    android:layout_centerHorizontal="true" 
    android:textSize="18sp" 
    android:textColor="#000"/> 

<Button 
    android:layout_width="fill_parent" 
    android:background="#ffc14e" 
    android:layout_height="wrap_content" 
    android:id="@+id/Btton" 
    android:text="Select" 

    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:textColor="@android:color/black" 
    android:layout_marginTop="450dp" 
    android:layout_marginLeft="10dp" 
    android:layout_centerHorizontal="true" 
    /> 
     </RelativeLayout> 

MainActivity5のXML:あなたはでのTextViewを保つことができる

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
<android.support.v4.view.ViewPager 
    android:id="@+id/view_pager" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

</FrameLayout> 
</RelativeLayout> 

答えて

1

むしろ活動それを膨らませたレイアウトに保つよりも。同じレイアウトの画像とテキストがある場合、スワイプがあるとすべてのコンテンツがスクロールします。

ビューポートでaddOnPageChangeListenerを使用して、アダプタの位置を検出し、アクティビティに応じてテキストビューを更新します。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} 
      @Override 
      public void onPageSelected(int position) { 
       // update your TextView here 
       txtView.setText(name[position]); 
       } 
      } 
      @Override 
      public void onPageScrollStateChanged(int state) {} 
     }); 
関連する問題