2014-05-09 19 views
5

Tôi đã theo dõi thisthat câu trả lời, tôi cũng tìm thấy liên kết this.Chế độ xem có chiều cao động không hoạt động (luôn sử dụng chiều cao của đoạn đầu tiên)

Tôi sử dụng các tài nguyên đó để thực hiện thử nghiệm & lỗi. Bây giờ, tùy chỉnh ViewPager tùy chỉnh nội dung của nó thành công, nhưng chỉ hiển thị nội dung đầu tiên. FYI, số ViewPager của tôi giữ một số chế độ xem phức tạp, như ExpendableListView - đó là lý do tại sao không có mã nào trong các tài nguyên đó hoạt động hoàn hảo, tôi cần phải tự sửa đổi mã.

Tôi có 4 mảnh (nội dung), do đó, tôi sử dụng pager.setOffscreenPageLimit(3);

Đây là tùy chỉnh của tôi ViewPager:

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

    public int measureFragment(View view) { 
     if (view == null) 
      return 0; 

     view.measure(0, 0); 
     return view.getMeasuredHeight(); 
    } 
} 

Đây là tôi CustomPagerTabStrip:

int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); 
super.onMeasure(widthMeasureSpec, expandSpec); 
android.view.ViewGroup.LayoutParams params = getLayoutParams(); 
params.height = getMeasuredHeight(); 

Và đây là XML của tôi :

 <RelativeLayout 
      android:id="@+id/relativePager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <com.xxx.yyy.util.CustomViewPager 
       android:id="@+id/detailPager" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <com.xxx.yyy.util.CustomPagerTabStrip 
        android:id="@+id/detailTab" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#161C28" 
        android:textColor="#FFFFFF" /> 
      </com.xxx.yyy.util.CustomViewPager> 

     </RelativeLayout> 

Ví dụ trường hợp:

Nếu chiều cao đoạn đầu tiên là 100px, sau đó tất cả các mảnh vỡ khác sẽ ở lại trên 100px. Vì vậy, nếu chiều cao của đoạn thứ hai là 130px, nó sẽ được cắt thành 100px.

Khi tôi đã cố gắng để gỡ lỗi, các onMeasure trên tôi CustomViewPager luôn gọi 4 lần khi Activity được tạo ra, nhưng tất cả đều chỉ đọc đoạn đầu tiên. onMeasurekhông bao giờ nhận được gọi lại sau đó, ngay cả khi tôi thay đổi (trượt sang trái/phải) từ đoạn này sang đoạn khác.

Hãy giúp tôi, tôi đã dành nhiều giờ để làm việc này. Chỉ cần hỏi tôi nếu bạn cần thêm thông tin, Cảm ơn bạn đã dành thời gian.

+0

Bất kỳ trợ giúp nào? –

+0

Xin chào Tôi muốn biết, cách bạn giải quyết vấn đề .. –

+0

@RenjithKN Rất tiếc, tôi không thể giải quyết vấn đề này vì vậy tôi sử dụng cách tiếp cận khác (không sử dụng một số chế độ xem phức tạp trong máy xem): ( –

Trả lời

14

Hi Blaze tìm thấy một số điều sử dụng đầy đủ, giải pháp sau đây đang làm việc cho tôi.

Nó bao gồm chế độ xem cuộn tùy chỉnh và máy nhắn tin chế độ xem tùy chỉnh. Mục đích của chế độ xem tùy chỉnh mục đích là tính chiều cao theo chiều cao của con, hieght được thiết lập để bọc nội dung ban đầu và mục đích của chế độ xem cuộn là xử lý sự kiện chạm, nếu người dùng cuộn màn hình theo chiều dọc. thậm chí để cuộn và do đó người dùng có thể cuộn trang.

xem Tuỳ chỉnh cuộn

public class CustomScrollView extends ScrollView { 

private GestureDetector mGestureDetector; 

public CustomScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mGestureDetector = new GestureDetector(context, new YScrollDetector()); 
    setFadingEdgeLength(0); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    return super.onInterceptTouchEvent(ev) 
      && mGestureDetector.onTouchEvent(ev); 
} 

// Return false if we're scrolling in the x direction 
class YScrollDetector extends SimpleOnGestureListener { 
    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, 
      float distanceX, float distanceY) { 
     return (Math.abs(distanceY) > Math.abs(distanceX)); 
    } 
} 
} 

xem Tuỳ chỉnh pager

public class CustomPager extends ViewPager{ 

public CustomPager (Context context) { 
    super(context); 
} 

public CustomPager (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); 
} 

public int measureFragment(View view) { 
    if (view == null) 
     return 0; 

    view.measure(0, 0); 
    return view.getMeasuredHeight(); 
} 
} 

tập tin Layout

<com.example.demo2.activity.widget.CustomScrollView 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" > 

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

    <TextView 
     style="@style/text" 
     android:text="Text 1" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 2" /> 


     <com.example.demo2.activity.widget.CustomPager 
      android:id="@+id/myViewPager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 4" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 5" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 6" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 7" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 8" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 9" /> 

    <View style="@style/text_divider" /> 

    <TextView 
     style="@style/text" 
     android:text="Text 10" /> 
</LinearLayout> 

Hoạt động

public class MainActivity extends FragmentActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager()); 
    ViewPager pager = (ViewPager)findViewById(R.id.myViewPager); 
    pager.setAdapter(pageAdapter); 

} 



} 

bộ chuyển đổi Fragment

mảnh với độ cao khác nhau được thêm vào ViewPager, FragmentBlue, Green vv chỉ là mảnh dùng để thêm vào ViewPager bạn có thể sử dụng riêng của bạn các mảnh, một điều cần nhớ là bố cục bên ngoài của chúng phải giống nhau trong trường hợp của tôi, tôi đã sử dụng bố cục lót cho tất cả

public class MyPagerAdapter extends FragmentPagerAdapter { 

private List<Fragment> fragments; 

public MyPagerAdapter(FragmentManager fm) { 
    super(fm); 
    this.fragments = new ArrayList<Fragment>(); 
    fragments.add(new FragmentBlue()); 
    fragments.add(new FragmentGreen()); 
    fragments.add(new FragmentPink()); 
    fragments.add(new FragmentRed()); 
} 

@Override 
public Fragment getItem(int position) { 
    return fragments.get(position); 
} 

@Override 
public int getCount() { 
    return fragments.size(); 
} 
} 
+0

Cảm ơn rất nhiều! Tôi có thể cần điều này trong tương lai: D –

+0

OMG !!!! Cảm ơn bạn rất nhiều!!!! ;) – juliobetta

+0

không hoạt động. Có cùng mã ở đây https://github.com/VihaanVerma89/DynamicViewPager –

3
public CustomPager (Context context) { 
    super(context); 
} 

public CustomPager (Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

int getMeasureExactly(View child, int widthMeasureSpec) { 
    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    int height = child.getMeasuredHeight(); 
    return MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
} 

@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); 
    if (tab == null) { 
     return; 
    } 

    int width = getMeasuredWidth(); 
    if (wrapHeight) { 
     // Keep the current measured width. 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
    } 
    Fragment fragment = ((Fragment) getAdapter().instantiateItem(this, getCurrentItem())); 
    heightMeasureSpec = getMeasureExactly(fragment.getView(), widthMeasureSpec); 

    //Log.i(Constants.TAG, "item :" + getCurrentItem() + "|height" + heightMeasureSpec); 
    // super has to be called again so the new specs are treated as 
    // exact measurements. 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
Các vấn đề liên quan