自定义ListView

来源:互联网 发布:智能化数据分析 编辑:程序博客网 时间:2024/06/05 08:57

自定义ListView部分:

package com.example.day10_;

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

public class MyListView extends ListView{

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyListView(Context context) {
        super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec=
                MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,//扩展高度的最大值
                MeasureSpec.AT_MOST);
        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

xml文件中引入自定义的ListView:

<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=".MainActivity"
    android:id="@+id/scrollView" >

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

        <ImageView
            android:id="@+id/imageViewId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/picture1" />

        <com.example.day10_.MyListView
            android:id="@+id/listid"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</ScrollView>


MainActivity中将其显示:

package com.example.day10_;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        List<String> list=new ArrayList<String>();
        for(char c='A';c<='Z';c++){
            list.add(String.valueOf(c));
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lsv=(ListView) findViewById(R.id.listid);
        lsv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list));
        ScrollView sc=(ScrollView) findViewById(R.id.scrollView);
        sc.smoothScrollBy(0, 0);    //平滑滚动到顶端
    }
}



0 0