Android 동적 로드 레이아웃

ListView 는 항상 사용 하고 있 습 니 다.다만 Adapter 의 내용 이 비교적 많 을 때 우 리 는 가끔 구성 요 소 를 설정 할 수 없습니다.예 를 들 어:

경 동의 이야기 속 의 이런 구 조 를 볼 수 있다.이 구 조 는 내 가 현재 본 내용 이 비교적 많다 고 할 수 있다.그의 모든 항목 은 프로필 사진,이름,분류,내용,그림,좋아 하 는 것,평론,공유 와 좋아 하 는 프로필 사진 을 포함한다.구 조 를 분석 한 후에 우 리 는 두상 이라는 부분 을 좋아 하 는 것 을 제외 하고 나머지 는 모두 잘 이 루어 진 다 는 것 을 알 게 되 었 다.
그럼 다음 에 다시 한 번 말씀 드 리 겠 습 니 다.이 프로필 사진 은 어떻게 이 루어 지나 요?
첫 번 째 방안:우 리 는 GridView 로 이 루어 질 수 있 습 니 다.GridView 와 ListView 의 용법 은 똑 같 습 니 다.속칭 구 궁 격 배열 이 라 고 부 릅 니 다.그러면 우 리 는 GridView 의 한 줄 을 9 장의 그림 으로 배열 하여 이 사진 들 을 표시 할 수 있 습 니 다.다만 ListView 에 GridView 가 내장 되 어 있 는 것 은 약간 번 거 로 울 뿐 스스로 만 들 었 습 니 다.느낌 이 좋 지 않 습 니 다.ListView 에 GridView 를 끼 워 넣 고 싶 은 사람 이 있 으 면 비밀 로 해 주세요.
두 번 째 방안:바로 이 글 에서 말 한 동적 로드 레이아웃 입 니 다.
아주 간단 합 니 다.우 리 는 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="                    ;           ;     ;      ;          " 
   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); 
     } 
크기,margins,scaletype 등 을 포함 하여 LayoutParams 를 정의 하고 이미지 뷰 에 마지막 으로 holder.llReply Icons.addView(rounded ImageView)를 설정 합 니 다.  하위 레이아웃 을 추가 하면 ok 입 니 다.경 동 씨 는 두상 의 개 수 를 고정 시 켰 고 저 는 핸드폰 화면의 너비 에 따라 두상 을 추 가 했 습 니 다.

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
  int width = wm.getDefaultDisplay().getWidth(); 
  int count = width / 116; 
count 는 추가 할 수 있 는 프로필 사진 입 니 다.View 의 i 가 count 와 같 을 때 마지막 생략 번호 의 그림 으로 표시 할 수 있 습 니 다.
앞서 그룹 에서 어떤 사람 이 이 프로필 사진 을 클릭 하여 개인 홈 페이지 로 넘 어 가 는 것 이 어떻게 이 루어 졌 는 지 물 었 다.핸드폰 으로 만 질 수 있 는 좌 표를 계산 해 보 니 좌 표 는 몇 번 째 프로필 사진 사이 에 있 는데 그렇게 번 거 로 웠 다.우 리 는 하위 레이아웃 프로필 사진 을 추가 할 때 이 하위 레이아웃 설정 에 이 벤트 를 클릭 하면 됩 니 다.코드 를 보 세 요.

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); 
     } 
 
    } 
이 코드 는 모두 포함 되 어 있 습 니 다.그 중 일부 인 자 는 서버 에서 돌아 온 것 이 고 모두 실제 데이터 입 니 다.이렇게 하면 프로필 사진 을 클릭 하여 점프 할 수 있다.
그럼 마지막 으로 제 가 이 룬 화면 을 보 겠 습 니 다.경 동 이랑 똑 같 지 않 나 요?

어 때 요,많이 다 르 지 않 아 요?

좋은 웹페이지 즐겨찾기