ScrollView巢狀ListView listView展示及分頁處理
阿新 • • 發佈:2018-12-04
1.自定義ListView 重寫OnMeasure方法適應ScrollView:
public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context) { super(context); } public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public2:ScrollView滑動到底部ListView分頁處理:ListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 重寫該方法,達到使ListView適應ScrollView的效果 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
2.1:自定義ScrollView重寫onOverScrolled方法 :
public class PageListScrollView extends ScrollView { private OnScrollToBottomListener mOnScrollToBottomListener; public PageListScrollView(Context context) { super(context); } public PageListScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public PageListScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //滾動到底部時,clampedY變為true,此時將回調將狀態傳出去 @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); if (scrollY > 0 && mOnScrollToBottomListener != null) { mOnScrollToBottomListener.onScrollBottomListener(clampedY); } } public void setOnScrollToBottomListener(OnScrollToBottomListener listener) { mOnScrollToBottomListener = listener; } public interface OnScrollToBottomListener { void onScrollBottomListener(boolean isBottom); } }
3.在佈局檔案裡巢狀:
<PageListScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@drawable/divider" android:orientation="vertical" android:showDividers="middle"> <!--其他佈局-->
........
<ListViewForScrollView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout> </com.jingcai.apps.qualitydev.component.widget.PageListScrollView>
3.在activity中監聽:
@Override public void onScrollBottomListener(boolean isBottom) { if (isBottom) { //page+1 再次請求
} }