双listview下滚动头部文件

小小的借鉴了一下别人的代码和自己的想法写的,希望给需要的人一点帮助

主activity

package com.example.listtest;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class MainActivity extends Activity implements OnTouchListener{
private scrollviewlist list1, list2;
private Button btn;
private List strli;
private SlidingDrawer mdrawer;
private LinearLayout content, content2;
private int _yDelta;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    content = (LinearLayout) findViewById(R.id.content);
    content2 = (LinearLayout) findViewById(R.id.content2);
    list1 = (scrollviewlist) findViewById(R.id.listView1);
    list2 = (scrollviewlist) findViewById(R.id.listView2);
    list2.setAdapter(new ArrayAdapter(this,
            android.R.layout.simple_expandable_list_item_1, getData()));
    list1.setAdapter(new ArrayAdapter(this,
            android.R.layout.simple_expandable_list_item_1, getData()));


    content.setOnTouchListener(this);

// content2.setOnTouchListener(this);
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    // TODO Auto-generated method stub
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                .getLayoutParams();
        _yDelta = Y - lParams.topMargin;
        break;
    case MotionEvent.ACTION_UP:
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_POINTER_UP:
        break;
    case MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                .getLayoutParams();
        if (Y - _yDelta  > (-450)) {//判断隐藏了多少,-120可以更改
            layoutParams.topMargin = Y - _yDelta ;
        } else {
            layoutParams.topMargin = -450;
        }
        if (Y - _yDelta  < 0) {//判断出现了多少,60可以更改
            layoutParams.bottomMargin = Y - _yDelta ;
        } else {
            layoutParams.topMargin = 0;
        }
        view.setLayoutParams(layoutParams);
        break;
    }
    return true;
}

private List getData() {

    List data = new ArrayList();
    data.add("测试数据1");
    data.add("测试数据2");
    data.add("测试数据3");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据1");
    data.add("测试数据2");
    data.add("测试数据3");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    data.add("测试数据4");
    return data;
}

}

副:自定义listview
package com.example.listtest;

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

public class scrollviewlist extends ListView {

public scrollviewlist(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public scrollviewlist(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
public scrollviewlist(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
            MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

}

xml文件:

你可能感兴趣的:(双listview下滚动头部文件)