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
- ScrollView+自定义ListView(一)
- ScrollView嵌套自定义ListView
- 使用LinearLayout,ScrollView自定义ListView,
- listview imageview scrollview自定义效果
- Android自定义控件(一) 下拉刷新,上拉分页加载更多(支持ListView, GridView, ScrollView)
- 自定义控件解决ListView和ScrollView冲突
- 自定义控件解决ListView和ScrollView冲突
- 自定义可适应ScrollView的ListView
- 自定义可适应ScrollView的ListView
- Android自定义适应scrollview的listview
- 自定义控件-TableView,ScrollView嵌套Listview
- 自定义ScrollView 和ListView 显示问题
- listview<一> 自定义适配器
- listview和ScrollView冲突的解决---自定义ListView
- scrollview嵌套listview,gridview时使用自定义listview或gridview
- 自定义ListView,解决ScrollView中嵌套ListView的问题
- ListView 和ScrollView共存问题(一)
- ScrollView嵌套ListView问题总结(一)
- html5本地存储-遍历以及清空数据
- HDU2147(模拟找规律)
- 设计模式 —— 策略模式(Strategy Pattern)
- c++ 左值与右值
- hdu 3501 Calculation 2 (欧拉函数的延伸)
- ScrollView+自定义ListView(一)
- Android之mvp设计模式
- hihoCoder #1032 : 最长回文子串
- C#三十四 常用开发的部分总结
- 我对mybatis的理解
- [leetcode] 179. Largest Number
- 爬虫爬取信息存入数据库
- Spring Mybatis整合
- 【醒目】【业界偷懒】【Public】BZOJ题目一句话题解整理