zoukankan      html  css  js  c++  java
  • 自定义listView与scrollView使用

    <?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.fuicuiedu.xc.listview_20170222.MainActivity">

    <ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <com.fuicuiedu.xc.listview_20170222.MyListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </LinearLayout>
    </ScrollView>
    </RelativeLayout>

    自定义listView

    package etest.ll.com.mylistview;

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

    /**
    * 自定义一个可以嵌套在ScrollView中的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);
    }

    //重写该方法,达到使ListView可以正确的嵌套在ScrollView中
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //自定义绘制规则
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
    }
    }

    //编写activity

    package etest.ll.com.mylistview;

    public class MainActivity extends AppCompatActivity {

    private ListView mLv;
    private List<String> datas;
    private ArrayAdapter<String> adapter;
    private Handler handler;

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

    handler = new Handler();

    mLv = (ListView) findViewById(R.id.listview);

    datas = new ArrayList<>();

    //添加假数据
    for (int i = 0; i < 50; i++) {
    datas.add("第" + i + "条数据");
    }

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datas);

    mLv.setAdapter(adapter);


    //动态的设置ListView高度
    setListViewHeight(mLv);
    }

    //动态的设置ListView高度(拿到所有item的高度,设置到listView身上)
    private void setListViewHeight(ListView listview) {
    //获取listView对应的Adapter
    ListAdapter listAdapter = listview.getAdapter();
    if (listAdapter == null){
    return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i,null,listview);
    //计算item的宽高(调用一个绘制的方法)
    listItem.measure(0,0);
    //统计所有子项的高度
    totalHeight += listItem.getMeasuredHeight();
    }

    //获取分割线的总高度
    int dividerHeight = listview.getDividerHeight() * (listAdapter.getCount() - 1);

    //拿到listView布局参数
    ViewGroup.LayoutParams parms = listview.getLayoutParams();
    parms.height= totalHeight + dividerHeight;
    listview.setLayoutParams(parms);
    }
    }

    !!adapter使用系统自带的进行演示

  • 相关阅读:
    十分钟内学会:将HTML格式化为合法的XML
    十分钟内学会:根据数据库生成站点导航
    Adobe Apollo vs Joyeur Slingshot
    英语阅读推荐:你真的懂UPDATE语句吗 & 当有layout之时
    欲练 CSS ,必先宫 IE
    英语阅读推荐:海明威写作技巧 & UpdatePanel为何失灵
    英语阅读推荐:在AJAX中制作自定义验证服务 & 优秀网站的5个因素
    Code is Configuration
    十分钟内学会:自动识别GB2312与UTF8编码的文件
    学习 Ruby on Rails 真的很爽!
  • 原文地址:https://www.cnblogs.com/ll-ouyang/p/6438318.html
Copyright © 2011-2022 走看看