Android動態加載佈局

最近一直比較忙,博客沒及時寫。最近項目中用到了動態加載佈局,今天閒下來記錄一下本身的學習經歷吧。java

ListView咱們一直都在用,只不過當Adapter中的內容比較多的時候咱們有時候沒辦法去設置一些組件,舉個例子:android


能夠看到京東的故事裏面的這樣一個佈局,這個佈局能夠說是我目前見到的內容比較多的了,它的每一項都包含頭像、姓名、分類、內容、圖片、喜歡、評論、分享以及喜歡的頭像。分析了一下佈局以後咱們不難發現,除了喜歡頭像這部分,其他的都很好實現。服務器

那麼下面着重說一下這個頭像這部分怎麼實現?ide

第一種方案:咱們能夠用GridView來實現,GridView和ListView的用法是同樣的,俗稱九宮格排列,那麼咱們能夠將GridView的一行排列九張圖片來顯示這些頭像,只不過ListView嵌套着GridView稍微的有些麻煩,本身作了一個,感受不太好用,有想要ListView嵌套GridView的能夠私密我。佈局

第二種方案就是本篇文章所講的動態加載佈局了:學習

很簡單,咱們在ListView中定義一個LinerLayout線性佈局,用來存放這些頭像,先看一下佈局吧:url

<?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">

    <LinearLayout
            android:padding="@dimen/small_space"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <com.view.RoundedImageView
                android:id="@+id/iv_myspace_usericon"
                android:src="@drawable/usericon"
                android:layout_width="50dp"
                android:layout_height="50dp"/>
        <TextView
                android:id="@+id/tv_myspace_username"
                android:layout_marginLeft="@dimen/middle_space"
                android:layout_gravity="center_vertical"
                android:text="王某某"
                android:textSize="@dimen/small_textSize"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:id="@+id/tv_myspace_time"
                android:textColor="@color/normal_bar_futext_color"
                android:textSize="@dimen/smallest_textSize"
                android:layout_gravity="center_vertical"
                android:text="2015-8-26 17.46"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </LinearLayout>
    <TextView
            android:id="@+id/tv_myspace_content"
            android:paddingRight="@dimen/middle_space"
            android:paddingLeft="@dimen/middle_space"
            android:paddingBottom="@dimen/middle_space"
            android:text="受到了房間啊了會計分錄會計法艦隊司令減肥;馬上受到傑弗里斯到付款;總是以爲煩;老卡機的說法;就是看到的就發了卡就"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <ImageView
            android:id="@+id/iv_myspace_image"
            android:scaleType="fitXY"
            android:src="@drawable/moren"
            android:paddingRight="@dimen/middle_space"
            android:paddingLeft="@dimen/middle_space"
            android:layout_width="match_parent"
            android:layout_height="140dp"/>
    <LinearLayout
            android:id="@+id/ll_myspace_reply_icons"
            android:paddingTop="@dimen/small_space"
            android:paddingRight="@dimen/middle_space"
            android:paddingLeft="@dimen/middle_space"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </LinearLayout>
    <LinearLayout
            android:layout_marginTop="@dimen/small_space"
            android:paddingRight="@dimen/middle_space"
            android:paddingLeft="@dimen/middle_space"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <ImageView
                android:id="@+id/iv_myspace_like"
                android:src="@drawable/zan_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"/>
            <ImageView
                    android:visibility="gone"
                    android:id="@+id/iv_myspace_liked"
                    android:src="@drawable/wozaixianchang_dianzanxi"
                    android:layout_width="20dp"
                    android:layout_height="20dp"/>
        <TextView
                android:id="@+id/tv_myspace_zan_count"
                android:layout_gravity="center_vertical"
                android:text="0"
                android:textColor="@color/normal_bar_futext_color"
                android:layout_marginLeft="@dimen/small_space"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <ImageView
                android:id="@+id/iv_myspace_comment"
                android:layout_marginLeft="@dimen/middle_space"
                android:src="@drawable/pinglun_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"/>
        <TextView
                android:id="@+id/tv_myspace_pinglun_count"
                android:layout_gravity="center_vertical"
                android:text="0"
                android:textColor="@color/normal_bar_futext_color"
                android:layout_marginLeft="@dimen/small_space"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>
