码迷,mamicode.com
首页 > 其他好文 > 详细

scrollviewlistview

时间:2016-04-14 14:09:30      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

xml

<com.example.scrollviewlistview.MyScrollView 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"

/>
<Button
android:id="@+id/change"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="改变listview的高"
/>

</LinearLayout>

</com.example.scrollviewlistview.MyScrollView>

 
   

MyListView.java

package com.example.scrollviewlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ListView;

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;
}

}

 
   

MyScrollView.java

package com.example.scrollviewlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView{

public MyScrollView(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);

@SuppressWarnings("unused")
int i = MotionEvent.ACTION_CANCEL;
Log.d("TAG","ScrollView.......onTouchEvent。。。。"+b+"......."+ev.getAction());
return b;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

// TODO Auto-generated method stub
boolean b = super.dispatchTouchEvent(ev);
Log.d("TAG","ScrollView.......dispatchTouchEvent。。。。"+b+"......."+ev.getAction());
return b;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
boolean b = super.onInterceptTouchEvent(ev);
Log.d("TAG","ScrollView.......onInterceptTouchEvent。。。。"+b+"......."+ev.getAction());
return b;
}

}

 
   

MainActivity.java

package com.example.scrollviewlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class MainActivity extends Activity {
private MyListView lv;
@SuppressWarnings("unused")
private Button change;
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);
change = (Button) findViewById(R.id.change);
//配置数据
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);
}
}

 
   

scrollviewlistview

标签:

原文地址:http://www.cnblogs.com/nanze/p/5390566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!