码迷,mamicode.com
首页 > 其他好文 > 详细

ScrollView&ListView

时间:2016-05-07 18:04:40      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

 

<ScrollView
    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"
    tools:context="org.mobiletrain.a8_5scrollviewlistview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>
    </LinearLayout>
</ScrollView>
public class MyAdapter extends BaseAdapter {
    private List<String> list;
    private Context context;
    private LayoutInflater inflater;

    public MyAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("google_lenve_fb", "getView: position:" + position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listview_item, null);
            holder = new ViewHolder();
            holder.username = (TextView) convertView.findViewById(R.id.username);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.username.setText(list.get(position));
        return convertView;
    }

    class ViewHolder {
        TextView username;
    }
}
/**
 * 使用重新测量ListView高度的方式解决ScrollView和ListView嵌套问题,一定要将ListView放入到一个LinearLayout中
 */
public class MainActivity extends AppCompatActivity {

    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.lv);
        initData();
        MyAdapter adapter = new MyAdapter(this, list);
        //获取listview中item的总数
        int count = adapter.getCount();
        int totalHeight = 0;
        for (int i = 0; i < count; i++) {
            //获取ListView中每一个itemView
            View view = adapter.getView(i, null, listView);
            //0表示不指定itemView的宽高,由它自己测量
            view.measure(0, 0);
            //获取每一个item的高度
            int measuredHeight = view.getMeasuredHeight();
            totalHeight += measuredHeight;
        }
        ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
        //获取item之间分隔线的总高度
        int dividerHeight = listView.getDividerHeight() * (count - 1);
        //重新给ListView指定高度
        layoutParams.height = totalHeight + dividerHeight;
        listView.setLayoutParams(layoutParams);
        //重新测量View的宽高并显示,invalidate方法也会重新测量View的宽高并显示,
        // 但是invalidate方法同时会调用onDraw方法,而requestLayout只会重新测量宽高,不重绘
        listView.requestLayout();
//        listView.invalidate();
        listView.setAdapter(adapter);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("李四:" + i);
        }
    }
}

 

ScrollView&ListView

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5468633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!