Android之ListView嵌套,访空间动态展示页面

项目需求,需要做动态展示功能,我的想法是每条动态的评论都是Listview,然后动态也是Listview,效果图是这样的



这个实现的难点主要是评论adapter和动态adapter的设计实现。

评论adapter是子Listview,与写其他adapter过程是一样的

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.amei.tt.R;
import com.tt.amei.entity.Comment;

import java.util.List;


public class CommentAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    private List<Comment> commentList;

    //需要一个Context,通过Context获得Layout.inflater,然后通过inflater加载item的布局
    public CommentAdapter(Context context, List<Comment> comment) {
        layoutInflater = LayoutInflater.from(context);
        commentList = comment;
    }


    @Override
    public int getCount() {
        return commentList.size();
    }

    @Override
    public Object getItem(int position) {
        return commentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.commentitem,parent,false);
            holder = new ViewHolder();
            holder.isGoodComment = (ImageView)convertView.findViewById(R.id.isgoodcomment);
            holder.name = (TextView)convertView.findViewById(R.id.comname);
            holder.content = (TextView)convertView.findViewById(R.id.comcontent);

            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }


        Comment comment = commentList.get(position);
        holder.name.setText(comment.getUserId());
        holder.content.setText(comment.getText());
        holder.isGoodComment.setImageResource(comment.getIsgoodcomment());


        return convertView;
    }

    private class ViewHolder {
        ImageView isGoodComment;
        TextView name;
        TextView content;
    }
}

父adapter是动态的DynamicAdapter

这里有一个子listview显示不全的问题,解决办法是重新计算listview高度(借鉴别人的方法)


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.amei.tt.R;
import com.tt.amei.entity.Comment;
import com.tt.amei.entity.Dynamic;

import java.util.List;


public class DynamicAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    private List<Dynamic> dynamicList;

    private Context context;
    private CommentAdapter commentAdapter;
    private List<List<Comment>> commentDyList;//所有动态的评论


    public  DynamicAdapter(Context context, List<Dynamic> dynamics,List<List<Comment>> commentDyList){
        super();
        this.context = context;
        dynamicList = dynamics;
        layoutInflater = LayoutInflater.from(context);
        this.commentDyList = commentDyList;
    }

    @Override
    public int getCount() {
        return dynamicList.size();
    }

    @Override
    public Object getItem(int position) {
        return dynamicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.dynamiclayout,parent,false);
            holder = new ViewHolder();
            holder.content = (TextView)convertView.findViewById(R.id.showInfo);
            holder.dynamicUserName = (TextView)convertView.findViewById(R.id.userName);
            holder.dyTime = (TextView)convertView.findViewById(R.id.tvAge);
            holder.dyUserHeadima = (ImageView)convertView.findViewById(R.id.iv_leftlogo);

            holder.likenum = (TextView)convertView.findViewById(R.id.dianzan_nums);
            holder.dislikenum = (TextView)convertView.findViewById(R.id.tramplenum);
            holder.giftnum = (TextView)convertView.findViewById(R.id.giftnums);
            holder.commentnum = (TextView)convertView.findViewById(R.id.pinglun_nums);
            holder.sharenum = (TextView)convertView.findViewById(R.id.fenxiang_nums);

            holder.comment = (ListView)convertView.findViewById(R.id.comment);
            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//常规写法
        Dynamic dynamic = dynamicList.get(position);
        holder.content.setText(dynamic.getText());
        holder.dynamicUserName.setText(dynamic.getDynamicUserName());
        holder.dyTime.setText(dynamic.getDyTime());
        holder.dyUserHeadima.setImageResource(dynamic.getDyUserHeadima());
        holder.likenum.setText(String.valueOf(dynamic.getFivenums(0)));
        holder.dislikenum.setText(String.valueOf(dynamic.getFivenums(1)));       
        holder.giftnum.setText(String.valueOf(dynamic.getFivenums(2)));      
        holder.commentnum.setText(String.valueOf(dynamic.getFivenums(3)));
        holder.sharenum.setText(String.valueOf(dynamic.getFivenums(4)));
        /**
         * 每条动态的评论适配
         * commentDyList是所有动态的评论,类型为List<List<Comment>>
         * */
        commentAdapter = new CommentAdapter(context,commentDyList.get(position));
        holder.comment.setAdapter(commentAdapter);
        //重新计算listview 的高度
        setListViewHeight(holder.comment);


        return convertView;
    }


    private class ViewHolder {
        TextView content;//内容
        TextView dynamicUserName;//昵称
        ImageView dyUserHeadima;//头像
        TextView dyTime;//距离发动态时间

        TextView likenum;
        TextView dislikenum;
        TextView giftnum;
        TextView commentnum;
        TextView sharenum;

        ListView comment;//动态的评论
    }


    /**
     *重新计算listview 的高度
     * 解决嵌套的Listview显示不全的问题
     * */
    public void setListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}

