ScrollView和Listview结合使用
来源:互联网 发布:java根据ip获取mac 编辑:程序博客网 时间:2024/05/25 05:38
- 自定义布局
public class MyListview extends ListView{ public MyListview(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public boolean dispatchTouchEvent(MotionEvent ev) { getParent().requestDisallowInterceptTouchEvent(true); boolean b = super.dispatchTouchEvent(ev); 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="com.yang.sl.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.yang.sl.MyListview android:id="@+id/main_mylistview" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="20dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="30dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="40dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="50dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="60dp" android:text="黑" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="20dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="30dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="40dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="50dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="60dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="70dp" android:text="红" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="80dp" android:text="红" /> </LinearLayout></ScrollView>
- 效果实现
public class MainActivity extends Activity { private String item[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","E","Y","Z"}; private MyListview lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setOnSL(lv); } private void init() { lv = (MyListview) findViewById(R.id.main_mylistview); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, item); lv.setAdapter(adapter); } @SuppressWarnings("deprecation") private void setOnSL(MyListview listview) { // TODO Auto-generated method stub @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(); } } LayoutParams params = listview.getLayoutParams(); params.height = toTalHeight + (listview.getDividerHeight() * (listview.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结合使用
- ListView和ScrollView的结合 解决方法
- ScrollView和listView嵌套使用
- 解决listview与scrollview结合使用时出现的问题
- 解决listview与scrollview结合使用时出现的问题
- 解决listview与scrollview结合使用时出现的问题
- ScrollView和ListView一起使用的问题
- cocosStudio中使用PageView,ListView和ScrollView
- 在ScrollView中使用GridView和ListView
- Scrollview.pageview和listview的使用
- cocosStudio中使用PageView,ListView和ScrollView
- listview和scrollview嵌套使用--解决方案
- 使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突
- ListView和ImageList的结合使用
- CheckBox和ListView的结合使用
- Dialog和ListView,progressBar的结合使用
- 防携程筛选,listview和checkbox结合使用
- ListView中的Adapter和ViewHolder结合使用
- C++ limits头文件的用法(numeric_limits)
- HTML5标莶使用初级技巧
- java-对象管理-集合
- Middle-题目98:316. Remove Duplicate Letters
- hibernate如何使用hql语句查询时间区间段
- ScrollView和Listview结合使用
- 机器学习资源大全中文版
- FragmentTarnsaction.add 和 FragmentTarnsaction.replace区别
- Middle-题目99:227. Basic Calculator II
- 栈内存与堆内存的区别
- JS时钟实现(一)
- 项目修改名字报错
- Middle-题目100:18. 4Sum
- 图论(一):DFS,BFS,邻接链表,并查集