本人想实现相似下图的功能。
自定义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无法上下滑动,问一下有没有朋友遇到过这个问题,这是怎么回事?
解决方案
100
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);//这个被你修改了 、、、、、、、、、、 }