ScrollView嵌套ListView解决方案

来源:互联网 发布:windows安全控件手机版 编辑:程序博客网 时间:2024/05/18 05:11

ScrollView嵌套ListView条目显示不全的解决方案:(自定义一个ListView,onMeasure方法把所以的条目全部显示出来)

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

MainActivity代码:

public class MainActivity extends AppCompatActivity {    private MyListView lv;    ArrayList<String> l=new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        for (int i=0;i<100;i++){            l.add("hulongjie"+i);        }        initView();    }    private void initView() {        lv = (MyListView) findViewById(R.id.lv);        ScrollView sv= (ScrollView) findViewById(R.id.sv);        //进入界面从scrollview顶部显示        sv.smoothScrollTo(0,0);        lv.setAdapter(new Abc());    }    class Abc extends BaseAdapter{        @Override        public int getCount() {            return l.size();        }        @Override        public Object getItem(int position) {            return null;        }        @Override        public long getItemId(int position) {            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            View v = View.inflate(MainActivity.this, R.layout.listview, null);            TextView tv= (TextView) v.findViewById(R.id.tv);            tv.setText(l.get(position));            return v;        }    }}

自定义ListView类:

public class MyListView extends ListView {    public MyListView(Context context) {        super(context);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, i);    }}

XML代码:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/sv"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.example.hu.myscrollviewlistview.MyListView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/lv" /></ScrollView>
0 0
原创粉丝点击