关闭

listview抢焦点

36人阅读 评论(0) 收藏 举报
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的高度,重新设置
		// change.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// setListViewHeightBasedOnChildren(lv);
		// }
		// });
	}

	// 根据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);
	}

}

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

}

<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="我是占位的"
	        />
	    
	    <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="我是占位的"
	        />
	    
	    <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>

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:191次
    • 积分:8
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章存档