Using viewpager for scrolling children inside a view - android

I have a menu which looks like this http://flashspeaksactionscript.com/wp-content/uploads/2008/06/vert-menu18.jpg Considering the child as that "3" in the picture I would like to be able to swipe that "3" to the left and see be able to see another item.
Here's my xml code:
<LinearLayout
android:id="#+id/llayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp" >
<Button
android:id="#+id/Sum"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
<Button
android:id="#+id/nana"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
<Button
android:id="#+id/mama"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/example_get_by_id"
android:layout_width
etc. These are the children. For example I want to be able to swipe through these buttons, just because I cannot use HorizontalScrollViewbecause my application is consisted of fragments and already has a swipe ability to surf through fragments. The swipe ability for those fragments and HorizontalScrollView get mixed up so, that's why I need to be able to use Viewpager for single items and not the whole page. Is there any at least an example for this? I couldn't find anything.
Thanks in advance.

have a look this may be useful to you:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6, R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8, R.drawable.ic_launcher };
}
and ViewPagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
#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;
}
}
xml file:
<LinearLayout 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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" >
</ImageView>
</android.support.v4.view.ViewPager>
</LinearLayout>

Related

How to create hover text on image slider android?

I have an image slider with the bottom code:
public class MainActivity extends Activity {
private class ImagePagerAdapter extends PagerAdapter {
private final int[] mImages = new int[] {
R.drawable.chiang_mai,
R.drawable.himeji,
R.drawable.petronas_twin_tower,
R.drawable.ulm
};
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public int getCount() {
return this.mImages.length;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Context context = MainActivity.this;
final ImageView imageView = new ImageView(context);
final int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(this.mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public boolean isViewFromObject(final View view, final Object object) {
return view == ((ImageView) object);
}
}
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
final CirclePageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circleIndicator.setViewPager(viewPager);
final TextView tvText = (TextView) findViewById(R.id.text);
tvText.setText(getResources().getString(R.string.bodyText));
}
}
And this is the XML code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
So I need to set a text Hover on the Viewpager so that when I swipe it, it will show up for a specific picture. How can I create a hover text on this image slider while making it run automatically for some specific time, and after a swipe show a hover text.
Have you just one XML Layout? If you've two it makes it quite alot easier to work with.
So this layout below is where the actual image slider will be place on the Home Fragment in this case.
<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"
tools:context="com.example.****.****.HomeFragment"
android:background="#000000"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="204dp"
android:scaleType="centerCrop"
></android.support.v4.view.ViewPager>
Secondly, this XML Layout below is the Layout for the ViewPager itself. As you can see it is made up of an ImageView and a TextView. With the TextView placed on top of the ImageView.
Hope this helps a bit.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/image_view"
android:layout_alignTop="#+id/image_view"
android:layout_alignRight="#+id/image_view"
android:layout_alignBottom="#+id/image_view"
android:layout_margin="1dp"
android:gravity="bottom"
android:textSize="35dp"
android:text="******"
android:textStyle="bold"
android:textColor="#17baef" />
</RelativeLayout>

VIewPager with two items - Android

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

ScrollView inside ViewPager

Please Help me.
I searched everywhere how I can make a ScrollView inside a ViewPager but it was not successfully. The hole layout of the ViewPager should be scrollable.
Important: I only want a ScrollView in the vertical.
Already now I want to say thanks for the answers!
My code
"Main"
public class Rezepte2 extends Fragment {
// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.rezepte_fragment, container, false);
// Generate sample data
rezeptTitel = new String[] { "Nudeln","Fleisch" };
naehrwerte = new String[] { "50kcal","70kcal" };
zubereitung = new String[] { "ganz easy","voll easy" };
rezeptImage = new int[] { R.drawable.ic_action_next_item, R.drawable.ic_action_next_item,
R.drawable.ic_launcher, R.drawable.ic_action_next_item };
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(getActivity(), rezeptTitel, naehrwerte, zubereitung, rezeptImage);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
return layout;
}
Adapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImages;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] rezeptTitel, String[] naehrwerte,
String[] zubereitung, int[] rezeptImages) {
this.context = context;
this.rezeptTitel = rezeptTitel;
this.naehrwerte = naehrwerte;
this.zubereitung = zubereitung;
this.rezeptImages = rezeptImages;
}
#Override
public int getCount() {
return rezeptTitel.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtrezeptTitel;
TextView txtnaehrwerte;
TextView txtzubereitung;
ImageView imagerezept;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.rezepte_fragment_basic, container,
false);
// Locate the TextViews in viewpager_item.xml
txtrezeptTitel = (TextView) itemView.findViewById(R.id.rezeptTitel);
txtnaehrwerte = (TextView) itemView.findViewById(R.id.naehrwerte);
txtzubereitung = (TextView) itemView.findViewById(R.id.zubereitung);
// Capture position and set to the TextViews
txtrezeptTitel.setText(rezeptTitel[position]);
txtnaehrwerte.setText(naehrwerte[position]);
txtzubereitung.setText(zubereitung[position]);
// Locate the ImageView in viewpager_item.xml
imagerezept = (ImageView) itemView.findViewById(R.id.image_rezepte_basic);
// Capture position and set to the ImageView
imagerezept.setImageResource(rezeptImages[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
SwipeView xml
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>`
ViewPager fragment xml
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rezeptTitel"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:id="#+id/image_rezepte_basic"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/naehrwerte" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/zubereitung" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>ยด
maybe you're just not scrolling correctly - an emulator doesn't really have a touch screen. You have to touch and hold part of it while panning with another finger

How to enable full page swiping in viewpager?

How can I make swipe available in the middle of the screen on the VIewPager? Like on the app store, you can swipe anywhere to go to the next tab! Currently that happens only when I swipe from the extreme corner of the screen(Bezel Swipe)
MY code:
public class SwipeFragment extends Fragment {
ViewPager viewPager;
SlidingTabLayout slidingTabLayout;
ArrayList<String> tabs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.swipe_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
tabs = new ArrayList<String>();
tabs.add("Image");
tabs.add("Something Else");
tabs.add("Some Long Title");
tabs.add("Songs");
tabs.add("Off The Page!");
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new VPAdapterHelper());
slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.slidingTabs);
slidingTabLayout.setViewPager(viewPager);
}
public class VPAdapterHelper extends PagerAdapter {
#Override
public int getCount() {
return tabs.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, container, false);
container.addView(view);
ListView listView = (ListView) view.findViewById(R.id.VP_ListView);
// TODO set different adapters for different tabs
listView.setAdapter(new AdapterHelper(context));
return view;
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return obj == view;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs.get(position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
swipe_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.nischal.test.SlidingTabLayout
android:id="#+id/slidingTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
pager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/VP_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I want to be able to swipe to different tabs from anywhere on the page.
I tried overriding the DragListner of the container and dispatched a draylistner to the viewpage, but did not help!
Any help will be appreciated!
Found the answer to my own question. The problem seems to be in the listview item layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingStart="8dip"
android:paddingEnd="8dip">
<ImageView
android:id="#+id/CL_imageView"
android:layout_width="48dp"
android:layout_height="48dp" />
<TextView
android:id="#+id/CL_textView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:ellipsize="end"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:singleLine="true"
android:text="New Text"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
here, the textview gravity tag was set to center, which for some reason caused the whole problem. removed the gravity tag and problem solved!

How to combine horizontal pager and vertical pager in a single view in android?

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.

Categories

Resources