<LinearLayout
            android:id="@+id/ll_myspace_reply_icons"
            android:paddingTop="@dimen/small_space"
            android:paddingRight="@dimen/middle_space"
            android:paddingLeft="@dimen/middle_space"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </LinearLayout>
上面的LinearLayout就是放這些頭像的,其餘的就很少說了,下面咱們看看怎麼來給咱們的adapter裏面加這些頭像

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
                    params.setMargins(8, 0, 8, 0);
                    roundedImageView.setLayoutParams(params);
                    roundedImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    if (!"".equals(replyUrl.get(m)) && replyUrl.get(m) != null) {
                        ImageLoader.getInstance().displayImage(replyUrl.get(m), roundedImageView);
                    } else {
                        roundedImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.usericon));
                    }
                    if (m == count) {
                        roundedImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wozaixianchangxiangqing_shenglve));
                    } else {
                        holder.llReplyIcons.addView(roundedImageView);
                    }
咱們先定義一個LayoutParams,設置頭像圖片的一些屬性,包括大小,margins以及scaletype等,而後給它設置到咱們的ImageView中,最後
holder.llReplyIcons.addView(roundedImageView);
添加子佈局就ok了。京東這個是固定了頭像的個數,而我寫的則是根據手機屏幕的寬度添加頭像:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int count = width / 116;
count就是能夠添加的頭像,當View中的i等於咱們的count的時候,咱們能夠用最後的省略號的圖片來顯示。

以前在羣裏有人問我這個頭像點擊跳轉到我的主頁怎麼實現的,想了一下,是否是能夠用手機觸摸的座標來算一下座標位於第幾個頭像之間,以爲那樣比較麻煩。咱們能夠在添加子佈局頭像的時候,就給這個子佈局設置點擊事件,就能夠了,看一下代碼:spa

for (int m = 0; m < replyUrl.size(); m++) {
                    RoundedImageView roundedImageView = new RoundedImageView(context);
                    final int finalM = m;
                    roundedImageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (story.getReply_user_id().get(finalM) != null) {
                                Intent intent = new Intent(context, MyStoryActivity.class);
                                intent.putExtra("userid", story.getReply_user_id().get(finalM));
                                intent.putExtra("user_iconurl", story.getReply_user_icon_url().get(finalM));
                                intent.putExtra("username", story.getReply_user_name().get(finalM));
                                intent.putExtra("flag", "others");
                                context.startActivity(intent);
                            } else {
                                Intent intent = new Intent(context, StoryFavoriteAcitvity.class);
                                intent.putExtra("storyId", story.getId());
                                context.startActivity(intent);
                            }

                        }
                    });
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
                    params.setMargins(8, 0, 8, 0);
                    roundedImageView.setLayoutParams(params);
                    roundedImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    if (!"".equals(replyUrl.get(m)) && replyUrl.get(m) != null) {
                        ImageLoader.getInstance().displayImage(replyUrl.get(m), roundedImageView);
                    } else {
                        roundedImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.usericon));
                    }
                    if (m == count) {
                        roundedImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wozaixianchangxiangqing_shenglve));
                    } else {
                        holder.llReplyIcons.addView(roundedImageView);
                    }

                }
這段代碼就全都包括了,其中一些裏面的參數是服務器返回來的,都是真實數據。這樣就能夠點擊頭像跳轉了。

那麼最後看一下我本身實現的界面是否是和京東的同樣呢?.net




怎麼樣是否是差很少?
code

對於這個圓形的頭像是重寫的一個RoundImageView,我以前的帖子也有介紹過,感興趣的小夥伴能夠看一下
Android 自定義上面圓角下面直角的ImageView 裏面也有一些相關的介紹,請你們多多關注,之後還會繼續維護個人博客,一些好的東西會拿出來跟你們分享,也但願各位多多指正不足之處共同窗習,共同進步。