关于使用PulltoRefreshListView来展示新闻的内容以及多级评论
来源:互联网 发布:数学建模优化模型论文 编辑:程序博客网 时间:2024/05/17 00:00
在项目当中,碰到了使用PulltoRefreshListView来展示新闻和多级评论,在评论那里碰到了难题,如何解决多级评论的显示呢?
为了方面起见,我直接使用ListView来代替PulltoRefreshListView做示例。
废话不多说,直接看代码示例。
第一步:首先,新建MainActivity的XML布局,布局就一个ListView。之后再新建MainActivity类
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView></RelativeLayout>
第二步:新建HeaderView的布局,就是ListView的头部分,为方便理解,直接使用简单化来展示
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文章Title" android:textColor="@android:color/holo_red_dark"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="@android:color/holo_green_dark" android:text="文章内容jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaffffffffffffffffffffffffffffffffffffffffffffffrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeee"/></LinearLayout>第三步:新建评论的子布局,如下:
<?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:background="@android:color/white" android:orientation="vertical"> <TextView android:id="@+id/tvHotComment" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:text="热门评论" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="一级评论内容" android:textColor="@android:color/black" /> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:background="@android:color/darker_gray"> </LinearLayout></LinearLayout>
id为ll的LinearLayout是为了展示二级评论内容的;
第四步:
新建评论适配器,继承BaseAdapter,因为是本地展示,所以数据源直接就以for循环来代替了。为了有热门评论和最新评论之分,在适配器中,写入判断position的位置决定title的显示与文字的变化。适配器代码如下:
package com.listviewdemo;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.TextView;public class CommentAdapter extends BaseAdapter { private Context mContext; public CommentAdapter(Context context) { mContext=context; } @Override public int getCount() { return 20; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int position, View view, ViewGroup viewGroup) { ViewHolder holder; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.comment_item, null); holder=new ViewHolder(); holder.ll=(LinearLayout) view.findViewById(R.id.ll); holder.tvHot=(TextView) view.findViewById(R.id.tvHotComment); view.setTag(holder); } else { holder=(ViewHolder)view.getTag(); } if(position==0){ holder.tvHot.setVisibility(View.VISIBLE); holder.tvHot.setText("热门评论"); }else{ if(position==3){ holder.tvHot.setVisibility(View.VISIBLE); holder.tvHot.setText("最新评论"); }else { holder.tvHot.setVisibility(View.GONE); } } holder.ll.removeAllViews(); for(int i=0;i<3;++i){ View commentItem2=LayoutInflater.from(mContext).inflate(R.layout.comment_2_item, null); holder.ll.addView(commentItem2); } return view; } class ViewHolder{ LinearLayout ll; TextView tvHot; }}第五步:在适配器写好后,在MainActivity方法中对ListView进行处理,使用ListView的特性,addHeaderView(View view)方法,再将适配器设置进去就完成了。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView=(ListView) findViewById(R.id.listView); View topView= LayoutInflater.from(this).inflate(R.layout.top_view,null); listView.addHeaderView(topView); listView.setAdapter(new CommentAdapter(this)); }}
效果图如下:
1 0
- 关于使用PulltoRefreshListView来展示新闻的内容以及多级评论
- 凤凰新闻自动评论推荐软件--把自己的评论推荐到最前面,然后通过头像、用户名或者评论内容进行展示
- 新闻内容展示
- 关于使用pulltorefreshlistview刷新的问题
- 使用UITableView实现新闻评论
- 练习项目 一款新闻app的开发 (四):通过RecyclerView来展示新闻列表
- 多级分类的树形展示
- PullToRefreshListView 的使用
- PullToRefreshListView的简单使用
- Android--PullToRefreshListView的使用
- PullToRefreshListView的使用
- PullToRefreshListView的使用
- PullToRefreshListView库的使用
- PullToRefreshListView的使用
- pullToRefreshListView的使用
- PullToRefreshListView的使用
- PullToRefreshListView的使用
- PullToRefreshListView的使用
- nginx实现负载均衡
- jQuery跳转链接
- Android retrofit历程(一)
- makefile-规则的命令
- Rails中的ajax运用1---评论
- 关于使用PulltoRefreshListView来展示新闻的内容以及多级评论
- ICP算法实现(MATLAB)
- [HNOI2002][poj1091]跳蚤
- 百度地图的基本应用
- python里面的encode和decode
- websocket
- Linux进程间通信——使用信号量
- 课程实践:电子词典
- Hibernate 开发实例全步骤