Android ListView嵌套GridView(同理嵌套ListView)类似QQ说说条目加点赞显示名称效果

29 篇文章 0 订阅
22 篇文章 0 订阅

Android ListView嵌套GridView和ListView嵌套ListView

先上要的效果图

ListVIew嵌套Gridview

好了,思想+代码。

MainActivity代码:

public class ListViewNestActivity extends Activity {
    private List<NowUpdateParentEntity> list=new ArrayList<>();
    List<NowUpdateChildEntity> updateChildEntityList=new ArrayList<>();
    private MyListView myListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.include_story_hall_now_update);
        findView();
        initView();
    }

    private void findView() {
        myListView= (MyListView) findViewById(R.id.now_update_main_listView);
    }

    private void initView() {
        addNum();
        NowUpdateAdapter nowUpdateParentAdapter=new NowUpdateAdapter(ListViewNestActivity.this,list,updateChildEntityList);
        myListView.setAdapter(nowUpdateParentAdapter);
    }

    private List<NowUpdateParentEntity> addNum(){
        NowUpdateParentEntity entity=new NowUpdateParentEntity();
        NowUpdateChildEntity childEntity=new NowUpdateChildEntity();
        entity.setIcon(R.mipmap.user_icon);
        entity.setName("admin");
        entity.setTime("2016-12-31");
        entity.setImage(R.mipmap.one);
        entity.setTitle("test");
        entity.setTodayNum("14255");
        entity.setContent("hfkahklahflahflaj");
        entity.setForm("test");
        entity.setClassify("test");
        entity.setTextNum("358万字");
        childEntity.setImage(R.mipmap.user_icon);
        updateChildEntityList.add(childEntity);
        list.add(entity);
        return list;
    }
}

Layout代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.aierjun.woxiangwoxiang.widget.MyListView
            android:id="@+id/now_update_main_listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </com.aierjun.woxiangwoxiang.widget.MyListView>
    </LinearLayout>
</ScrollView>

NowUpdateAdapter代码:

public class NowUpdateAdapter extends BaseAdapter {
    private Context context;
    private List<NowUpdateParentEntity> updateParentEntityList;
    private List<NowUpdateChildEntity> updateChildEntityList;
    public NowUpdateAdapter(Context context, List<NowUpdateParentEntity> updateParentEntityList,List<NowUpdateChildEntity> updateChildEntityList) {
        this.context=context;
        this.updateParentEntityList=updateParentEntityList;
        this.updateChildEntityList=updateChildEntityList;
    }

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

    @Override
    public Object getItem(int i) {
        return updateParentEntityList.get(i);
    }

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

    @Override
    public View getView(int i, View canverView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (canverView==null){
            viewHolder=new ViewHolder();
            canverView=View.inflate(context, R.layout.now_update_main_listview_item,null);
            viewHolder.icon= (ImageView) canverView.findViewById(R.id.user_icon_main_story);
            viewHolder.name= (TextView) canverView.findViewById(R.id.name_main_story);
            viewHolder.time= (TextView) canverView.findViewById(R.id.time_main_story);
            viewHolder.image= (ImageView) canverView.findViewById(R.id.image_main_story);
            viewHolder.title= (TextView) canverView.findViewById(R.id.title_main_story);
            viewHolder.content= (TextView) canverView.findViewById(R.id.content_main_story);
            viewHolder.form= (TextView) canverView.findViewById(R.id.form_main_story);
            viewHolder.classify= (TextView) canverView.findViewById(R.id.calssify_main_story);
            viewHolder.textNum= (TextView) canverView.findViewById(R.id.text_num_main_story);
            viewHolder.todayNum= (TextView) canverView.findViewById(R.id.today_num_main_story);
            viewHolder.gridView= (GridView) canverView.findViewById(R.id.gridview_collect_icon);
            canverView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) canverView.getTag();
        }
        NowUpdateParentEntity entity= (NowUpdateParentEntity) getItem(i);
        viewHolder.icon.setImageResource(entity.getIcon());
        viewHolder.name.setText(entity.getName());
        viewHolder.time.setText(entity.getTime());
        viewHolder.image.setImageResource(entity.getImage());
        viewHolder.title.setText(entity.getTitle());
        viewHolder.todayNum.setText(entity.getTodayNum());
        viewHolder.content.setText(entity.getContent());
        viewHolder.form.setText(entity.getForm());
        viewHolder.classify.setText(entity.getClassify());
        viewHolder.textNum.setText(entity.getTextNum());
        viewHolder.gridView.setAdapter(new NowUpdateChildAdapter(context,updateChildEntityList));
        return canverView;
    }
    class ViewHolder{
        ImageView icon;  //头像
        TextView name; //昵称
        TextView time;//时间
        ImageView image;//图片
        TextView title; //文字标题
        TextView todayNum;
        TextView content; //内容
        TextView form; //来源
        TextView classify; //类型
        TextView textNum;//字数
        ImageView collectionImage;//收藏图片
        TextView collectionshNum;//输出数
        TextView commentNum;//评论数
        GridView gridView;
    }
}
注意:这是最外层的大的adapter

