RecyclerView的添加头部布局

忙碌一天,仔细一想,也就深入了解了一下RecyclerView,改了点bug。

朋友在做一个类似朋友圈的页面,大体的看了下布局,感觉挺简单,但实际做起来并没有这么容易。

大概就是一个RecyclerView,然后写一个item就能搞定的事,细节处理比较多,写一下步骤吧

一个Item,大体布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="@dimen/head_img"
        android:layout_height="@dimen/head_img"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="5dip"
        android:background="@drawable/bmob_1"
        android:scaleType="fitXY" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="12dip"
        android:layout_marginRight="10dip"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="WeChat"
            android:textColor="#7888a9"
            android:textSize="18.0sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip" >

            <TextView
                android:id="@+id/content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bmob--移动云服务平台,真是个人开发者的福利呀,给个人开发者免费提供云数据库,棒棒哒"
                android:textColor="#404040"
                android:textSize="16.0sp" />
        </RelativeLayout>

       <com.msquirrel.widget.NoScrollGridView
           android:id="@+id/gridView"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:horizontalSpacing="1dp"
           android:numColumns="3"
           android:stretchMode="columnWidth"
           android:verticalSpacing="1dp"
           android:visibility="visible" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:text="福州"
            android:textColor="#7888a9"
            android:textSize="14.0sp" />

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="2dip"
                    android:layout_marginRight="10dip"
                    android:text="昨天"
                    android:textColor="@color/gray"
                    android:textSize="14.0sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="10dip"
                    android:background="@drawable/feed_more"
                    android:scaleType="fitXY" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_good_comment"
                android:layout_width="fill_parent"
                android:layout_height="30dip"
                android:layout_marginRight="32dip"
                android:visibility="gone" >

                <RelativeLayout
                    android:id="@+id/rl_good_rl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true" >

                    <ImageView
                        android:id="@+id/good_img"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/good_seletor" />

                    <ImageView
                        android:id="@+id/comment_img"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@id/good_img"
                        android:scaleType="fitXY"
                        android:src="@drawable/comment_seletor" />
                </RelativeLayout>
            </RelativeLayout>
        </FrameLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="20dip"
            android:layout_marginTop="10dip"
            android:background="#e6e6e6" >

            <ImageView
                android:id="@+id/iv_share_heart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dip"
                android:background="@drawable/s_msg_icon_praise"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/tv_share_names"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dip"
                android:layout_toRightOf="@id/iv_share_heart"
                android:text="点赞一,点赞二"
                android:textColor="#7888a9"
                android:textSize="13.0sp" />
        </RelativeLayout>

        <ImageView
            android:id="@+id/line"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="@drawable/line" />

        <LinearLayout
            android:id="@+id/comment_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#e6e6e6"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_comment_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="评论一:"
                    android:textColor="#7888a9"
                    android:textSize="14.0sp" />

                <TextView
                    android:id="@+id/tv_comment_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="不错啊,很好。"
                    android:textColor="@color/gray"
                    android:textSize="14.0sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="WeChat"
                    android:textColor="#7888a9"
                    android:textSize="14.0sp" />

                <TextView
                    android:id="@+id/tv_comment_conte"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="回复"
                    android:textColor="@color/gray"
                    android:textSize="14.0sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="评论一:"
                    android:textColor="#7888a9"
                    android:textSize="14.0sp" />

                <TextView
                    android:id="@+id/tv_commentontent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dip"
                    android:text="谢谢。"
                    android:textColor="@color/gray"
                    android:textSize="14.0sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在点赞、评论的时候,要实时更新RecyclerView就可以了
在整个布局头部还要有一块区域来显示一个图片背景,直接用别人封装好的类就可以,非常方便
在gradle中加入

dependencies {
compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0'
}

在代码中加入

RecyclerViewHeader header = RecyclerViewHeader.fromXml(MainActivity.this, R.layout.main_header);
header.attachTo(mRecyclerView);

需要注意的是mRecyclerView要先定义好竖直排列。
或者在Layout中加入

<com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top">

        <TextView
            android:textSize="50sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="header"/>

    </com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader>
RecyclerViewHeader header = (RecyclerViewHeader) findViewById(R.id.header);
header.attachTo(mRecyclerView, true);

该方法只能在Activity中布局,在Fragment中使用会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值