2014-05-09 17 views
5

this ve that cevaplarını takip ettim, ayrıca this linkini de buldum.Dinamik yükseklikte çalışmayan Viewpager (her zaman ilk parçanın yüksekliğini kullanın)

Bu kaynakları deneme & hatasını yapmak için kullanıyorum. Artık benim özel ViewPager içeriğiyle, içeriğini ölçüyor ancak sadece ilk görüntülenen. FYI, benim ViewPager benim ExpendableListView gibi bazı karmaşık görünümleri tutar - bu nedenle bu kaynaklardaki kodların hiçbiri mükemmel çalışmadığı için kodu kendim değiştirmem gerekir.

ben 4 fragmanlarını (içerik) sahip, bu yüzden bu benim özel ViewPager olduğunu pager.setOffscreenPageLimit(3);

kullanın: Bu benim CustomPagerTabStrip olduğunu

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

:

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

Ve bu benim XML :

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

örnek vaka:

ilk veridiliminin yüksekliği sonra tüm diğer parçaların 100 piksel kalacak, 100px ise. İkinci parçanın yüksekliği 130px ise 100 piksele kesilecektir. i hata ayıklamak için çalıştı

, Activity oluşturulur benim CustomViewPager hep denilen 4 kez onMeasure, ancak bunların hepsi sadece ilk parçasını okuyun. hiçbir zaman hiçbir zaman tekrar aramayı denemiyorum (sol/sağa) bir parçadan diğerine değişse bile.

Lütfen bana yardım edin, bu şeyin işe yaraması için çok zaman harcadım. Daha fazla bilgiye ihtiyacınız varsa bana sorun, Zaman ayırdığınız için teşekkürler.

+0

Herhangi bir yardım lütfen? –

+0

Merhaba, sorunu nasıl çözdüğünüzü biliyorum .. –

+0

@RenjithKN Ne yazık ki bu sorunu çözemedim bu yüzden farklı bir yaklaşım kullanıyorum (görüş cihazında bazı karmaşık görünümleri kullanmayın) :( –

cevap

14

Merhaba Blaze bazı şeyleri dolu dolu buldu, aşağıdaki çözüm benim için çalışıyor.

Özel kaydırma görünümü ve özel görünüm çağrı cihazı içerir. Amaca özel görüntüleme çağrı cihazı, yüksekliği, çocuğun boyuna göre dinamik olarak hesaplar, hiyerarlı içeriği başlangıçta sarmak için ayarlanır ve kaydırma görüntüsünün amacı dokunmatik olayı ele alır, kullanıcılar ekranı dikey olarak kaydırırsa dokunmayı geçmez kaydırmak ve böylece kullanıcı sayfayı kaydırmak için bile.

Özel kaydırma görünümü

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

Özel görünüm çağrı cihazı

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

Düzen dosya

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

Etkinlik

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

} 



} 

Fragment adaptör

farklı yükseklikleri

fragmanları, FragmentBlue, Yeşil vb sadece kendi kullanabilirsiniz viewpager eklemek için kullanılır fragmanlarında olan viewpager eklenir fragmanları, hatırlanması gereken bir şey, dış düzeninin benim durumumda aynı olması gerekir. Tüm liner düzenini kullanmıştım

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

Çok teşekkürler! Gelecekte buna ihtiyacım olabilir: D –

+0

OMG !!!! Çok teşekkür ederim!!!! ;) – juliobetta

+0

çalışmadı. Aynı kod burada olsun 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); 
} 
İlgili konular