Entity代码:

public class NowUpdateParentEntity {
    private int icon;  //头像
    private String name; //昵称
    private String time;//时间
    private int image;//图片
    private String title; //文字标题
    private String todayNum; //今日指数
    private String content; //内容
    private String form; //来源
    private String classify; //类型
    private String textNum;//字数
    private int collectionImage;//收藏图片
    private String collectionshNum;//输出数
    private String commentNum;//评论数

    public NowUpdateParentEntity() {
    }

    public NowUpdateParentEntity(String classify, int collectionImage, String collectionshNum, String commentNum, String content, String form, int icon, int image, String name, String textNum, String time, String title, String todayNum) {
        this.classify = classify;
        this.collectionImage = collectionImage;
        this.collectionshNum = collectionshNum;
        this.commentNum = commentNum;
        this.content = content;
        this.form = form;
        this.icon = icon;
        this.image = image;
        this.name = name;
        this.textNum = textNum;
        this.time = time;
        this.title = title;
        this.todayNum = todayNum;
    }

    public String getClassify() {
        return classify;
    }

    public void setClassify(String classify) {
        this.classify = classify;
    }

    public int getCollectionImage() {
        return collectionImage;
    }

    public void setCollectionImage(int collectionImage) {
        this.collectionImage = collectionImage;
    }

    public String getCollectionshNum() {
        return collectionshNum;
    }

    public void setCollectionshNum(String collectionshNum) {
        this.collectionshNum = collectionshNum;
    }

    public String getCommentNum() {
        return commentNum;
    }

