I have a ViewPager and it is working. It is showing one image and text on every page.
This is my adapter:
private class SuggestionsAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
private LayoutInflater inflater;
private ViewPager pager;
public SuggestionsAdapter(ViewPager pager) {
this.pager = pager;
}
#Override
public void onPageSelected(int position) {
if (position == 0) {
pager.setCurrentItem(gallery.size(), true);
} else if (position == gallery.size() + 1) {
pager.setCurrentItem(1, true);
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public int getCount() {
return gallery.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.one_suggestion_layout, container, false);
ImageView ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
ivImage.setImageBitmap(gallery.get(position));
// Add viewpager_item.xml to ViewPager
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
container.removeView((RelativeLayout) object);
}
}
And I'm using very simple in my code:
ViewPager vpSuggestions = (ViewPager) findViewById(R.id.vpSuggestions);
SuggestionsAdapter sugAdapter = new SuggestionsAdapter(vpSuggestions);
vpSuggestions.setAdapter(sugAdapter);
This is mu xml for slider:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7">
<TextView
android:gravity="center"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/suggestions"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/vpSuggestions" />
</LinearLayout>
And this is xml for one page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ivImage1"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:src="#drawable/cat_321_sofa" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
android:id="#+id/tvTitle1"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/ivImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Because the size of pager is rectangle (small height, huge width) I want to show two images in one page.
The pager now looks like this image:
I want to be like this image:
How can I do that?
The solution is to override getPageWidth() and divide it with 2
#Override
public float getPageWidth(int position) {
return (super.getPageWidth(position) / 2);
}
Related
I want to make clicklistener on cardview in my viewpager to startActivity(Intent)
i've tried to make clicklistener with position in setOnPageChangeListener method, but it does not work for me. Please help.
I want to set Intent on cardview.clicklistener, but i don't understand how to set by position, wheter using swith or if method
HERE MY ACTIVITY CODE :
public class Activity extends AppCompatActivity {
ViewPager viewPager;
Adapter adapter;
List<Model> models;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
CardView cardView = findViewById(R.id.cardViewLogin);
//ImageViewPager
models = new ArrayList<>();
models.add(new Model(R.drawable.m,"title", "desc"));
models.add(new Model(R.drawable.d,"title", "desc"));
models.add(new Model(R.drawable.a,"title", "desc"));
adapter = new Adapter(models,this);
viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(final int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
HERE MY ADAPTER CODE :
public class Adapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}
#Override
public int getCount() {
return models.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position)
{
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.item,container,false);
ImageView imageView;
TextView title, desc;
imageView = view.findViewById(R.id.image);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.desc);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDesc());
container.addView(view,0);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position,
#NonNull Object object) {
container.removeView((View)object);
}
HERE IS MY item.xml CODE :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:id="#+id/cardViewLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
android:layout_margin="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:src="#drawable/hourglass"
android:layout_height="200dp"
android:scaleType="centerInside"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/title"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_marginTop="10dp"
android:layout_marginStart="16dp"
android:text="Title"
android:textSize="16sp"/>
<View
android:id="#+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_shadow_start_color"
android:layout_margin="10dp"
android:layout_below="#+id/title"/>
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/separator"
android:layout_marginTop="3dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="20dp"
android:maxLines="3"
android:drawablePadding="10dp"
android:ellipsize="end"
android:text="Description"
android:textSize="16sp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
HERE IS MY XML ACTIVITY LAYOUT CODE :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserLoginActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center"
android:layout_gravity="center"
android:overScrollMode="never"
android:clipToPadding="false">
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
onClick listener of cardView
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int pos=viewPager.getCurrentItem();
//used switch case
switch (pos) {
case 0:
//Do whatever you want
break;
case 1:
//Do whatever you want
break;
}
});
I'm using ViewPager as image slider. I'm using Android Studio. Everything works fine, I can slide between images. However beneath my images I have for some reason white space. I know that you can't call wrap_content on ViewPager and I've tried to put the fragment which contains the image slider into an Activity with another fragment below the image-slider-fragment, but between them is still some weird white space and within the white space I can also swipe to another image, which would of course cause only problems later on:
fragment_home.xml:
<FrameLayout 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"
tools:context="HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="ImageSliderFragment"
android:id="#+id/fragment1"
tools:layout="#layout/fragment_image_slider"
android:layout_weight="2"/>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="BottomFragment"
tools:layout="#layout/fragment_bottom"
android:id="#+id/fragment2"
android:layout_weight="2"/>
</LinearLayout>
</FrameLayout>
swipe_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view_of_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<!-- adjustviewBounds prevents non-defined padding -->
<TextView
android:id="#+id/text_view_of_swipe_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</RelativeLayout>
fragment_image_slider.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="ImageSliderFragment"
android:id="#+id/image_slide_id">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager_of_image_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--height="wrap_content/match_parent" both have the same result -->
</FrameLayout>
CustomSliderAdapter.java:
public class CustomSliderAdapter extends PagerAdapter {
private int[] image_res = {R.drawable.a,R.drawable.b,
R.drawable.c, R.drawable.d};
private String[] image_names = {"a", "b", "c", "d"};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSliderAdapter(Context ctx) {
this.ctx = ctx;
}
#Override
public int getCount() {
return image_res.length;
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == (RelativeLayout) object);
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view_of_slider);
TextView textView = (TextView) view.findViewById(R.id.text_view_of_swipe_layout);
imageView.setImageResource(image_res[position]);
textView.setText(image_names[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
ImageSliderFragment.class:
public class ImageSliderFragment extends Fragment {
ViewPager viewPager;
CustomSliderAdapter adapter;
View myView;
public static int imageHeight;
public ImageSliderFragment() {}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(myView==null) {
myView = inflater.inflate(R.layout.fragment_image_slider, container, false);
viewPager = (ViewPager) myView.findViewById(R.id.view_pager_of_image_slider);
adapter = new CustomSliderAdapter(getActivity());
//With this I could set height of ViewPager manually
ViewGroup.LayoutParams params = viewPager.getLayoutParams();
params.height = imageHeight;
viewPager.setLayoutParams(params);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setAdapter(adapter);
} else {
((ViewGroup) myView.getParent()).removeView(myView);
}
return myView;
}
}
How activity looks
I've also tried to change the height of my xml file with numbers (f.e.200dp), which works, but there are so many different device screens that at some point it would get at some point messed up
So again my problem is: make ViewPager fit to Imageview,because I can swipe between images also in the white space.
Help is much appreciated!
You can try like this on your imageview for viewpager image ....
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
I added a ViewPageIndicator inside recylcerview item, this is recyclerview item layout.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/background"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
android:fillViewport="true">
<LinearLayout
android:id="#+id/main_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="100dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/pager_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
ViewPageIndicator is not showing when scrolling down, but when I'm scrolling up it shows on some items, please help.
Issue was on the viewpager adapter, i used both FragmentStatePagerAdapter and FragmentPagerAdapter but no sucess. so finally change the adapter to PagerAdapter and my fragment view implemnted in instantiateItem in PagerAdapter
public class FeedImageAdapter extends PagerAdapter {
private ArrayList<FeedContentGroup> feedContentGroups;
private Context mContext;
private LayoutInflater layoutInflater;
public FeedImageAdapter(Context context, ArrayList<FeedContentGroup> feedContentGroups) {
this.mContext = context;
this.feedContentGroups = feedContentGroups;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = layoutInflater.inflate(R.layout.feed_image_view, null);
ImageView image1 = (ImageView) v.findViewById(R.id.image1);
FeedContentGroup mFeedContentGroup = feedContentGroups.get(position);
if (mFeedContentGroup.getContent1() != null && mFeedContentGroup.getContent1().getThumbnail_file_path() != null) {
Glide.with(mContext).load(mFeedContentGroup.getContent1().getThumbnail_file_path()).into(image1);
} else {
image1.setVisibility(View.GONE);
}
((ViewPager) collection).addView(v, 0);
return v;
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public int getCount() {
return feedContentGroups.size();
}
}
I am trying to create a ViewPager that shows a list of ImageViews. I am able to implement the pager and adapter properly, and it works as expected on swiping. However, it does not show the content of the ImageView properly, i.e. the image itself. Its as if the images are there, but they are transparent. Only one image is visible out of the whole lot.
Here is my code-
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
List<ImageView> views = new ArrayList<>();
boolean[] done = {false,false,false,false,false};
ItemDetailFragment idf;
public ImagePagerAdapter(ItemDetailFragment idf,
LayoutInflater inflater) {
this.idf = idf;
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(idf.getActivity());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
public ImagePagerAdapter(LayoutInflater inflater) {
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(inflater.getContext());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (done[position]) {
return views.get(position);
}
ImageView v = views.get(position);
views.set(position, v);
((ViewPager) container).addView(v);
done[position] = true;
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return true;
}
}
ItemDetailFragment.java : This is where I set the adapter.
public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
pager = (ViewPager) rootView.findViewById(R.id.pager);
adapter = new ImagePagerAdaper(this, inflater);
pager.setAdapter(adapter);
}
fragment_item_detail.xml
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
tools:context="com.trial.piclist.ItemDetailFragment" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >
</android.support.v4.view.ViewPager>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/item_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textIsSelectable="true" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/controls_layout" />
</TableRow>
<ScrollView
android:id="#+id/descScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please Help. Thanks.
Use:
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
act = a;
count=c;
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return count;
}
#Override
public Object instantiateItem(View collection, int position) {
View v1 = Inflater.inflate(R.layout.element_image, null);
ImageView image = (ImageView) v1
.findViewById(R.id.about_image);
image.setImageResource(R.drawable.light_grey_background);
((ViewPager) collection).addView(v1, 0);
return v1;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
Make a layout element_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Replace
adapter = new ImagePagerAdaper(this, inflater);
with
adapter = new ImagePagerAdaper(getActivity(), count_of_images);
initiate the ImageView in the instantiateItem() method, set the LayoutParams of the imageView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
Also set the bg colour for debugging
imageView.setBackgroundColor(Color.GREEN);
viewPager.setBackgroundColor(Color.RED);
I'm not sure how to integrate both these pagers in a single view. What I wanna do is, the images should have the ability to be swiped both vertically and horizontally. This screen would have a page indicator in the screen.
scrollView = (ScrollView) findViewById(R.id.scroll_view);
contentView = (ViewGroup) findViewById(R.id.content);
scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView));
scrollView.post(new Runnable()
{
public void run()
{
scrollView.scrollTo(0, contentView.getPaddingTop());
}
});
final PagerControl control = (PagerControl) findViewById(R.id.control);
final HorizontalPager pager = (HorizontalPager) findViewById(R.id.pager);
control.setNumPages(pager.getChildCount());
pager.addOnScrollListener(new HorizontalPager.OnScrollListener() {
public void onScroll(int scrollX) {
//Log.d("TestActivity", "scrollX=" + scrollX);
float scale = (float) (pager.getPageWidth() * pager.getChildCount()) / (float) control.getWidth();
control.setPosition((int) (scrollX / scale));
}
public void onViewScrollFinished(int currentPage) {
//Log.d("TestActivity", "viewIndex=" + currentPage);
control.setCurrentPage(currentPage);
}
});
and xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/com.example.page"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.page.HorizontalPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
example:pageWidth="250dip"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="200dip"
android:paddingBottom="200dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#0a0"
android:text="Text 1"
android:focusable="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#00a"
android:text="Text 2"
android:focusable="true"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Text 3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#a00"
android:text="Text 4"
android:focusable="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#0aa"
android:text="Text 5"
android:focusable="true"
/>
</ScrollView>
</com.example.page.HorizontalPager>
<com.example.page.PagerControl
android:id="#+id/control"
android:layout_width="fill_parent"
android:layout_height="4dip"
android:layout_margin="8dip"
example:roundRectRadius="2dip"
/>
</LinearLayout>
so, here's my code:
viewpager in xml of main layout:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
code for view pager:
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
List<View> pages = new ArrayList<View>();
// add here all views that you need
pages.add(view_1);
pages.add(view_2);
...
pages.add(view_N);
///////////
MyPageAdapter pageAdapter = new MyPageAdapter(pages);
viewPager.setAdapter(pageAdapter);
viewPager.setCurrentItem(0);
and MyPageAdapter:
public class MyPageAdapter extends PagerAdapter{
List<View> pages = null;
public MyPageAdapter(List<View> pages){
this.pages = pages;
}
#Override
public Object instantiateItem(View collection, int position){
View v = pages.get(position);
((ViewPager) collection).addView(v, 0);
return v;
}
#Override
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((View) view);
}
#Override
public int getCount(){
return pages.size();
}
#Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
#Override
public void finishUpdate(View arg0){
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1){
}
#Override
public Parcelable saveState(){
return null;
}
#Override
public void startUpdate(View arg0){
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
you may check this web out http://vision-apps.blogspot.com/2013/06/solution-for-android-viewpager-inside.html
I hope it will help you.