android 自定义listView,重写onMeasure,无法上下滑动

山城忙碌人 2016-07-31 12:09:49
我想实现类似下图的功能。

自定义listView代码如下:
public class MyListView extends ListView {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//重写该方法后,导致listView无法滑动
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}

}


适配器代码如下:
public class TestActAdapter extends BaseAdapter {
private Context mContext;
private List<RechargeBanlance> list;
MyListView myListView;


public TestActAdapter(Context mContext, List<RechargeBanlance> list,MyListView myListView) {
this.mContext = mContext;
this.list = list;
this.myListView=myListView;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return list.get(i);
}

@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewgroup) {
ViewHolder vh = null;
if(view==null){
view = LayoutInflater.from(mContext).inflate(R.layout.item_recharge_detailed, null);
vh = buildViewHolder(view);
view.setTag(vh);
}else{
vh = (ViewHolder) view.getTag();

}
initData(viewgroup, vh, i);
return view;
}
public void initData(View view, Object viewHolder, int position) {
// if (!((RefreshListView) viewGroup).isMeasure()) {

ViewHolder vh = (ViewHolder) viewHolder;
RechargeBanlance bean = (RechargeBanlance) list.get(position);
Util.printLog("充值详细:"+bean.getTime()+ ",position:"+position, 3);
if (bean.getType() == RechargeBanlance.DATA) {
vh.contentBox.setVisibility(View.VISIBLE);
vh.title.setVisibility(View.GONE);
vh.msg.setText(bean.getMsg());
vh.money.setText(""+bean.getMoney());
vh.date.setText(bean.getTime());
} else if (bean.getType() == RechargeBanlance.TITLE) {
vh.contentBox.setVisibility(View.GONE);
vh.title.setVisibility(View.VISIBLE);
vh.title.setText(bean.getTime());
}else{
Util.printLog("充值详细 类型不对", 3);
}
// setListViewHeightBasedOnChildren(myListView);
// }
}
public ViewHolder buildViewHolder(View view) {
ViewHolder vh = new ViewHolder();
vh.contentBox = (RelativeLayout) view.findViewById(R.id.contentBox);
vh.title = (TextView) view.findViewById(R.id.title);
vh.date = (TextView) view.findViewById(R.id.date);
vh.msg = (TextView) view.findViewById(R.id.msg);
vh.money = (TextView) view.findViewById(R.id.money);
return vh;
}
private class ViewHolder {
TextView title, msg, money, date;
RelativeLayout contentBox;
}
}

xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/box"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/WHITE"
android:orientation="vertical" >

<yftx.view.MyListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/WHITE"
android:divider="@color/eee"
android:dividerHeight="1dip" >
</yftx.view.MyListView>

</RelativeLayout>

activity代码如下:
public class TestAct extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_test);
mContext = this;
collectionList = (MyListView) findViewById(R.id.myListView);
list = new ArrayList<RechargeBanlance>();
for (int i = 0; i < 23; i++) {
RechargeBanlance bean = new RechargeBanlance();
if (i % 2 == 0) {
bean.setMsg("购买商品"+i);
bean.setMoney("+" + 10 + i);
} else if (i == 3) {
bean.setMsg("签到"+i);
bean.setMoney("+" + 10 + i);
} else {
bean.setMsg("兑换优惠卷"+i);
bean.setMoney("-" + 10 + i);
}
bean.setType(RechargeBanlance.DATA);
bean.setTime("03-1" + i);
list.add(bean);
}
adapter = new TestActAdapter(mContext, list,collectionList);
collectionList.setAdapter(adapter);
}
private MyListView collectionList;
private NumberPicker repetCount;
private TextView result, text;
private Button button1, button2, button3;
private Context mContext;
private List<RechargeBanlance> list;
private TestActAdapter adapter;
}

测试数据有30条,listView无法上下滑动,请问有没有朋友遇到过这个问题,这是怎么回事?
...全文
505 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyp6163 2016-08-14
  • 打赏
  • 举报
回复
我的按照楼主的修改后,是可以滑动了,不过listview还是只能显示1条数据,求解决方案。
tudouzi007 2016-08-01
  • 打赏
  • 举报
