Android Studio--ListView和ScrollView同方向的嵌套

前几天由于需要,需要在一个垂直的ScrollView添加一个相同方向的ListView,但是默认的情况下是不行的,ListView会显示不完全。
后来在博客中找到了一种解决办法简便而有方便的操作,就是在ListView中的每个子项重新计算ListView的高度,再把高度作为LayoutParams中。

下面简述这种方法的实现:

1.创建自定义的ListView

这里的ListViewInScrollView直接继承了ListView,只是重写了里边来自ListView的onMeasure方法,即可。

package com.example.demo_listviewinscrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/** * Created by 寒 on 2016/5/16. */
public class ListViewInScrollView extends ListView {
    public ListViewInScrollView(Context context) {
        super(context);
    }

    public ListViewInScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewInScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ListViewInScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
    1. onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。
    2. 它们指明控件可获得的空间以及关于这个空间描述的元数据。
    3. 依据MeasureSpec的值,如果是AT_MOST,代表的是最大可获得的空间;如果是EXACTLY,代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
    4. 这里需要注意的是,ScrollView里边只支持一个容器布局,通常我们在ScrollView里边嵌套一个LinearLayout即可

2.布局文件activity_main.xml

<?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:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.demo_listviewinscrollview.MainActivity">

    <TextView  android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:background="@color/colorPrimary" android:gravity="center" android:text="这里用来测试ScrollView" />

    <ImageView  android:layout_width="match_parent" android:layout_height="3dp" android:layout_marginTop="55dp" android:background="@color/colorAccent" />

    <ScrollView  android:id="@+id/scrollView" android:layout_marginTop="70dp" android:layout_width="match_parent" android:layout_height="wrap_content">

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

            <TextView  android:layout_gravity="center" android:id="@+id/listTitle" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:background="@color/colorPrimary" android:gravity="center" android:text="这里用来测试ScrollView" />
            <com.example.demo_listviewinscrollview.ListViewInScrollView  android:focusableInTouchMode="true" android:focusable="true" android:layout_marginTop="20dp" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content">

            </com.example.demo_listviewinscrollview.ListViewInScrollView>



        </LinearLayout>
    </ScrollView>
</RelativeLayout>

3.listView子项布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout  android:id="@+id/content" android:orientation="horizontal" android:padding="3dp" android:weightSum="3" android:layout_width="match_parent" android:layout_height="50dp">
        <TextView  android:layout_weight="2" android:id="@+id/message" android:layout_width="0dp" android:layout_height="match_parent" />
        <ImageView  android:background="@color/colorAccent" android:layout_width="3dp" android:layout_height="match_parent" />
        <Button  android:text="变换" android:id="@+id/touch" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

4.Adapter

package com.example.demo_listviewinscrollview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/** * Created by 寒 on 2016/5/16. */
public class MessageAdapter extends ArrayAdapter<Msg>{

    int resource;

    public MessageAdapter(Context context, int resource, List<Msg> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Msg msg = getItem(position);
        View view ;
        final ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resource , null);
            viewHolder = new ViewHolder();
            viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.content);
            viewHolder.textView = (TextView) view.findViewById(R.id.message);
            viewHolder.button = (Button) view.findViewById(R.id.touch);

            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

       viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "改变标题", Toast.LENGTH_SHORT).show();
                viewHolder.textView.setText(msg.getTitle_copy());
            }
        });
        viewHolder.linearLayout.setVisibility(View.VISIBLE);
        viewHolder.textView.setText("这是 " + position + "子项");
        return view;
    }


}


class ViewHolder{
    LinearLayout linearLayout;
    TextView textView;
    Button button;
}

msg类

package com.example.demo_listviewinscrollview;

/** * Created by 寒 on 2016/5/16. */
public class Msg {
    String title;
    String title_copy;

    public Msg(String title, String title_copy) {
        this.title = title;
        this.title_copy = title_copy;
    }

    public String getTitle() {
        return title;
    }

    public String getTitle_copy() {
        return title_copy;
    }
}

你可能感兴趣的:(android,ListView)