1

I want to implement the following screen :

ScreenShot

In the screen shot you can see that below MyAdvisor TextView i have an image.On swiping this image different image will be displayed .To create Swipe Gallery i am using view pager here.I am using an Adapter which is providing images to display through view pager.

Adapter:

   public class ImageAdapter extends PagerAdapter {

    private Context context;
    private int[] images = new int[]{R.drawable.imgone,
            R.drawable.img2,
            R.drawable.img3};

    public ImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return images.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.activity_horizontal_margin);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setImageResource(images[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

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

Below is the xml file of demo project of mine which contains two text view and a view pager.

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:layout_above="@+id/txt"
    android:layout_marginTop="15sp"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txt"
    android:text="Hello2"
    android:layout_above="@+id/pager"
    android:layout_marginTop="15sp"/>



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

My problem is that the view pager is having height as match_parent.Even after changing it as wrap_content ,the other views are not displayed .Please guide me how can i implement this screen.

Deepak Rattan
  • 1,279
  • 7
  • 21
  • 47
  • Post your XML layout file. – dieter_h Jan 24 '16 at 10:57
  • Hard code your view pager height to android:layout_height="200dp" . possible duplicate of http://stackoverflow.com/questions/13966904/viewpager-in-android-is-taking-full-screen or http://stackoverflow.com/questions/8532307/android-viewpager-dimension?rq=1 – Hitesh Sahu Jan 24 '16 at 11:05

1 Answers1

0

I suggest you to use Heterogenous Layouts inside RecyclerView for that approach.If you're not familiar with the concept, go thru this tutorial.And for the viewpager part, there is this awesome library which comes up with so many built in features that you will absolutely love.Just inflate the Sliderlayout of this library inside onCreateViewHolder() of adapter and provide corresponding data.No need to write your own custom pageradapter and handle events.You just have to know how to use Recyclerview and you're pretty much done.

ishwor kafley
  • 938
  • 14
  • 32