Android: Slider image viewPager is not showing anything

Viewed 2056

I have seen all the question related to this topic but didn't find solution, in my case viewpager is not showing anything, its simple image slider, moreover toast are appearing as i'm swaping but didn't see image and text, and when im adding these lines(as other solutions said)

 ((ViewPager) container).addView(iv, 0);
 ((ViewPager) container).addView(tv, 0);

it gives me error

android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

this is my main.xml file (inside relative layout):

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

this is viewPager adapter

public class ClsCustomPagerViewAdapter extends PagerAdapter {

int[] layouts;
String[] titles;
LayoutInflater layoutInflater;
Context context;

public ClsCustomPagerViewAdapter(Context context, int[] layouts, 
String[] titles) {
    this.layouts = layouts;
    this.titles = titles;
    this.context = context;
    layoutInflater = LayoutInflater.from(context);
}

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

    View imageLayout = layoutInflater.inflate(R.layout.image_placeholder
, container);
    TextView tv = (TextView) imageLayout.findViewById(R.id.tv);
    ImageView iv = (ImageView) imageLayout.findViewById(R.id.iv);
    Toast.makeText(context,
            "Viewpagers " + titles[position] ,
            Toast.LENGTH_SHORT).show();

    tv.setText(titles[position]);
    iv.setImageResource(layouts[position]);

    ((ViewPager) container).addView(iv, 0);
    ((ViewPager) container).addView(tv, 0);
    return imageLayout;
}

@Override
public int getCount() {
    return layouts.length;
}

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

and this is layout for images and textviews for viewpager adapter(inside framelayout)

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    android:text="Hello world"
    android:gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:layout_alignTop="@+id/iv"
    android:layout_alignBottom="@+id/iv"
    android:layout_alignLeft="@+id/iv"
    android:layout_alignRight="@+id/iv"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/iv"
    />
1 Answers
Related