Android实现 ScrollView + ListView无滚动条滚动
来源:互联网 发布:qq三国js卡多少级好 编辑:程序博客网 时间:2024/05/11 15:30
Android实现 ScrollView+ListView无滚动条滚动,即ListView的数据会全部显示完,但Listview无滚动条。
核心代码如下:
1. NoScrollListView.java
/*** * 自定义ListView子类,继承ListView * @author Administrator * */public class NoScrollListView extends ListView {public NoScrollListView(Context context) {super(context);}public NoScrollListView(Context context, AttributeSet attrs) {super(context, attrs);}public NoScrollListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}
2. res > layout > activity_main.xml
<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF4F4F4" android:scrollbars="vertical" > <LinearLayout android:id="@+id/LinearLayout" android:gravity="center_horizontal" android:background="#FFF4F4F4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginTop="20dp" android:text="This's a listView..." /> <!--原: <ListView> --> <com.example.listviewdemo.NoScrollListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerHeight="0.0dip" android:fadingEdge="none" android:cacheColorHint="#FFF4F4F4"/> </LinearLayout></ScrollView>
2. res > layout > activity_main.xml
这里注意:原来的<ListView 改为<com.example.listviewdemo.NoScrollListView 。
3.MainActivity
private String[] nameList = {"Miley Cyruc","Alice Keys","Jewel","Dublin","Kelly Clarkson", "Mariah Carey","Sheen","Adele","Avril Lavigne","Taylor Swift"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lv = (ListView) findViewById(R.id.listView1);ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, nameList);lv.setAdapter(adapter);//lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//选择效果//listView注册一个元素点击事件监听器lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Log.d(TAG, "//:"+MainActivity.this.nameList[arg2]);Toast.makeText(MainActivity.this, "Hey, "+nameList[arg2], Toast.LENGTH_LONG).show();}}); }
2. res > layout > activity_main.xml
效果图:
2. res > layout > activity_main.xml
效果图:
0 0
- Android实现 ScrollView + ListView无滚动条滚动
- Android实现 ScrollView + ListView无滚动条滚动
- Android实现 ScrollView + ListView无滚动条滚动
- Android实现 ScrollView + ListView无滚动条滚动
- Android实现 ScrollView + ListView无滚动条滚动
- Android ScrollView滚动 + ListView无滚动条加载全部内容
- Android实现 ScrollView + ListView无滚动条滚动 解决2者间的冲突
- 滚动条 ScrollView与Listview 嵌套 无滚动条实现滚动
- Android scrollview 滚动条
- Android ScrollView滚动条
- Android ScrollView滚动条
- 【Android学习笔记】ScrollView下使用无滚动条ListView,即ListView的item内容自适应拉伸
- Android ListView滚动条
- android中的滚动条ScrollView
- android中的滚动条ScrollView
- Android ScrollView去掉滚动条
- ListView 实现类似listview + scrollview滚动效果
- Android项目笔记一:TextView+ScrollView 实现垂直滚动条
- 关于UIView的autoresizingMask属性的研究
- 小心环境变量-浅谈LD_PRELOAD
- 聊聊穷人皇帝的人和事儿
- Android 系统电量统计
- Eclipse中@author的修改
- Android实现 ScrollView + ListView无滚动条滚动
- 欢迎使用CSDN-markdown编辑器
- hdu1207 汉诺塔II
- Java就业指导
- rqnoj302题解
- Ubuntu下安装Android Studio 1.1
- Java CMS提交内容中带中文导致乱码修改jdbc连接字符串来解决的方法
- 2015编程之美挑战赛
- 遍历Map的四种方法