最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android動態(tài)加載布局

 更新時間:2016年02月02日 15:30:25   投稿:lijiao  
這篇文章主要介紹了Android動態(tài)加載布局,感興趣的小伙伴們可以參考一下

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

可以看到京東的故事里面的這樣一個布局,這個布局可以說是我目前見到的內(nèi)容比較多的了,它的每一項都包含頭像、姓名、分類、內(nèi)容、圖片、喜歡、評論、分享以及喜歡的頭像。分析了一下布局之后我們不難發(fā)現(xiàn),除了喜歡頭像這部分,其余的都很好實現(xiàn)。

那么下面著重說一下這個頭像這部分怎么實現(xiàn)?

第一種方案:我們可以用GridView來實現(xiàn),GridView和ListView的用法是一樣的,俗稱九宮格排列,那么我們可以將GridView的一行排列九張圖片來顯示這些頭像,只不過ListView嵌套著GridView稍微的有些麻煩,自己做了一個,感覺不太好用,有想要ListView嵌套GridView的可以私密我。

第二種方案:就是本篇文章所講的動態(tài)加載布局了:

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

<?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="受到了房間啊了會計分錄會計法艦隊司令減肥;立刻受到杰弗里斯到付款;老是覺得煩;老卡機的說法;就是看到的就發(fā)了卡就" 
   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,設(shè)置頭像圖片的一些屬性,包括大小,margins以及scaletype等,然后給它設(shè)置到我們的ImageView中,最后holder.llReplyIcons.addView(roundedImageView);  添加子布局就ok了。京東這個是固定了頭像的個數(shù),而我寫的則是根據(jù)手機屏幕的寬度添加頭像:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
  int width = wm.getDefaultDisplay().getWidth(); 
  int count = width / 116; 

count就是可以添加的頭像,當View中的i等于我們的count的時候,我們可以用最后的省略號的圖片來顯示。
之前在群里有人問我這個頭像點擊跳轉(zhuǎn)到個人主頁怎么實現(xiàn)的,想了一下,是不是可以用手機觸摸的坐標來算一下坐標位于第幾個頭像之間,覺得那樣比較麻煩。我們可以在添加子布局頭像的時候,就給這個子布局設(shè)置點擊事件,就可以了,看一下代碼:

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); 
     } 
 
    } 

這段代碼就全都包括了,其中一些里面的參數(shù)是服務器返回來的,都是真實數(shù)據(jù)。這樣就可以點擊頭像跳轉(zhuǎn)了。
那么最后看一下我自己實現(xiàn)的界面是不是和京東的一樣呢?

怎么樣是不是差不多?

相關(guān)文章

  • Android Bitmap的加載與緩存

    Android Bitmap的加載與緩存

    這篇文章主要介紹了Android Bitmap的加載與緩存的相關(guān)資料,需要的朋友可以參考下
    2018-03-03
  • Android Studio 新手入門教程(一)基本設(shè)置圖解

    Android Studio 新手入門教程(一)基本設(shè)置圖解

    這篇文章主要介紹了Android Studio 新手入門教程(一)基本設(shè)置圖解,需要的朋友可以參考下
    2017-12-12
  • Android開發(fā)小技巧篇之集合

    Android開發(fā)小技巧篇之集合

    這篇文章主要介紹了Android開發(fā)-小技巧篇(集合) 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 深入android Unable to resolve target ''android-XX''詳解

    深入android Unable to resolve target ''android-XX''詳解

    本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android使用Retrofit2.0技術(shù)仿微信發(fā)說說

    Android使用Retrofit2.0技術(shù)仿微信發(fā)說說

    這篇文章主要為大家詳細介紹了Android使用Retrofit2.0技術(shù)仿微信發(fā)說說,實現(xiàn)拍照,選圖庫,多圖案上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android開發(fā)改變字體顏色方法

    Android開發(fā)改變字體顏色方法

    用以下方法基本上可以解決大多數(shù)字體顏色設(shè)置問題,先發(fā)一篇,后續(xù)繼續(xù)發(fā)一篇高級的,通過用戶選擇的方式,改變字體顏色。
    2015-05-05
  • Android實現(xiàn)微信的圖片選擇器

    Android實現(xiàn)微信的圖片選擇器

    這篇文章主要為大家詳細介紹了Android實現(xiàn)微信的圖片選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實現(xiàn)精確到天時分秒的搶購倒計時

    Android實現(xiàn)精確到天時分秒的搶購倒計時

    這篇文章主要為大家詳細介紹了Android實現(xiàn)精確到天時分秒的搶購倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Android自定義可控制速度的跑馬燈

    Android自定義可控制速度的跑馬燈

    這篇文章主要為大家詳細介紹了Android自定義可控制速度的跑馬燈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Kotlin Fragment的具體使用詳解

    Kotlin Fragment的具體使用詳解

    Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應大屏幕的平板電腦, 當然現(xiàn)在他仍然是平板APP UI設(shè)計的寵兒,而且我們普通手機開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段
    2022-10-10

最新評論

河池市| 麟游县| 定西市| 若羌县| 清原| 毕节市| 武宣县| 四平市| 吴旗县| 逊克县| 铜鼓县| 金秀| 社会| 广西| 祁阳县| 灵川县| 商河县| 扶沟县| 阜阳市| 右玉县| 尉犁县| 古田县| 南康市| 从江县| 进贤县| 西和县| 新余市| 革吉县| 广水市| 嘉定区| 九龙县| 都兰县| 扶沟县| 清镇市| 定襄县| 平潭县| 丹凤县| 长泰县| 青龙| 星子县| 宜都市|