Android-->关于ScrollView、ListView嵌套ListeView所出现的问题(心得)

    ListView是开发过程中经常打交道的控件之一,但是使用过程中需要主要很多事项。譬如,如果ListView的高度不是设置为match_parent或者高度值确定的话,那么在getView()方法你会发现,getView()方法被调用多轮而且所以的ListView item都会跑到不管是否正在屏幕上显示。此时我们所使用的ListView缓存机制感觉就是无效的。自己查了资料发现,如果ListView的高度不确定的话,其实系统是无法很快确定有多少item显示与屏幕上。只有不断的通过measure才能确定,这也导致了getview方法执行多次。所以在使用ListView过程中尽量确定其高度(同时包括其结点上的Parent View),不然对性能是很大的消耗。

   知道了上面所提到的问题,我们就知道了为什么Google不建议ScrollView中嵌套ListView了,因为ScrollView的child View只能是warp_content。当然网上针对嵌套问题也给出了某些解决方案。例如:

public class MyListView extends ListView {

  private boolean isOnMeasure = false;

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

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    isOnMeasure = true;
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
    isOnMeasure = false;
    super.onLayout(changed, l, t, r, b);
  }

  public boolean isOnMeasure() {
    return isOnMeasure;
  }

}
接着只需要在你的Adapter中getView方法调用就行例如:

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder = null;
    if (convertView == null) {
      holder = new Holder();
      convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
      ..........
      convertView.setTag(holder);
    }
    else {
      holder = (Holder)convertView.getTag();
    }
    //一定要在完成convertView初始化之后调用
    if ( ((ListView)parent).isOnMeasure()) {
      return convertView;
    }
    ......
  }
这种方法,我在ListView中嵌套ListView(将ListView添加到另外ListView的headerView中)测试了下,当每个item的高度是相同的没有问题。如果不同的话就会出现内容显示不全的bug。

所以这种这种方案也不是完美方案。如果需要在ListView的头或尾添加其他View,推荐使用ListView的addHeaderView()方法和BaseAdapter中getItemViewType()方法结合使用。关于BaseAdapter中getItemViewType中网上有很多教程,主要作用是使ListView中的item填充不同的布局。

最后提醒下,对于ListView的嵌套使用一定要慎重,因为坑很多!


仿某团list嵌套list\ package com.zl.listview; import java.util.ArrayList; import java.util.HashMap; import com.kjy.kjylistview.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; /** * @author zl * @time 2014年12月24日15:37:18 */ public class QianTaoListviewActivity extends Activity { private ListView listView; private ArrayList<HashMap<String, Object>> parentList, childList; private ParentAdapter parentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_qiantao); init(); } private void init() { listView = (ListView) findViewById(R.id.qiantao_lv); getList(); } private void getList() { parentList = new ArrayList<HashMap<String, Object>>(); for (int i = 1; i < 10; i++) { childList = new ArrayList<HashMap<String, Object>>(); for (int j = i < 7 ? i : 7; j < 9; j++) { HashMap<String, Object> ParentMap = new HashMap<String, Object>(); ParentMap.put("parent_title", "三木铁板烧" + i); ParentMap.put("parent_address", "8." + i); ParentMap.put("parent_distance", "1" + i); ParentMap.put("parent_distance1", "已售123" + i); childList.add(ParentMap); } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("child_item_title", "九州八拖二火锅" + i); map.put("child_item_price_old", "4." + i + "分"); map.put("child_item_price_score", "(100人)"); map.put("parent_lv", childList); parentList.add(map); } parentAdapter = new ParentAdapter(parentList, QianTaoListviewActivity.this); listView.setAdapter(parentAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent intent = new Intent(QianTaoListviewActivity.this, Activity_1.class); Toast.makeText(QianTaoListviewActivity.this, "第" + arg2 + "个条目", Toast.LENGTH_LONG).show(); startActivity(intent); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值