简体   繁体   中英

Android: Make imageview with viewpager

I'm trying to make a swipe imageview so I use viewpager . In my main layout I want to have and a button as you can see on the main_activity.xml .

The thing is the app is crashing and I don't know why with exception:

(ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams).

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lgray"
android:gravity="clip_vertical|top|bottom|fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.plushost.wizgr.MainActivity" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/ImageView3_Left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:src="@drawable/front" />

    <android.support.v4.view.ViewPager
    android:id="@+id/myviewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

<Button
    android:id="@+id/buttonCat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:text="@string/action_something" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends SherlockActivity {

public int currentimageindex=0;
ImageView slidingimage;

private int[] IMAGE_IDS = {
      R.drawable.front,
      R.drawable.front2
};

ViewPager viewPager;
MyPagerAdapter myPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(".gr");
    getSupportActionBar().setLogo(R.drawable.logo);
    setContentView(R.layout.activity_main);        

    viewPager = (ViewPager)findViewById(R.id.myviewpager);
    myPagerAdapter = new MyPagerAdapter();
    viewPager.setAdapter(myPagerAdapter);



}

private class MyPagerAdapter extends PagerAdapter{

      int NumberOfPages = 2;

      @Override
      public int getCount() {
       return NumberOfPages;
      }

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

      @Override
      public Object instantiateItem(View container, int position) {


          ImageView imageView = (ImageView) MainActivity.this.findViewById(R.id.ImageView3_Left);
          imageView.setImageResource(IMAGE_IDS[position]);


          ((ViewPager) container).addView(imageView, 0);
          return imageView;
      }

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

     }
}

Its because you are using findViewById on a view which is already in your RelativeLayout.

 ImageView imageView = (ImageView) MainActivity.this.findViewById(R.id.ImageView3_Left);

Create a new ImageView instead:

 @Override
  public Object instantiateItem(View container, int position) {


      ImageView imageView = new ImageView(MainActivity.this);
      imageView.setImageResource(IMAGE_IDS[position]);


      ((ViewPager) container).addView(imageView, 0);
      return imageView;
  }

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