简体   繁体   中英

Android viewpager for imageview only

I have a viewpager which is supposed to navigate only the imageview and text view underneath the image. However, the viewpager shifts the entire screen on swipe.Can someone please help? My Fragment class:

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

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

Fragment 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>

The MainActivity5 xml:

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

You can keep the TextView in the Activity rather than keeping it in inflated layout. if you have image and text in same layout all content will scroll when there is a swipe.

Use addOnPageChangeListener with the viewpager to detect adapter position and update the textview accordingly in the activity.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM