用一个listview达到ScrollView的效果
2015-11-20 09:44
543 查看
在工作中,有用到ScrollView包裹listView实现页面滑动的效果,但是用的时候ScrollView和ListVew有冲突!所以在最近学到一种新的做法,就是用一个ListView就可搞定了,也能达到ScrollView的效果。下面请具体看代码:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/11/fafc18b3494a0d37ec285f507d79c935)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/11/2d5e20b5d39ca78a88745d5d9ab5bd8c)
首先是主xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33ffffff"
/>
</LinearLayout>
在算上两个从的xml文件也就是说是一个头部,一个尾部
header.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
这里我就不写布局了 你们可以根据你们的要求下上布局
</LinearLayout>
footer.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
这里我就不写布
4000
局了 你们可以根据你们的要求下上布局
</LinearLayout>
上面是布局文件 ,接下来是java文件:
代码如下:
public class ListViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
ListView lv_serving_people_list = (ListView) findViewById(R.id.lv_list);
View header = LayoutInflater.from(context).inflate(R.layout.header, null);
View footer= LayoutInflater.from(context).inflate(R.layout.footer, null);
//里面的id你们可以根据这两个header,footer去点findViewById
List<Object> annoList = new ArrayList<Object>();
annoList.add("赵日天");
annoList.add("叶良辰");
annoList.add("福尔康");
annoList.add("龙傲天");
//这是适配器,适配器的代码我就不贴出来了
StaffDetailAdapter adapter = new StaffDetailAdapter(context, annoList);
//这两个 一定要放在适配器的前面,不然运行后会报错
lv_serving_people_list.addHeaderView(header);
lv_serving_people_list.addFooterView(footer);
lv_serving_people_list.setAdapter(adapter);
}
}
首先是主xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33ffffff"
/>
</LinearLayout>
在算上两个从的xml文件也就是说是一个头部,一个尾部
header.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
这里我就不写布局了 你们可以根据你们的要求下上布局
</LinearLayout>
footer.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/common_bg"
>
这里我就不写布
4000
局了 你们可以根据你们的要求下上布局
</LinearLayout>
上面是布局文件 ,接下来是java文件:
代码如下:
public class ListViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
ListView lv_serving_people_list = (ListView) findViewById(R.id.lv_list);
View header = LayoutInflater.from(context).inflate(R.layout.header, null);
View footer= LayoutInflater.from(context).inflate(R.layout.footer, null);
//里面的id你们可以根据这两个header,footer去点findViewById
List<Object> annoList = new ArrayList<Object>();
annoList.add("赵日天");
annoList.add("叶良辰");
annoList.add("福尔康");
annoList.add("龙傲天");
//这是适配器,适配器的代码我就不贴出来了
StaffDetailAdapter adapter = new StaffDetailAdapter(context, annoList);
//这两个 一定要放在适配器的前面,不然运行后会报错
lv_serving_people_list.addHeaderView(header);
lv_serving_people_list.addFooterView(footer);
lv_serving_people_list.setAdapter(adapter);
}
}
相关文章推荐
- 使用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