    public void setCommentNum(String commentNum) {
        this.commentNum = commentNum;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getForm() {
        return form;
    }

    public void setForm(String form) {
        this.form = form;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTextNum() {
        return textNum;
    }

    public void setTextNum(String textNum) {
        this.textNum = textNum;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTodayNum() {
        return todayNum;
    }

    public void setTodayNum(String todayNum) {
        this.todayNum = todayNum;
    }

    @Override
    public String toString() {
        return "NowUpdateParentEntity{" +
                "classify='" + classify + '\'' +
                ", icon=" + icon +
                ", name='" + name + '\'' +
                ", time='" + time + '\'' +
                ", image=" + image +
                ", title='" + title + '\'' +
                ", todayNum='" + todayNum + '\'' +
                ", content='" + content + '\'' +
                ", form='" + form + '\'' +
                ", textNum='" + textNum + '\'' +
                ", collectionImage=" + collectionImage +
                ", collectionshNum='" + collectionshNum + '\'' +
                ", commentNum='" + commentNum + '\'' +
                '}';
    }
}

NowUpdateChildAdapter代码:

public class NowUpdateChildAdapter extends BaseAdapter {
    private Context context;
    private List<NowUpdateChildEntity> list;
    public NowUpdateChildAdapter(Context context, List<NowUpdateChildEntity> list) {
        this.context=context;
        this.list=list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View canerView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (canerView==null){
            viewHolder=new ViewHolder();
            canerView=View.inflate(context, R.layout.imageview_item,null);
            viewHolder.icon= (ImageView) canerView.findViewById(R.id.image_gridview);
            canerView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) canerView.getTag();
        }
        NowUpdateChildEntity entity= (NowUpdateChildEntity) getItem(i);
        viewHolder.icon.setImageResource(entity.getImage());
        return canerView;
    }
    class ViewHolder{
        ImageView icon;
    }
}
注意:这里的adapter是嵌套的gridview的adapter

Entity代码:

public class NowUpdateChildEntity {
    private int image;

    public NowUpdateChildEntity() {
    }

    public NowUpdateChildEntity(int image) {
        this.image = image;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    @Override
    public String toString() {
        return "NowUpdateChildEntity{" +
                "image=" + image +
                '}';
    }
}
注意:这里的entity是嵌套的gridview的entity

item的layout代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dp">
        <ImageView
            android:id="@+id/user_icon_main_story"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/user_icon"/>
        <RelativeLayout
            android:id="@+id/main_title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toRightOf="@id/user_icon_main_story"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="10dp">
            <TextView
                android:id="@+id/name_main_story"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="张三"
                android:textSize="23sp"
                android:textColor="@color/black" />
            <TextView
                android:id="@+id/time_main_story"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2016-11-10"
                android:layout_below="@id/name_main_story"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_below="@id/main_title">
            <ImageView
                android:id="@+id/image_main_story"
                android:layout_width="100dp"
                android:layout_height="140dp"
                android:src="@mipmap/one"
                android:scaleType="centerCrop"/>
            <RelativeLayout
                android:id="@+id/title_main_big_story"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_toRightOf="@id/image_main_story">
                <TextView
                    android:id="@+id/title_main_story"
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:text="造物集自传"
                    android:textSize="20sp"
                    android:textColor="@color/black"
                    android:layout_marginLeft="10dp"/>
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:gravity="center_vertical">
                    <TextView
                        android:id="@+id/today_num_main_story"
                        android:layout_width="wrap_content"
                        android:layout_height="30dp"
                        android:text="1425"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:layout_marginLeft="10dp"
                        android:layout_alignParentRight="true"
                        android:gravity="center_vertical"/>
                    <TextView
                        android:id="@+id/title_num_text_main_story"
                        android:layout_width="wrap_content"
                        android:layout_height="30dp"
                        android:text="今日指数:"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:layout_toLeftOf="@id/today_num_main_story"
                        android:gravity="center_vertical"/>
                </RelativeLayout>
            </RelativeLayout>
            <TextView
                android:id="@+id/content_main_story"
                android:layout_width="match_parent"
                android:layout_height="85dp"
                android:text="造物集自传dddddddsadfa阿发发ddddd大道大道啊dad哇低洼大 大道打我阿迪王昂达dddd"
                android:textSize="18sp"
                android:textColor="@color/black_gray"
                android:maxLines="3"
                android:ellipsize="end"
                android:layout_toRightOf="@id/image_main_story"
                android:layout_below="@id/title_main_big_story"
                android:paddingBottom="10dp"
                android:layout_marginLeft="10dp"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/image_main_story"
                android:layout_below="@id/content_main_story"
                android:layout_marginLeft="10dp">
                <TextView
                    android:id="@+id/form_main_story"
                    android:layout_width="wrap_content"
                    android:layout_height="25dp"
                    android:text="#造物集自传"
                    android:textSize="15sp"
                    android:textColor="@color/blue" />
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true">
                    <TextView
                        android:id="@+id/calssify_main_story"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp"
                        android:text="连载"
                        android:textSize="11sp"
                        android:textColor="@color/gray"
                        android:layout_marginLeft="15dp"
                        android:layout_marginRight="15dp"
                        android:background="@drawable/gray_btn_bg"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:gravity="center"/>
                    <TextView
                        android:id="@+id/text_num_main_story"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp"
                        android:text="39万字"
                        android:textSize="11sp"
                        android:textColor="@color/gray"
                        android:layout_toRightOf="@id/calssify_main_story"
                        android:background="@drawable/gray_btn_bg"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:gravity="center"/>
                </RelativeLayout>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:id="@+id/collect_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:layout_marginTop="10dp">
            <ImageView
                android:id="@+id/collect_image_main_story"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@mipmap/user_icon"/>
            <TextView
                android:id="@+id/collect_num_main_story"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/collect_image_main_story"
                android:text="22"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/collect_num_main_story"
                android:text="个收藏"
                android:layout_marginLeft="2dp"
                android:gravity="center_vertical"/>
        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/collect_left"
            android:layout_marginLeft="25dp"
            android:orientation="horizontal">
            <com.aierjun.woxiangwoxiang.widget.MyGridView
                android:id="@+id/gridview_collect_icon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:numColumns="6"
               android:layout_marginLeft="10dp"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

GridView item的layout代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image_gridview"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/user_icon"/>
</LinearLayout>

好了,说下思路。

简单的说就是嵌套,在list里面的adapter里面,找到gridview,然后setAdapter,然后一样的填充就好。在list的adapter里注意传参数给gridview的adapter就好。

上面的数据都是死的,可以自己修改添加,也可以网络获取后添加,上面的代码可以直接复制使用,能够跑起来,希望对你们有帮助。觉得有用的记得顶一下。


注意:本文为我原创,自己一行行写的,请尊重劳动成果,转载请注明出处和连接,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值