关于使用PulltoRefreshListView来展示新闻的内容以及多级评论

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013868143/article/details/53995992

在项目当中,碰到了使用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));
    }

}

效果图如下:
 
 
 
 
         



猜你喜欢

转载自blog.csdn.net/u013868143/article/details/53995992