1. 程式人生 > >RecyclerView的新增頭部佈局

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中使用會報錯