Activity中要写的是


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.amei.tt.R;
import com.tt.amei.adapter.DynamicAdapter;
import com.tt.amei.entity.Comment;
import com.tt.amei.entity.Dynamic;

import java.util.ArrayList;
import java.util.List;


public class DynamicActivity extends AppCompatActivity{

    private DynamicAdapter dynamicAdapter;
    private ListView listDynamic;

    /**
     * 评论内容
     * 评论数据初始化
     * */
    private List<Dynamic> dynamicList;
    private List<Comment> commentList;
    private List<List<Comment>> dynamicCommentList;
    public void initCommentData(){
        dynamicCommentList = new ArrayList<>();

        commentList = new ArrayList<Comment>();
        Comment comment = new Comment("汪洋",":一切都在改变,唯独你的样子在我心中没变",R.mipmap.goodcomment);
        commentList.add(comment);
        comment = new Comment("周用",":特别好!",0);
        commentList.add(comment);
        comment = new Comment("丫丫",":我喜欢你的图片!",0);
        commentList.add(comment);
        comment = new Comment("海天相接",":同意楼上!",0);
        commentList.add(comment);
        dynamicCommentList.add(commentList);

        commentList = new ArrayList<Comment>();
        comment = new Comment("漱芳华",":一切都在改变1",R.mipmap.goodcomment);
        commentList.add(comment);
        comment = new Comment("周用",":特别好666!",0);
        commentList.add(comment);
        comment = new Comment("丫丫",":我喜欢你的图片!",0);
        commentList.add(comment);
        dynamicCommentList.add(commentList);

        commentList = new ArrayList<Comment>();
        dynamicCommentList.add(commentList);
        commentList = new ArrayList<Comment>();
        dynamicCommentList.add(commentList);

        commentList = new ArrayList<Comment>();
        dynamicCommentList.add(commentList);
        commentList = new ArrayList<Comment>();
        dynamicCommentList.add(commentList);

        commentList = new ArrayList<Comment>();
        comment = new Comment("丫丫",":我喜欢你的图片!",R.mipmap.goodcomment);
        commentList.add(comment);
        comment = new Comment("海天相接",":同意楼上!",0);
        commentList.add(comment);
        dynamicCommentList.add(commentList);
    }
    /**
     * 动态数据初始化*/
    public void initDynamic(){
        dynamicList = new ArrayList<Dynamic>();
        int[] fivenums1 = {20,1,2,1,3};
        int[] fivenums2 = {0,1,2,0,1};
        int[] fivenums3 = {20,1,2,1,5};
        int[] fivenums4 = {10,1,0,1,2};
        int[] fivenums5 = {2,1,2,1,0};
        int[] fivenums6 = {6,0,2,1,3};
        int[] fivenums7 = {9,1,0,1,0};
        Dynamic dynamic = new Dynamic("111111111111l","无法无天","2分钟前",R.drawable.blurima,dynamicCommentList.get(0),fivenums1);
        dynamicList.add(dynamic);
        dynamic = new Dynamic("la2222222222222222l11111","破小流苏","20分钟前",R.drawable.blurima2,dynamicCommentList.get(1),fivenums2);
        dynamicList.add(dynamic);

        dynamic = new Dynamic("lal33333333l22222","急疯的兔子会咬人","1小时前",R.drawable.blurima3,dynamicCommentList.get(2),fivenums3);
        dynamicList.add(dynamic);
        dynamic = new Dynamic("lalalalallalal33333","阿拉蕾","2小时前",R.drawable.blurima4,dynamicCommentList.get(3),fivenums4);
        dynamicList.add(dynamic);

        dynamic = new Dynamic("lalalalallalal44444","无法无天","1天前",R.drawable.blurima2,dynamicCommentList.get(4),fivenums5);
        dynamicList.add(dynamic);
        dynamic = new Dynamic("lalalalallalal55555","无法无天","1天前",R.drawable.blurima3,dynamicCommentList.get(5),fivenums6);
        dynamicList.add(dynamic);

        dynamic = new Dynamic("la6666666666666","无法无天","10-15",R.drawable.blurima4,dynamicCommentList.get(6),fivenums7);
        dynamicList.add(dynamic);

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamiclist);
        findview();
        initCommentData();
        initDynamic();
        init();
    }

    public void findview(){
        listDynamic = (ListView)findViewById(R.id.dynamiclist);
    }

    public void init(){
        dynamicAdapter = new DynamicAdapter(this,dynamicList,dynamicCommentList);
        listDynamic.setAdapter(dynamicAdapter);
    }
}

initCommentData();
initDynamic();
这两个为评论数据和动态数据的初始化。

Comment类和Dynamic类不详细说明,我觉得关键就是父Adapter的内容。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值