2015-06-17 9 views
7

ViewPaper के साथ काम करते समय मुझे समस्याएं मिलीं। जब मैं व्यूपेपर के height = wrap_content सेट करता हूं तो यह कुछ भी नहीं दिखाता है और यह दिखाता है कि क्या मैं ViewPager (उदा: android:layout_height= "350dp") के लिए ऊंचाई निर्धारित करता हूं। कृपया सहायता कीजिए! धन्यवाद!ViewPager काम नहीं कर रहा है अगर सेट लेआउट_हेइट = wrap_content

यह मेरा एक्सएमएल कोड है:

<?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="match_parent" 
    android:background="@color/white" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:scrollbars="none" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:background="#FF0000" 
       android:gravity="center" 
       android:text="Quick Links" 
       android:textColor="@color/white" 
       android:textSize="20sp" /> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

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

       <com.viewpagerindicator.CirclePageIndicator 
        android:id="@+id/circlePage" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignBottom="@+id/pager" 
        android:layout_marginBottom="10dp" > 
       </com.viewpagerindicator.CirclePageIndicator> 
      </RelativeLayout> 

      <com.custom.ExpandableHeightListView 
       android:id="@+id/lvAdmissions" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/white" 
       android:divider="@drawable/bg_listview_divider" 
       android:dividerHeight="1dp" 
       android:listSelector="@drawable/selector_animation" > 
      </com.custom.ExpandableHeightListView> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

हल: मैं इस लिंक का अनुसरण और यह मुझे इस समस्या का समाधान कर मदद करते हैं। https://stackoverflow.com/a/24666987/1928560

उत्तर

1

हाय वहाँ, मैं इसका भी उपयोग कर रहा हूं, कृपया इसे आज़माएं और देखें कि यह आपकी समस्या का हल करता है या नहीं।

<LinearLayout 
    android:id="@+id/container_app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 
+0

यह काम नहीं किया। मुझे उम्मीद है कि इसमें इस पर पेज सूचक शामिल होना चाहिए। वैसे भी, आपकी टिप्पणी के लिए धन्यवाद। –

10

ViewPager ऊंचाई या तो match_parent निर्दिष्ट किया जाना चाहिए या आप इसके लिए एक मूल्य निर्दिष्ट करना होगा। दो समाधान,

  1. उपयोग वजन के अंदर अपने वर्ग फ़ाइल में LinearLayout या
  2. उपयोग onMeasure विधि अवधारणा। देखें Android: I am unable to have ViewPager WRAP_CONTENT

इस तरह आप अपने LinearLayout

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="#FF0000" 
      android:gravity="center" 
      android:text="Quick Links" 
      android:textColor="@color/white" 
      android:textSize="20sp" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

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

      <com.viewpagerindicator.CirclePageIndicator 
       android:id="@+id/circlePage" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/pager" 
       android:layout_marginBottom="10dp" > 
      </com.viewpagerindicator.CirclePageIndicator> 
     </RelativeLayout> 

     <com.custom.ExpandableHeightListView 
      android:id="@+id/lvAdmissions" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@color/white" 
      android:divider="@drawable/bg_listview_divider" 
      android:dividerHeight="1dp" 
      android:listSelector="@drawable/selector_animation" > 
     </com.custom.ExpandableHeightListView> 
    </LinearLayout> 

इस तरह से वजन का उपयोग कर सकते अपने viewpager लेआउट और ExpandableListView शेयरों बराबर ऊंचाई है। आप वजन मान

+0

धन्यवाद, यह अभी काम करता है। बस मेरी पोस्ट अपडेट की गई। –

-2

बदलकर अनुपात बदल सकते हैं हम व्यूपेजर को अपने वर्तमान पृष्ठ आकार में व्यूपेजर का आकार बदलने के लिए अनुकूलित कर सकते हैं। आप नीचे दिए गए कोड का उपयोग कर सकते हैं।

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 { 
      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(); 
    } 
} 
इसके बजाय android.support.v4.view.ViewPager WrapContentViewPager का उपयोग का उपयोग करने का XML में

<WrapContentViewPager 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</WrapContentViewPager> 

पेज स्वाइप पर कॉल reMeasureCurrentpage फ़ंक्शन कॉल करें।

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) { 

      } 
     }); 
+1

http://stackoverflow.com/a/34524240/3758898 –

1

=> 100% काम करते समय जब दर्शक ऊंचाई सेट wrap_content।

public class FollowUseWrapHeightViewPager extends ViewPager { 
public FollowUseWrapHeightViewPager(Context context) { 
    super(context); 
} 

public FollowUseWrapHeightViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int height = 0; 
    View view = null; 
    for(int i = 0; i < getChildCount(); i++) { 
     view = getChildAt(i); 
     view.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = view.getMeasuredHeight(); 
     if(h > height) height = h; 
    } 

    if (height != 0) { 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); 
} 

/** 
* Determines the height of this view 
* 
* @param measureSpec A measureSpec packed into an int 
* @param view the base view with already measured height 
* 
* @return The height of the view, honoring constraints from measureSpec 
*/ 
private int measureHeight(int measureSpec, View view) { 
    int result = 0; 
    int specMode = MeasureSpec.getMode(measureSpec); 
    int specSize = MeasureSpec.getSize(measureSpec); 

    if (specMode == MeasureSpec.EXACTLY) { 
     result = specSize; 
    } else { 
     // set the height from the base view if available 
     if (view != null) { 
      result = view.getMeasuredHeight(); 
     } 
     if (specMode == MeasureSpec.AT_MOST) { 
      result = Math.min(result, specSize); 
     } 
    } 
    return result; 
} 
} 
+0

का डुप्लिकेट सभी बच्चे की ऊंचाई एक-एक करके प्राप्त होती है और इसकी ऊंचाई को viepager में सेट करती है –

संबंधित मुद्दे