Android ScrollView滚动 + ListView无滚动条加载全部内容
2016-06-28 11:49
573 查看
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);
}
@Override
public 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>这里注意:原来的<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"};
@Override
protected 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() {
@Override
public 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();
}
});
}
效果图:
核心代码如下:
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);
}
@Override
public 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>这里注意:原来的<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"};
@Override
protected 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() {
@Override
public 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();
}
});
}
效果图:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories