ScrollView嵌套ListView

来源:互联网 发布:盖德穆勒数据 编辑:程序博客网 时间:2024/05/21 17:01


  1、布局文件

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

           <com.bawei.min.scrollerview.MyListView
               android:layout_width="match_parent"
               android:layout_height="500dp"
               android:id="@+id/lv"></com.bawei.min.scrollerview.MyListView>
           <Button
               android:layout_width="match_parent"
               android:layout_height="50dp"
               android:text="按钮"/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="50dp"
               android:text="按钮"/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="50dp"
               android:text="按钮"/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="50dp"
               android:text="按钮"/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="50dp"
               android:text="按钮"/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="50dp"
               android:text="按钮"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="按钮"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="按钮"/>
    </LinearLayout>
    </ScrollView>



2、自定义一个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) {
        MeasureSpec.makeMeasureSpec(800,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

3、自定义一个适配器

public class MyAdapter extends BaseAdapter {
    private ArrayList<String> list;
    private Context context;

    public MyAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    @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) {
        TextView text=new TextView(context);
        text.setText(list.get(position));
        return text;
    }


4、给设置监听

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlv= (MyListView) findViewById(R.id.lv);
        list=new ArrayList<String>();
        for(int i=0;i<50;i++){
            list.add("信息"+i);
        }
        mlv.setAdapter(new MyAdapter(this,list));
        mlv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    mlv.requestDisallowInterceptTouchEvent(false);
                }else{
                    mlv.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });

    }








0 0
原创粉丝点击