Android Scollowview嵌套ListView

来源:互联网 发布:淘宝 百雀羚漫网专卖店 编辑:程序博客网 时间:2024/06/06 05:33

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