列表嵌套滚动及显示问题
ScrollView嵌套ListView(GridView,ExpandableListView相同)时ListView一般都会显示不全,需要自定义ListView。
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
可以看到,关键就是重写onMeasure。
而且在某些情况下会有滚动冲突的情况,这时可以在ListView的xml中添加android:descendantFocusability="blocksDescendants"
滚动加载更多
ScrollView上滑加载更多一般用到setOnTouchListener(会报警告,某些控件直接用setOnTouchListener可能还会影响点击事件响应,这时可以在方法名顶部加入@SuppressLint("ClickableViewAccessibility"))
slv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (slv.getScrollY() != 0) {
if (!flag) {
flag = true;
reTop.setBackgroundColor(Color.parseColor("#00bf9f"));
}
} else {
flag = false;
reTop.setBackground(null);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (slv.getChildAt(0).getHeight() - slv.getHeight()
== slv.getScrollY()) {
getAnswerList(list_category.get(adapter_category.getSelect()).getId());//上拉加载更多
}
}
return false;
}
});
ListView,ExpandableListView用的setOnScrollListener
lvOrder.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
getList();//上拉加载更多
}
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
RecyclerView用的addOnScrollListener
rlv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!rlv.canScrollVertically(-1)) {
LogUtil.i("滑动到顶部");
}
if (!rlv.canScrollVertically(1)) {
//系统已经自动帮我们弄了只回调一次
if (rlvLDInterface != null)
rlvLDInterface.loadMore();
LogUtil.i("滑动到底部");
}
}
});
网友评论