您的位置:首页 > 移动开发 > Android开发

四:资讯列表实现(借助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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息