Android Scollowview嵌套ListView

Scollowview 嵌套ListView 

缺点:

listView只会显示一行,滑动冲突

解决办法:

重新测量ListView的高度

代码如下:直接粘贴复制即可

这里面有两个测量listview高度的方法

一个是自定义listview 

一个事动态设置listview的高度,如果用自定义的话,就是用那个自定义的MyListView,用的时候需要把布局的listview换成你的包名加你的类名,效果是一样的,喜欢那个就用那个吧。

xml布局


activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.scollview_listview.MainActivity">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
                <ListView
                    android:id="@+id/mLv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                </ListView>
        </ScrollView>
</RelativeLayout>

主界面

MainActivity

public class MainActivity extends AppCompatActivity {
    private List<String> list;
    private ListView mLv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        mLv= (ListView) findViewById(R.id.mLv);
//        mLv.setSelection(MyListView.FOCUS_DOWN);
        mLv.setFocusable(false);
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,list);
        mLv.setAdapter(adapter);
        initHeight(mLv);
    }


    private void initList() {
        list=new ArrayList<>();
        for (int i=0;i<50;i++){
            list.add("这是第"+i+"数据");
        }
    }
    private void initHeight(ListView listview){
        int height=0;
        View view=null;
        ListAdapter listAdapter=listview.getAdapter();
        if(listAdapter==null){
            return;
        }
        for(int i=0;i<listview.getCount();i++){
            view=listAdapter.getView(i,view,listview);
            view.measure(0,0);
            height+=view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params=listview.getLayoutParams();
        params.height=height+(listview.getDividerHeight()*(listview.getCount()-1));
        listview.setLayoutParams(params);
    }


}


public class MyListView extends ListView {

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

public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
//Integer.MAX_VALUE>>2   系统规定只能显示30个数值,而int类型是32个数值,所以需要再往后两位
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,
    MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, height);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值