listview+viewpager+tabPageIndicator出现viewpager高度问题及页面不稳定

黑金白土 2017-01-07 03:35:37
最近项目要求,头布局是自动轮播图+listview,listview中的一个item中是如此布局:viewpager+tabPageIndicator。后来我将此布局作为footview加入到listview中,但是有如下两个问题:1、尾布局中的viewpager高度只能固定值。2、整个页面切换后出现页面不稳定现象。
其中viewpager + fragment +gridview。
求大神指点!

部分代码如下:
自定义的listview:
public class CustomListView<T> extends ListView implements OnScrollListener,Pullable {

private static final String LOADING_MORE_DATA = "正在加载数据";
private static final String ONCLICK_LOADING_MORE_DATA = "点击查看更多";

private Context mContext;
private boolean isAllowAutoLoadMore = false;
private boolean isLoadingData = false;
private boolean isCanLoadData = false;
private boolean isScrollEnd = false;

private RelativeLayout mFooterViewLayout;
private TextView mFooterViewTextView;
private ProgressBar mFooterViewProgressBar;

private IDragLoadMoreListener mIDragLoadMoreListener;

public CustomListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
initViews(context);
}

public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initViews(context);
}

public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews(context);
}


/**
* 初始化控件
* */
private void initViews(Context context) {
this.mContext = context;
this.setOnScrollListener(this); // 注册listview滚动事件
setCacheColorHint(context.getResources().getColor(R.color.bgcolor22));

initFooterView();
this.addFooterView(mFooterViewLayout); // 添加底部view

mFooterViewLayout.setClickable(false); // 初始化底部view不容许点击
// 注册底部view点击事件
mFooterViewLayout.setVisibility(View.GONE);

setDivider(new ColorDrawable(context.getResources().getColor(R.color.bgcolor23)));
setSelector(context.getResources().getDrawable(R.drawable.bg_list_item));
setDividerHeight(context.getResources().getDimensionPixelOffset(R.dimen.common_listview_divider_height));
mFooterViewTextView.setTextColor(context.getResources().getColor(R.color.textcolor09));
}

/**
* 初始化FooterView
*
* @author xujun
* */
private void initFooterView() {
mFooterViewLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.ah_listview_footer_layout_sj, this, false);
mFooterViewTextView = (TextView) mFooterViewLayout.findViewById(R.id.ah_listview_footer_TextView);
mFooterViewTextView.setText(ONCLICK_LOADING_MORE_DATA);
mFooterViewProgressBar = (ProgressBar) mFooterViewLayout.findViewById(R.id.ah_listview_footer_ProgressBar);
mFooterViewProgressBar.setVisibility(View.GONE);
mFooterViewLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isLoadingData) {
return;
}
if (null == mIDragLoadMoreListener) {
return;
}
setOnLoadMore();
}
});
}

/**
* 设置是否显示底部控件
* */
public void setIsShowFooterView(boolean isShowFooterView) {
try {
synchronized (LOADING_MORE_DATA) {
mFooterViewLayout.setClickable(isShowFooterView); // 设置底部是否可以点击
isCanLoadData = isShowFooterView;
if (isShowFooterView) {
mFooterViewLayout.setVisibility(View.VISIBLE);
mFooterViewTextView.setVisibility(View.VISIBLE);
if (getFooterViewsCount() == 0) {
initFooterView();
addFooterView(mFooterViewLayout);
}

} else {
mFooterViewLayout.setVisibility(View.GONE);
if (getFooterViewsCount() > 0) {
removeFooterView(mFooterViewLayout);
}
}

}
} catch (ClassCastException e) {
// TODO: handle exception
e.printStackTrace();
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int lastVisibleIndex = view.getLastVisiblePosition();
int viewCount = view.getCount();
if (isAllowAutoLoadMore && isCanLoadData) {
if (isScrollEnd && lastVisibleIndex == (viewCount - 1)) {
if (isLoadingData) {
return;
}
if (null == mIDragLoadMoreListener) {
return;
}
setOnLoadMore();
}
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
isScrollEnd = (firstVisibleItem + visibleItemCount) == totalItemCount;
}

public void setAllowAutoLoadMore(boolean isAllowAutoLoadMore) {
this.isAllowAutoLoadMore = isAllowAutoLoadMore;
}

public void setmIDragLoadMoreListener(IDragLoadMoreListener mIDragLoadMoreListener) {
this.mIDragLoadMoreListener = mIDragLoadMoreListener;
}

/**
* listview上拉加载更多接口
* */
public interface IDragLoadMoreListener {
/**
* 上拉加载更多开始
* */
public void onDragLoadMoreData();
}

/**
* 开始执行上拉操作
*/
private void setOnLoadMore() {
mFooterViewProgressBar.setVisibility(View.VISIBLE);
mFooterViewTextView.setText(LOADING_MORE_DATA);
isLoadingData = true;
mIDragLoadMoreListener.onDragLoadMoreData();
}

/**
* 上拉刷新数据请求结束后调用方法(必须调用)
*/
public void isLoadMoreComplete() {
mFooterViewProgressBar.setVisibility(View.GONE);
mFooterViewTextView.setText(ONCLICK_LOADING_MORE_DATA);
if (isAllowAutoLoadMore) {
mFooterViewTextView.setVisibility(View.INVISIBLE);
}
isLoadingData = false;
}

@Override
public boolean canPullDown() {
if (getCount() == 0)
{
// 没有item的时候也可以下拉刷新
return true;
} else if (getFirstVisiblePosition() == 0
&& getChildAt(0).getTop() >= 0)
{
// 滑到ListView的顶部了
return true;
} else
return false;
}

@Override
public boolean canPullUp() {
if (getCount() == 0)
{
// 没有item的时候也可以上拉加载
return true;
} else if (getLastVisiblePosition() == (getCount() - 1))
{
// 滑到底部了
if (getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()) != null
&& getChildAt(
getLastVisiblePosition()
- getFirstVisiblePosition()).getBottom() <= getMeasuredHeight())
return true;
}
return false;
}

}

自定义的viewpager:
public class MyViewPager extends ViewPager {
private float startX;
private float startY;
private float endX;
private float endY;
public MyViewPager(Context context) {
super(context);
}

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE :

break;
case MotionEvent.ACTION_DOWN :
startX = ev.getX();
startY = ev.getY();
break;
case MotionEvent.ACTION_UP :
endX = ev.getX();
endY = ev.getY();
float x = Math.abs(endX - startX);
float y = Math.abs(endY - startY);
if(x-y >8) {
getParent().requestDisallowInterceptTouchEvent(false);
}else {
//这句话的作用 告诉父view,我的单击事件我自行处理,不要阻碍我。
getParent().requestDisallowInterceptTouchEvent(true);
}
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
view.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height = Math.max(view.getMeasuredHeight(),height);
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
作为尾布局的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.cubic.chachong.indicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="44dp" />

<com.cubic.common.view.MyCutomViewPager
android:id="@+id/vp_jiangshi"
android:background="@color/bg_viewpager_white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
...全文
156 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑金白土 2017-02-07
  • 打赏
  • 举报
回复
关于viewpager高度固定的问题暂时还没有解决。第二个问题页面不稳定,是因为我用了fragment的onActivityCreated回调方法,并在其中有添加头布局的代码,而我用的是用FragmentTabHost,这个控件里面所用的fragment切换是replace会导致每次切换的时候都回调onActivityCreated方法,导致头布局添加多次。后将添加头布局的方法写到OncreatedView回调方法中就没有问题了。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