Tôi đã theo dõi this và that 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. onMeasure
khô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.
Bất kỳ trợ giúp nào? –
Xin chào Tôi muốn biết, cách bạn giải quyết vấn đề .. –
@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): ( –