四:资讯列表实现(借助PullToRefres实现上拉和下拉刷新)
2016-05-18 08:56
405 查看
之前已经实现了ViewPager的整体框架,然后现在需要实现类NewsListFragment在其中填充资讯列表、新闻列表,总之就是个列表。在Material Design里面对列表的每一项长什么样子都是有比较完整的定义的,所以我们只需要知道列表项中有几行内容,显示不显示图片就可以了。这里每个列表项有一个主标题和一个副标题,不显示图片,于是得到的布局就是这个样子的:
<?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="72dp"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:textColor="#87000000"
android:textSize="16sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="2dp"
android:singleLine="true"
android:text="这个是新闻的标题" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:textColor="#54000000"
android:textSize="14sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="2dp"
android:singleLine="true"
android:text="这个是新闻的副标题,作为副标题通常需要很长才不会被删掉"/>
</LinearLayout>
</LinearLayout>
实现listview的代码是这个样子的:
public class NewsListFragment extends Fragment {
View view;
int pid;
ListView listview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_list,container,false);
listview = (ListView) view.findViewById(R.id.list);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(int i=0;i<20;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("text1", "这个是新闻的题目");
map.put("text2", "新闻的副标题必须很长很长才能被显示出来,这里现在需要一只团子");
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,R.layout.news_list_item, new String[] { "text1", "text2" }, new int[] { R.id.text1, R.id.text2 });
listview.setAdapter(adapter);
return view;
}
public NewsListFragment(int pid){
this.pid = pid;
}
}实现之后的效果如图:
至于外面那一圈tab是在上一篇博客里面实现的,只是这样子不够,因为还需要实现一个上拉刷新和下拉刷新的功能。看上去非常高大上,其实只要导入一个叫做pulltorefresh的第三方库,然后把里面的ListView全都改成PullToRefreshListView就可以了。pulltorefresh的github地址是:https://github.com/chrisbanes/Android-PullToRefresh
<?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="72dp"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:textColor="#87000000"
android:textSize="16sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="2dp"
android:singleLine="true"
android:text="这个是新闻的标题" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:textColor="#54000000"
android:textSize="14sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="2dp"
android:singleLine="true"
android:text="这个是新闻的副标题,作为副标题通常需要很长才不会被删掉"/>
</LinearLayout>
</LinearLayout>
实现listview的代码是这个样子的:
public class NewsListFragment extends Fragment {
View view;
int pid;
ListView listview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_list,container,false);
listview = (ListView) view.findViewById(R.id.list);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(int i=0;i<20;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("text1", "这个是新闻的题目");
map.put("text2", "新闻的副标题必须很长很长才能被显示出来,这里现在需要一只团子");
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,R.layout.news_list_item, new String[] { "text1", "text2" }, new int[] { R.id.text1, R.id.text2 });
listview.setAdapter(adapter);
return view;
}
public NewsListFragment(int pid){
this.pid = pid;
}
}实现之后的效果如图:
至于外面那一圈tab是在上一篇博客里面实现的,只是这样子不够,因为还需要实现一个上拉刷新和下拉刷新的功能。看上去非常高大上,其实只要导入一个叫做pulltorefresh的第三方库,然后把里面的ListView全都改成PullToRefreshListView就可以了。pulltorefresh的github地址是:https://github.com/chrisbanes/Android-PullToRefresh
相关文章推荐
- 使用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