ScrollView+自定义ListView(一)

来源:互联网 发布:宝马320li2017款数据 编辑:程序博客网 时间:2024/06/05 16:52

activity_main.xml

<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" >    <LinearLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <com.example.scrollviewlistview.MyListView            android:id="@+id/lv"            android:layout_width="match_parent"            android:layout_height="wrap_content"            />        <TextView             android:layout_width="match_parent"            android:layout_height="50dp"            android:background="#fbfbfb"            android:gravity="center_vertical"            android:textAppearance="?android:attr/textAppearanceMedium"            android:text="我是占位的"            />        <TextView             android:layout_width="match_parent"            android:layout_height="50dp"            android:background="#fbfbfb"            android:gravity="center_vertical"            android:textAppearance="?android:attr/textAppearanceMedium"            android:text="我是占位的"            />    </LinearLayout></ScrollView>

MyListView.class

public class MyListView extends ListView{    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        // TODO Auto-generated method stub        boolean b = super.onTouchEvent(ev);        Log.d("TAG","ListView.......onTouchEvent。。。。"+b+"......."+ev.getAction());        return b;    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        // TODO Auto-generated method stub        boolean b = super.onInterceptTouchEvent(ev);        Log.d("TAG","ListView.......onInterceptTouchEvent。。。。"+b+"......."+ev.getAction());        return b;    }     public boolean dispatchTouchEvent(MotionEvent ev) {          //请求父(Scrollview不截断touch事件,因为Scrollview也有滚动效果)         getParent().requestDisallowInterceptTouchEvent(true);           boolean b = super.dispatchTouchEvent(ev);         Log.d("TAG","ListView.......dispatchTouchEvent。。。。"+b+"......."+ev.getAction());        return b;        }  }

MainActivity.class

public class MainActivity extends Activity {    private MyListView lv;    String[] books = { "A语言", "B语言", "C语言", "D语言", "E语言", "A1语言", "B1语言",            "C1语言", "D1语言", "E1语言", "A2语言", "B2语言", "C2语言", "D2语言", "E2语言",            "A2语言", "B2语言", "C语言", "D语言", "E语言" };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        setListViewHeightBasedOnChildren(lv);        // lv.getParent().requestDisallowInterceptTouchEvent(false);    }    private void init() {        // TODO Auto-generated method stub        lv = (MyListView) findViewById(R.id.lv);        // 配置数据        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                android.R.layout.simple_list_item_1, books);        lv.setAdapter(adapter);          }    // 根据listview的数据来计算他的应得的高度,设置高度    @SuppressWarnings("deprecation")    public void setListViewHeightBasedOnChildren(MyListView listView) {        @SuppressWarnings("rawtypes")        ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();        if (listAdapter == null) {            return;        }        int totalHeight = 0;        for (int i = 0; i < listAdapter.getCount(); i++) {            View listItem = listAdapter.getView(i, null, listView);            if (listItem != null) {                listItem.setLayoutParams(new LayoutParams(                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));                listItem.measure(MeasureSpec.UNSPECIFIED,                        MeasureSpec.UNSPECIFIED); //无束缚测量                totalHeight += listItem.getMeasuredHeight();            }        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight                + (listView.getDividerHeight() * (listAdapter.getCount() - 1))                + listView.getPaddingTop() + listView.getPaddingBottom();        int h = getWindowManager().getDefaultDisplay().getHeight();        if (params.height > h / 2) {            params.height = h / 2;         }        listView.setLayoutParams(params);    }}
0 0
原创粉丝点击