ScrollView中嵌套ListView存在的问题及解决办法

ScrollView中嵌套ListView的时候只能显示一个条目.

解决:通过自定义ListVIew,并对ListView的高度进行重新计算

package com.example.dk.text1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.logging.Level;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView) findViewById(R.id.lv);
        MyAdapter adapter = new MyAdapter();
        lv.setAdapter(adapter);

        initData();

    }

    private void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            mDatas.add("当前条目为 "+i);
        }
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mDatas==null?0:mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas==null?null:mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder ;
            if (convertView==null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);

            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.mItemTV.setText(mDatas.get(position));
            holder.mItemBtn.setText(mDatas.get(position));

            return convertView;
        }

        class ViewHolder {

            private final TextView mItemTV;
            private final Button mItemBtn;

            public ViewHolder(View convertView) {
                mItemTV = (TextView) convertView.findViewById(R.id.item_tv);
                mItemBtn = (Button) convertView.findViewById(R.id.item_btn);
            }
        }
    }


布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.example.dk.text1.MainActivity">

   <ScrollView
       android:id="@+id/scrollView"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#ff5500"
       >

       <com.example.dk.text1.MyListView
       android:id="@+id/lv"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="#ff005555"
       >

   </com.example.dk.text1.MyListView>

   </ScrollView>

</LinearLayout>
//自定义ListView
package com.example.dk.text1;

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

/*
 *  @项目名:  Text1 
 *  @包名:    com.example.dk.text1
 *  @文件名:   MyListView
 *  @创建者:   D.K
 *  @创建时间:  2016/12/1 19:14
 *  @描述:    TODO
 */
public class MyListView extends ListView {

    public MyListView(Context context) {
        super(context);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heigh = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heigh);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值