這個吧我的頭都搞痛了,哎!!先看佈局
這種類型於朋友圈的這樣,最多9張至少一張的,剛開始決定每個大小size 用不同的LinearLayoutmanager來實現,一個用1,兩個就用gradmanager來實現,這類文字很多,因爲這裏的地址是網絡地址傳的是string,在自定義manager的時候發現,測量的高度始終爲0,這樣的話就無法顯示圖片,最後在本地使用一張圖片,發現可以顯示,那說明方法沒有用錯,最後就不得不放棄,另找一種方法。這個原因就花了一個下午的時間,因爲時間緊,所以就換個寫法。
用new Imageview方法,,這個方法可以實現!!直接上代碼:
主布句就一個relativelayout
<RelativeLayout
android:id="@+id/friend_cle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然後在適配器裏面實現一個方法:
public class FriendCircleView implements View.OnClickListener {
private onItemPositionClickListener mListener;
private RelativeLayout relativeLayout;
private ImageView imageView1,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8;
public void setOnListener(onItemPositionClickListener listener) {
this.mListener = listener;
}
@SuppressLint("ResourceType")
public void setImageView(RelativeLayout friendsCircleImageLayout, Context mContext, List<String> list) {
int width = ScreenUtil.getScreenWidth();
int height = ScreenUtil.getScreenWidth();
RequestOptions options = new RequestOptions().bitmapTransform(new CenterCropRoundCornerTransform(0))
.priority(Priority.NORMAL)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.place_white)
.error(R.drawable.place_white)
.centerCrop();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) friendsCircleImageLayout.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
int num = list.size();
friendsCircleImageLayout.removeAllViews();
/**
* 1
*/
imageView1 = new ImageView(mContext);
imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (num == 5) {
lp_1.width = width / 3 * 2;
lp_1.height = height / 3 * 2;
Glide.with(mContext).load(list.get(0)).apply(options).override(width / 3 * 2,height / 3 * 2).into(imageView1);
} else if (num == 6) {
lp_1.width = width / 3 * 2;
lp_1.height = height / 3 * 2;
Glide.with(mContext).load(list.get(0)).apply(options).override(width / 3 * 2,height / 3 * 2).into(imageView1);
} else if (num == 7) {
lp_1.width = width / 4 * 3;
lp_1.height = height / 4 * 3;
Glide.with(mContext).load(list.get(0)).apply(options).override(width / 4 * 3,height / 4 * 3).into(imageView1);
} else if (num == 8) {
lp_1.width = width / 4 * 3;
lp_1.height = height / 4 * 3;
Glide.with(mContext).load(list.get(0)).apply(options).override(width / 4 * 3,height / 4 * 3).into(imageView1);
}
imageView1.setLayoutParams(lp_1);
imageView1.setId(1);
// imageView1.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(0)).apply(options).into(imageView1);
// }
// });
friendsCircleImageLayout.addView(imageView1);
/**
* 2
*/
imageView2 = new ImageView(mContext);
imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 5) {
lp_2.width = width / 3;
lp_2.height = height / 3;
Glide.with(mContext).load(list.get(1)).apply(options).override(width / 3 ,height / 3 ).into(imageView2);
} else if (num == 6) {
lp_2.width = width / 3;
lp_2.height = height / 3;
Glide.with(mContext).load(list.get(1)).apply(options).override(width / 3 ,height / 3 ).into(imageView2);
} else if (num == 7) {
lp_2.width = width / 4;
lp_2.height = height / 4;
Glide.with(mContext).load(list.get(1)).apply(options).override(width / 4,height / 4).into(imageView2);
} else if (num == 8) {
lp_2.width = width / 4;
lp_2.height = height / 4;
Glide.with(mContext).load(list.get(1)).apply(options).override(width / 4,height / 4).into(imageView2);
}
lp_2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, imageView1.getId());
imageView2.setId(2);
imageView2.setLayoutParams(lp_2);
// imageView2.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(1)).apply(options).into(imageView2);
// }
// });
// Glide.with(mContext).load(list.get(1)).apply(options).into(imageView2);
friendsCircleImageLayout.addView(imageView2);
/**
* 3
*/
imageView3 = new ImageView(mContext);
imageView3.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_3 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 5) {
lp_3.width = width / 3;
lp_3.height = height / 3;
Glide.with(mContext).load(list.get(2)).apply(options).override(width / 3 ,height / 3 ).into(imageView3);
} else if (num == 6) {
lp_3.width = width / 3;
lp_3.height = height / 3;
Glide.with(mContext).load(list.get(2)).apply(options).override(width / 3 ,height / 3 ).into(imageView3);
} else if (num == 7) {
lp_3.width = width / 4;
lp_3.height = height / 4;
Glide.with(mContext).load(list.get(2)).apply(options).override(width / 4,height / 4).into(imageView3);
} else if (num == 8) {
lp_3.width = width / 4;
lp_3.height = height / 4;
Glide.with(mContext).load(list.get(2)).apply(options).override(width / 4,height / 4).into(imageView3);
}
lp_3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, imageView1.getId());
lp_3.addRule(RelativeLayout.BELOW, imageView2.getId());
imageView3.setId(3);
imageView3.setLayoutParams(lp_3);
// imageView3.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(2)).apply(options).into(imageView3);
// }
// });
// Glide.with(mContext).load(list.get(2)).apply(options).into(imageView3);
friendsCircleImageLayout.addView(imageView3);
/**
* 4
*/
imageView4 = new ImageView(mContext);
imageView4.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_4 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 5) {
lp_4.width = width / 2;
lp_4.height = height / 3;
lp_4.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(3)).apply(options).override(width / 2,height / 3 ).into(imageView4);
} else if (num == 6) {
lp_4.width = width / 3;
lp_4.height = height / 3;
lp_4.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(3)).apply(options).override(width / 3 ,height / 3 ).into(imageView4);
} else if (num == 7) {
lp_4.width = width / 4;
lp_4.height = height / 4;
lp_4.addRule(RelativeLayout.BELOW, imageView3.getId());
lp_4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, imageView1.getId());
Glide.with(mContext).load(list.get(3)).apply(options).override(width / 4,height / 4).into(imageView4);
} else if (num == 8) {
lp_4.width = width / 4;
lp_4.height = height / 4;
lp_4.addRule(RelativeLayout.BELOW, imageView3.getId());
lp_4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, imageView1.getId());
Glide.with(mContext).load(list.get(3)).apply(options).override(width / 4,height / 4).into(imageView4);
}
imageView4.setId(4);
imageView4.setLayoutParams(lp_4);
// imageView4.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(3)).apply(options).into(imageView4);
// }
// });
// Glide.with(mContext).load(list.get(3)).apply(options).into(imageView4);
friendsCircleImageLayout.addView(imageView4);
/**
* 5
*/
imageView5 = new ImageView(mContext);
imageView5.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_5 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 5) {//
lp_5.width = width / 2;
lp_5.height = height / 3;
lp_5.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, imageView4.getId());
lp_5.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(4)).apply(options).override(width /2,height / 3 ).into(imageView5);
} else if (num == 6) {
lp_5.width = width / 3;
lp_5.height = height / 3;
lp_5.addRule(RelativeLayout.RIGHT_OF, imageView4.getId());
lp_5.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(4)).apply(options).override(width / 3 ,height / 3 ).into(imageView5);
} else if (num == 7) {//
lp_5.width = width / 4 * 3 / 2;
lp_5.height = height / 4;
lp_5.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(4)).apply(options).override(width / 4 * 3 / 2,height / 4).into(imageView5);
} else if (num == 8) {//
lp_5.width = width / 4;
lp_5.height = height / 4;
lp_5.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(4)).apply(options).override(width / 4,height / 4).into(imageView5);
}
imageView5.setId(5);
imageView5.setLayoutParams(lp_5);
// imageView5.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(4)).apply(options).into(imageView5);
// }
// });
// Glide.with(mContext).load(list.get(4)).apply(options).into(imageView5);
friendsCircleImageLayout.addView(imageView5);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
/**
* 6
*/
if (num > 5) {
imageView6 = new ImageView(mContext);
imageView6.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_6 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 6) {
lp_6.width = width / 3;
lp_6.height = height / 3;
lp_6.addRule(RelativeLayout.RIGHT_OF, imageView5.getId());
lp_6.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(5)).apply(options).override(width / 3,height / 3).into(imageView6);
} else if (num == 7) {
lp_6.width = width / 4 * 3 / 2;
lp_6.height = height / 4;
lp_6.addRule(RelativeLayout.RIGHT_OF, imageView5.getId());
lp_6.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(5)).apply(options).override(width / 4*3/2,height / 4).into(imageView6);
} else if (num == 8) {
lp_6.width = width / 4;
lp_6.height = height / 4;
lp_6.addRule(RelativeLayout.RIGHT_OF, imageView5.getId());
lp_6.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(5)).apply(options).override(width / 4,height / 4).into(imageView6);
}
imageView6.setId(6);
imageView6.setLayoutParams(lp_6);
imageView6.post(new Runnable() {
@Override
public void run() {
Glide.with(mContext).load(list.get(5)).apply(options).into(imageView6);
}
});
// Glide.with(mContext).load(list.get(5)).apply(options).into(imageView6);
friendsCircleImageLayout.addView(imageView6);
imageView6.setOnClickListener(this);
if (num > 6){
/**
* 7
*/
imageView7 = new ImageView(mContext);
imageView7.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_7 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (num == 7) {
lp_7.width = width / 4;
lp_7.height = height / 4;
lp_7.addRule(RelativeLayout.RIGHT_OF, imageView6.getId());
lp_7.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(6)).apply(options).override(width / 4,height / 4).into(imageView7);
} else if (num == 8) {
lp_7.width = width / 4;
lp_7.height = height / 4;
lp_7.addRule(RelativeLayout.RIGHT_OF, imageView6.getId());
lp_7.addRule(RelativeLayout.BELOW, imageView1.getId());
Glide.with(mContext).load(list.get(6)).apply(options).override(width / 4,height / 4).into(imageView7);
}
imageView7.setId(7);
imageView7.setLayoutParams(lp_7);
// imageView7.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(6)).apply(options).into(imageView7);
// }
// });
// Glide.with(mContext).load(list.get(6)).apply(options).into(imageView7);
friendsCircleImageLayout.addView(imageView7);
imageView7.setOnClickListener(this);
if (num > 7){
/**
* 8
*/
imageView8 = new ImageView(mContext);
imageView8.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams lp_8 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp_8.width = width / 4;
lp_8.height = height / 4;
Glide.with(mContext).load(list.get(7)).apply(options).override(width / 4,height / 4).into(imageView8);
lp_8.addRule(RelativeLayout.RIGHT_OF, imageView7.getId());
lp_8.addRule(RelativeLayout.BELOW, imageView1.getId());
imageView8.setId(8);
imageView8.setLayoutParams(lp_8);
// imageView8.post(new Runnable() {
// @Override
// public void run() {
// Glide.with(mContext).load(list.get(7)).apply(options).into(imageView8);
// }
// });
// Glide.with(mContext).load(list.get(7)).apply(options).into(imageView8);
friendsCircleImageLayout.addView(imageView8);
imageView8.setOnClickListener(this);
}
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case 1:
mListener.onPositionClick(0);
break;
case 2:
mListener.onPositionClick(1);
break;
case 3:
mListener.onPositionClick(2);
break;
case 4:
mListener.onPositionClick(3);
break;
case 5:
mListener.onPositionClick(4);
break;
case 6:
mListener.onPositionClick(5);
break;
case 7:
mListener.onPositionClick(6);
break;
case 8:
mListener.onPositionClick(7);
break;
}
}
}
這裏是寫了5,6,7,8,這四種最不規則的格式,其他用layoutmanager就可以顯示。就能正常顯示了。但是這裏會有一個問題就是,添加一條數據,new Imageview就不管用了,刪除數據我這裏還沒有試過,不知道會不會出現,最後也不知道怎麼回事,很奇怪。可能是對recycler的刷新機制不瞭解。
第二種方法,也差不多,就是每個規格設置一種佈局,這個佈局比較多,比如:
LayoutInflater.from(mContext).inflate(R.layout.one_pic, null);
貼個代碼:
public class FriendCircleTwoView implements View.OnClickListener {
private ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6, imageView7, imageView8;
private LinearLayout.LayoutParams ll_layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
private onItemPositionClickListener mListener;
public void setOnListener(onItemPositionClickListener listener) {
this.mListener = listener;
}
public void setImageView(RelativeLayout friendsCircleImageLayout, Context mContext, List<String> list) {
int num = list.size();
int width = ScreenUtil.getScreenWidth();
int height = ScreenUtil.getScreenWidth();
ll_layoutParams.height = height;
ll_layoutParams.width = width;
friendsCircleImageLayout.removeAllViews();
if (num == 5) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_five_picture, null);
imageView1 = view.findViewById(R.id.img_1);
imageView2 = view.findViewById(R.id.img_2);
imageView3 = view.findViewById(R.id.img_3);
imageView4 = view.findViewById(R.id.img_4);
imageView5 = view.findViewById(R.id.img_5);
Glide.with(mContext).load(list.get(0)).into(imageView1);
Glide.with(mContext).load(list.get(1)).into(imageView2);
Glide.with(mContext).load(list.get(2)).into(imageView3);
Glide.with(mContext).load(list.get(3)).into(imageView4);
Glide.with(mContext).load(list.get(4)).into(imageView5);
view.setLayoutParams(ll_layoutParams);
friendsCircleImageLayout.addView(view);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
} else if (num == 6) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_six_picture, null);
imageView1 = view.findViewById(R.id.img_1);
imageView2 = view.findViewById(R.id.img_2);
imageView3 = view.findViewById(R.id.img_3);
imageView4 = view.findViewById(R.id.img_4);
imageView5 = view.findViewById(R.id.img_5);
imageView6 = view.findViewById(R.id.img_6);
Glide.with(mContext).load(list.get(0)).into(imageView1);
Glide.with(mContext).load(list.get(1)).into(imageView2);
Glide.with(mContext).load(list.get(2)).into(imageView3);
Glide.with(mContext).load(list.get(3)).into(imageView4);
Glide.with(mContext).load(list.get(4)).into(imageView5);
Glide.with(mContext).load(list.get(5)).into(imageView6);
view.setLayoutParams(ll_layoutParams);
friendsCircleImageLayout.addView(view);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
imageView6.setOnClickListener(this);
} else if (num == 7) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_seven_picture, null);
imageView1 = view.findViewById(R.id.img_1);
imageView2 = view.findViewById(R.id.img_2);
imageView3 = view.findViewById(R.id.img_3);
imageView4 = view.findViewById(R.id.img_4);
imageView5 = view.findViewById(R.id.img_5);
imageView6 = view.findViewById(R.id.img_6);
imageView7 = view.findViewById(R.id.img_7);
Glide.with(mContext).load(list.get(0)).into(imageView1);
Glide.with(mContext).load(list.get(1)).into(imageView2);
Glide.with(mContext).load(list.get(2)).into(imageView3);
Glide.with(mContext).load(list.get(3)).into(imageView4);
Glide.with(mContext).load(list.get(4)).into(imageView5);
Glide.with(mContext).load(list.get(5)).into(imageView6);
Glide.with(mContext).load(list.get(6)).into(imageView7);
view.setLayoutParams(ll_layoutParams);
friendsCircleImageLayout.addView(view);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
imageView6.setOnClickListener(this);
imageView7.setOnClickListener(this);
} else if (num == 8) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_eight_picture, null);
imageView1 = view.findViewById(R.id.img_1);
imageView2 = view.findViewById(R.id.img_2);
imageView3 = view.findViewById(R.id.img_3);
imageView4 = view.findViewById(R.id.img_4);
imageView5 = view.findViewById(R.id.img_5);
imageView6 = view.findViewById(R.id.img_6);
imageView7 = view.findViewById(R.id.img_7);
imageView8 = view.findViewById(R.id.img_8);
Glide.with(mContext).load(list.get(0)).into(imageView1);
Glide.with(mContext).load(list.get(1)).into(imageView2);
Glide.with(mContext).load(list.get(2)).into(imageView3);
Glide.with(mContext).load(list.get(3)).into(imageView4);
Glide.with(mContext).load(list.get(4)).into(imageView5);
Glide.with(mContext).load(list.get(5)).into(imageView6);
Glide.with(mContext).load(list.get(6)).into(imageView7);
Glide.with(mContext).load(list.get(7)).into(imageView8);
view.setLayoutParams(ll_layoutParams);
friendsCircleImageLayout.addView(view);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
imageView6.setOnClickListener(this);
imageView7.setOnClickListener(this);
imageView8.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_1:
mListener.onPositionClick(0);
break;
case R.id.img_2:
mListener.onPositionClick(1);
break;
case R.id.img_3:
mListener.onPositionClick(2);
break;
case R.id.img_4:
mListener.onPositionClick(3);
break;
case R.id.img_5:
mListener.onPositionClick(4);
break;
case R.id.img_6:
mListener.onPositionClick(5);
break;
case R.id.img_7:
mListener.onPositionClick(6);
break;
case R.id.img_8:
mListener.onPositionClick(7);
break;
}
}
}
這裏也只寫了5,6,7,8 也是可以完全實現的,這兩種都無奈方法,也能實現需求,最好的就是自定layoutmanager,,等以後有時間再試一試,之前爲了偷懶,1,2,3,4,9這幾種的規則是有順序的不用new Imageview就可以實現,只要每個佈局實現不用layoutmanager就可以實現了,但是在添加數據的時候也會出現那種問題,,有個別的問題會出現佈局會失效的原因,到底是不是這個原因還不知道,希望以後能夠知曉吧,最後解決的方法是在第二種上面,全部都用
LayoutInflater.from(mContext).inflate這個方法來寫,最後測試是正常的。
提示:在每次添加數據之前要移除所有的子佈局,不會圖片不會更新調用
removeAllViews方法 ok