回复
引用 5 楼 bisubisu 的回复:
[quote=引用 4 楼 tudouzi007 的回复:] 首先你这个需求很奇怪,你禁止listview的滑动,但是你又有很多条数据(一个屏幕放不下),然后你又想叫他滑,你到底想做啥? 第一个:一屏放不下想叫他滑,那就用原来的原生的listview,不做过多操作,不禁止他滑动,还节省了自定义控件的麻烦 第二个:想用自定义控件listview禁止滑动,那就在你自定义的外层包一层scrollview,滑动不考listview而是靠scrollview来实现
我没有禁止listView滑动,我需要listView滑动。 我的需求是:执行adapter时,getView不要重复去刷新ui,所以我重写了listView,我重写listView出现了listView不动滑动的情况,不是我要禁止listView滑动。可能是我有些地方没说明白吧,才让你误解了! 目前listView无法滑动的问题已解决![/quote] 哦哦 解决了就好
山城忙碌人 2016-08-01
  • 打赏
  • 举报
回复
引用 4 楼 tudouzi007 的回复:
首先你这个需求很奇怪,你禁止listview的滑动,但是你又有很多条数据(一个屏幕放不下),然后你又想叫他滑,你到底想做啥? 第一个:一屏放不下想叫他滑,那就用原来的原生的listview,不做过多操作,不禁止他滑动,还节省了自定义控件的麻烦 第二个:想用自定义控件listview禁止滑动,那就在你自定义的外层包一层scrollview,滑动不考listview而是靠scrollview来实现
我没有禁止listView滑动,我需要listView滑动。 我的需求是:执行adapter时,getView不要重复去刷新ui,所以我重写了listView,我重写listView出现了listView不动滑动的情况,不是我要禁止listView滑动。可能是我有些地方没说明白吧,才让你误解了! 目前listView无法滑动的问题已解决!
tudouzi007 2016-08-01
  • 打赏
  • 举报
回复

首先你这个需求很奇怪,你禁止listview的滑动,但是你又有很多条数据(一个屏幕放不下),然后你又想叫他滑,你到底想做啥?
第一个:一屏放不下想叫他滑,那就用原来的原生的listview,不做过多操作,不禁止他滑动,还节省了自定义控件的麻烦
第二个:想用自定义控件listview禁止滑动,那就在你自定义的外层包一层scrollview,滑动不考listview而是靠scrollview来实现
山城忙碌人 2016-08-01
  • 打赏
  • 举报
回复
引用 1 楼 birdsaction 的回复:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
         int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); //你把默认的height修改了 导致父类无法正常计算child总体高度
            super.onMeasure(widthMeasureSpec, expandSpec);
    }


我之所以super.onMeasure(widthMeasureSpec, heightMeasureSpec);是因为,MeasureSpec.AT_MOST,MeasureSec.EXACTLY,这两个模式,多都试了,一样滑不动,MeasureSpec.UNSPECIFIED 这个模式只能显示第一条数据。
b87936260 2016-08-01
  • 打赏
  • 举报
回复
你重写这个onMeasure方法不就是让它不能上下滑动的吗?这问题无解,你要再让它滑,套个scrollview
山城忙碌人 2016-07-31
  • 打赏
  • 举报
回复
引用 1 楼 birdsaction 的回复:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
         int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); //你把默认的height修改了 导致父类无法正常计算child总体高度
            super.onMeasure(widthMeasureSpec, expandSpec);
    }


源码里面
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Sets up mListPadding
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);//这个被你修改了
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);//这个被你修改了 
}

确实是这个问题,不愧是版主,谢谢版主,这是解决getView被多次调用,出现的问题,今晚比较晚了,明天可能还有问题还你请教。我把你说的提示改了,确实可以滑动了, 不知道是不是治标不治本,明天再说吧。

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
Birds2018 2016-07-31
  • 打赏
  • 举报
回复

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
         int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); //你把默认的height修改了 导致父类无法正常计算child总体高度
            super.onMeasure(widthMeasureSpec, expandSpec);
    }


源码里面
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Sets up mListPadding
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);//这个被你修改了
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);//这个被你修改了

   、、、、、、、、、、

}

80,471

社区成员

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

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