I am having view pager with 4 different fragments. In this, 3 fragments only contains webview. All the fragments load different contents. I unable to set a height based on the loaded content. For that, I wrote a custom viewpager to measure height of viewpager. For the first viewpager fragment, height is calculated properly. But when navigate to second or third fragment, viewpager height is not calculated accurately. I don't know, what is the issue is in my code and what i need to change to calculate proper height for each fragment after navigate to that view pager page. Here is my view pager and pager adapter codes. Kindly help me.
Custom View pager:
public class CustomViewPager extends ViewPager {
public CustomViewPager (Context context) {
super(context);
}
public CustomViewPager (Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
final View tab = getChildAt(0);
int width = getMeasuredWidth();
int tabHeight = tab.getMeasuredHeight();
if (wrapHeight) {
// Keep the current measured width.
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
}
int fragmentHeight = measureFragment(((Fragment) getAdapter().instantiateItem(this, getCurrentItem())).getView());
heightMeasureSpec = MeasureSpec.makeMeasureSpec(tabHeight + fragmentHeight + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
#Override
public void setOnPageChangeListener(OnPageChangeListener listener) {
requestLayout();
invalidate();
super.setOnPageChangeListener(listener);
}
#Override
public void addOnPageChangeListener(OnPageChangeListener listener) {
super.addOnPageChangeListener(listener);
}
public int measureFragment(View view) {
if (view == null)
return 0;
view.measure(0, 0);
return view.getMeasuredHeight();
}
}
PagerAdapter:
public class MerchantsCataAdapter extends FragmentPagerAdapter {
Context context;
Merchant merchant;
Fragment fragment;
private int mCurrentPosition = -1;
private String[] tabs = {"Home", "About Us","Promotions","Store Information"};
public MerchantsCataAdapter(FragmentManager fm, Context context,Merchant merchant) {
super(fm);
this.context = context;
this.merchant = merchant;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
fragment= new HomeContentFragment().newInstance(merchant.getCompany_home_content(),merchant.getVideo_url());
break;
case 1:
fragment= new AboutusFragment().newInstance(merchant.getFull_description(),merchant.getVideo_url());
break;
case 2:
fragment= new AboutusFragment().newInstance(merchant.getCompany_promotions(),merchant.getVideo_url());
break;
case 3:
fragment= new FragmentStoreInformation().newInstance(merchant.getMerchantId(),merchant.getName());
break;
}
return fragment;
}
#Override
public int getCount() {
return tabs.length;
}
public String getTabView(int position) {
return tabs[position];
}
Finally, I find the answer. Place below property in Webview in xml. This will take webview height automatically based on the content.
android:scrollbarStyle="insideOverlay"
Related
I have a page in viewpager which have a button and a few views which are hidden by default. On click of the button, the views are to be toggled(Show/Hide). My issue is button click is working, but viewpager's height does not changes.
This is the code for adapter:
public class HomeCategoryAdapter extends PagerAdapter implements View.OnClickListener {
private LayoutInflater inflater;
private View lin_view_more, lin_view_less, lin_view_1, lin_view_2, lin_view_3, lin_view_4;
private WrapContentViewPager pager;
public HomeCategoryAdapter(Context context) {
this.mContext = context;
this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return 5;
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
pager = (EnhancedWrapContentViewPager) container;
View itemView = inflater.inflate(R.layout.homescreen_category_adapter, container, false);
...
lin_view_more = itemView.findViewById(R.id.lin_view_more);
lin_view_more.setOnClickListener(this);
lin_view_1 = itemView.findViewById(R.id.lin_view_1);
lin_view_1.setOnClickListener(this);
lin_view_2 = itemView.findViewById(R.id.lin_view_2);
lin_view_2.setOnClickListener(this);
lin_view_3 = itemView.findViewById(R.id.lin_view_3);
lin_view_3.setOnClickListener(this);
lin_view_4 = itemView.findViewById(R.id.lin_view_4);
lin_view_4.setOnClickListener(this);
switch (position) {
case 0:
parent_1.setVisibility(View.VISIBLE);
parent_2.setVisibility(View.GONE);
parent_3.setVisibility(View.GONE);
parent_4.setVisibility(View.GONE);
parent_5.setVisibility(View.GONE);
break;
case 1:
parent_1.setVisibility(View.GONE);
parent_2.setVisibility(View.VISIBLE);
parent_3.setVisibility(View.GONE);
parent_4.setVisibility(View.GONE);
parent_5.setVisibility(View.GONE);
break;
case 2:
parent_1.setVisibility(View.GONE);
parent_2.setVisibility(View.GONE);
parent_3.setVisibility(View.VISIBLE);
parent_4.setVisibility(View.GONE);
parent_5.setVisibility(View.GONE);
break;
case 3:
parent_1.setVisibility(View.GONE);
parent_2.setVisibility(View.GONE);
parent_3.setVisibility(View.GONE);
parent_4.setVisibility(View.VISIBLE);
parent_5.setVisibility(View.GONE);
break;
case 4:
parent_1.setVisibility(View.GONE);
parent_2.setVisibility(View.GONE);
parent_3.setVisibility(View.GONE);
parent_4.setVisibility(View.GONE);
parent_5.setVisibility(View.VISIBLE);
break;
}
...
((WrapContentViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((WrapContentViewPager) container).removeView((View) object);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.lin_view_more:
lin_view_more.setVisibility(View.GONE);
lin_view_1.setVisibility(View.VISIBLE);
lin_view_2.setVisibility(View.VISIBLE);
lin_view_3.setVisibility(View.VISIBLE);
lin_view_4.setVisibility(View.VISIBLE);
lin_view_less.setVisibility(View.VISIBLE);
if (pager != null) {
pager.measureView();
}
break;
case R.id.lin_view_less:
lin_view_more.setVisibility(View.VISIBLE);
lin_view_1.setVisibility(View.GONE);
lin_view_2.setVisibility(View.GONE);
lin_view_3.setVisibility(View.GONE);
lin_view_4.setVisibility(View.GONE);
lin_view_less.setVisibility(View.GONE);
if (pager != null) {
pager.post(new Runnable() {
#Override
public void run() {
pager.measureView();
}
});
}
break;
}
}
}
This is the code for modified view pager to make it wrap_content:
public class WrapContentViewPager extends ViewPager {
public WrapContentViewPager (Context context) {
super(context);
}
public WrapContentViewPager (Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mode = MeasureSpec.getMode(heightMeasureSpec);
if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int childMeasuredHeight = child.getMeasuredHeight();
if (childMeasuredHeight > height) {
Log.debugMessage("WrapContentViewPager", "page height updated");
height = childMeasuredHeight;
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void measureView(){
requestLayout();
}
}
As shown in onClick above, I am calling requestLayout() on pager. The page height gets calculated but the layout is not expanding.
Update:
If I swipe to another page(3rd or 4th) then go back to this page, show/hide are working fine.
Not getting why is it not working.
Can you add this code after requestLayout();code line:
invalidate();
if it doesn't work also can you try this:
postInvalidate();
For what these code lines do, you can take a look at this:
What does postInvalidate() do?
I would like to know if is it possible to have a PagerAdapter with wrap_content without using getChildCount() and getChildAt(i)?
I have checked the answer below, but since on my adapter doesn't have children it doesn't work.
https://stackoverflow.com/a/20784791/10020799
Does someone know how to make an wrap_content adapter in this case?
My PagerAdapter code is:
public class SectionsPagerAdapter extends PagerAdapter {
private Integer pagerTotal = 10;
public SectionsPagerAdapter(Context c) {
super();
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setPagerTotal(Integer item) {
pagerTotal = item;
}
#Override
public int getCount() {
return pagerTotal;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.home_top_page, null);
container.addView(layout);
topMain = (ImageView) layout.findViewById(R.id.top_main);
if(imageBitmapArray != null && imageBitmapArray.length > 0){
topMain.setImageBitmap(imageBitmapArray[position]);
}else{
if(intImageArray != null) {
topMain.setImageResource(R.drawable.top_main_01);
}
}
return layout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
You can resize the ViewPager to it's current page size on page swipe from this answer.
You can use the following code:
public class WrapContentViewPager extends ViewPager {
private int mCurrentPagePosition = 0;
public WrapContentViewPager(Context context) {
super(context);
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
if (wrapHeight) {
View child = getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
Use it in xml:
<your.package.name.WrapContentViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</your.package.name.WrapContentViewPager>
After that call reMeasureCurrentpage function on page swipe.
final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager);
wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
wrapContentViewPager.reMeasureCurrentPage(wrapContentViewPager.getCurrentItem());
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Note that a list can have hundreds of items then this is not good to use this wrap_content ViewPager.
Even with a not expandable adapter is possible to use getChildCount() and getChildAt(i) functions.
Based on Khemraj's answer and this post's answer, I was able to solve the problem with the following code:
class:
public class WrapContentViewPager extends ViewPager {
public WrapContentViewPager(Context context) {
super(context);
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
if (height != 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
xml:
<your.package.name.WrapContentViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</your.package.name.WrapContentViewPager>
on page swipe:
final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager);
wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I am using ViewPager with FragmentStatePageAdapter to show a number of views where user can go through. With current implementation only one page is changed at any single swipe. Instead I want to flip through multiple views based on user swipe. If swipe is long/fast i need to flip through multiple views and vice versa.
here is my current code
public class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return 10;
}
#Override
public Fragment getItem(int position) {
return ItemFragment.init(position);
}
}
PageContainer class
public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener {
private ViewPager mPager;
boolean mNeedsRedraw = false;
public PagerContainer(Context context) {
super(context);
init();
}
public PagerContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PagerContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//Disable clipping of children so non-selected pages are visible
setClipChildren(false);
//Child clipping doesn't work with hardware acceleration in Android 3.x/4.x
//You need to set this value here if using hardware acceleration in an
// application targeted at these releases.
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
#Override
protected void onFinishInflate() {
try {
mPager = (ViewPager) getChildAt(0);
mPager.setOnPageChangeListener(this);
} catch (Exception e) {
throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
}
}
public ViewPager getViewPager() {
return mPager;
}
private Point mCenter = new Point();
private Point mInitialTouch = new Point();
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenter.x = w / 2;
mCenter.y = h / 2;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
//We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitialTouch.x = (int)ev.getX();
mInitialTouch.y = (int)ev.getY();
default:
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
break;
}
return mPager.dispatchTouchEvent(ev);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Force the container to redraw on scrolling.
//Without this the outer pages render initially and then stay static
if (mNeedsRedraw) invalidate();
}
#Override
public void onPageSelected(int position) { }
#Override
public void onPageScrollStateChanged(int state) {
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
}
}
How can I do this in android?
Thanks
You can change the view by calling:
ViewPager.setCurrentItem(int index)
ViewPager.setCurrentItem(int index, bool animation)
You must handle the sliding commands yourself, but on a certain slide you cloud call setCurrentItem of the ViewPager. The setCurrentItem method also allows you to enable or disable animations.
I want to change the height of a ViewPager because it contains some childs with different height. I have read that wrap_content is not possible for ViewPager so I want to change the height of it in runtime.
I have tried this but it is not working:
pager.getLayoutParams().height=1000;
Could someone offer help please?
CustomViewPager.class
public class CustomViewPager extends ViewPager {
/**
* Constructor
*
* #param context
* the context
*/
public CustomViewPager(Context context) {
super(context);
}
/**
* Constructor
*
* #param context
* the context
* #param attrs
* the attribute set
*/
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
xml file:
<com.arsoft.ex.viewpager.CustomViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.arsoft.ex.viewpager.CustomViewPager>
MainClass:
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (CustomViewPager) findViewById(R.id.viewPager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
FragmentPagerAdapter:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Cook", "Recipe", "Comments" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: **pager.getLayoutParams().height=100;**
return CookCardFragment.newInstance(position, pager);
case 1: **pager.getLayoutParams().height=400;**
return RecipeCardFragment.newInstance(position, pager);
case 2: **pager.getLayoutParams().height=1000;**
return CookCardFragment.newInstance(position, pager);
}
return null;
}
}
I have seen this problem before.
Maybe you could use the fumction like this:
ViewPager vp = (CustomViewPager) findViewById(R.id.viewPager);
...
ViewGroup.LayoutParams params = vp.getLayoutParams();
params.height = 1000;
vp.setLayoutParams(params);
Check my answer, I was able to change the height of ViewPager when you change the page or change the height of it child
https://stackoverflow.com/a/25710358/1835245
I am trying to show horizontal scroll view with images to scroll in two directions.Previously for this i used Gallery View but galleryview is deprected ,i am using horizontal scroll view instead of gallery view but horizontal scroll view is different with gallery view.
Now i have to do two implementation
1) Scrolling in two directions continuously.
2) Center lock feature as same as gallery.
My screen looks like
you can use viewpager,
try this code and modify it as your need .
note : this not my code already found it on the net belong to:
( Dave Smith,#devunwired Date: 8/17/12 PagerActivity).
MainActivity:
public class MainActivity extends Activity{
PagerContainer mContainer;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (PagerContainer) findViewById(R.id.pager_container);
ViewPager pager = mContainer.getViewPager();
PagerAdapter adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
// Necessary or the pager will only have one extra page to show
// make this at least however many pages you can see
pager.setOffscreenPageLimit(adapter.getCount());
// A little space between pages
pager.setPageMargin(15);}
// Nothing special about this adapter, just throwing up colored views for
// demo
private class MyPagerAdapter extends PagerAdapter{
#Override
public Object instantiateItem(ViewGroup container, int position){
TextView view = new TextView(MainActivity.this);
view.setText("Item " + position);
view.setGravity(Gravity.CENTER);
view.setBackgroundColor(Color.argb(255, position * 50,
position * 10, position * 50));
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);}
#Override
public int getCount(){
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return (view == object);
}}}
PagerContainer:
public class PagerContainer extends FrameLayout implements
ViewPager.OnPageChangeListener {
private ViewPager mPager;
boolean mNeedsRedraw = false;
public PagerContainer(Context context) {
super(context);
init();
}
public PagerContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PagerContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setClipChildren(false);
}
#Override
protected void onFinishInflate() {
try {
mPager = (ViewPager) getChildAt(0);
mPager.setOnPageChangeListener(this);
} catch (Exception e) {
throw new IllegalStateException("The root child of PagerContainer must be a
ViewPager");
}
}
public ViewPager getViewPager() {
return mPager;
}
private Point mCenter = new Point();
private Point mInitialTouch = new Point();
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenter.x = w / 2;
mCenter.y = h / 2;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
//We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitialTouch.x = (int)ev.getX();
mInitialTouch.y = (int)ev.getY();
default:
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y -
mInitialTouch.y);
break;
}
return mPager.dispatchTouchEvent(ev);
}
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
//Force the container to redraw on scrolling.
//Without this the outer pages render initially and then stay static
if (mNeedsRedraw) invalidate();
}
public void onPageSelected(int position) { }
public void onPageScrollStateChanged(int state) {
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.testviewpager.PagerContainer
android:id="#+id/pager_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCC" >
<android.support.v4.view.ViewPager
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal" />
</com.example.testviewpager.PagerContainer>
</RelativeLayout>
hope help you.