android朋友圈九宫格用Recyclerview实现
//效果图
//话不多说,代码先给上关键代码
if (StringUtils.isEmpty(list.get(position).getImage())) {
holder.gridView.setVisibility(View.GONE);
} else {
holder.gridView.setVisibility(View.VISIBLE);
String[] images = list.get(position).getImage().split(",");
int imageCount = images.length;
int colNum;//列数
if (imageCount == 1) {
colNum = 1;
ViewGroup.LayoutParams layoutParams = holder.linyout_recyc.getLayoutParams();
layoutParams.height = (int) context.getResources().getDimension(R.dimen.dp_250);
layoutParams.width = (int) context.getResources().getDimension(R.dimen.dp_250);
} else if (imageCount == 2 || imageCount == 4) {
colNum = 2;
} else {
colNum = 3;
}
holder.imageAdapter.clear();//清空
holder.imageAdapter.addStringListAll(Arrays.asList(images));
holder.imageAdapter.addAoo(list.get(position));
holder.gridLayoutManager.setSpanCount(colNum);//设置列数
holder.imageAdapter.notifyDataSetChanged();
}
//在item获取id写
gridView = itemView.findViewById(R.id.recyc_view);
imageAdapter = new ImageAdapter(context);
int space = context.getResources().getDimensionPixelSize(R.dimen.dp_10);
gridLayoutManager = new GridLayoutManager(context, 3);
gridView.addItemDecoration(new SpacingItemDecoration(space));
gridView.setLayoutManager(gridLayoutManager);
gridView.setAdapter(imageAdapter);
//图片的适配器
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyHodler> {
private List<Object> mList = new ArrayList<>();
private List<DynamBean> beanList = new ArrayList<>();
private int sign;//0:普通点击,1自定义
private Context context;
public void addAll(List<Object> list) {
mList.addAll(list);
}
public void addStringListAll(List<String> list) {
mList.addAll(list);
}
public ImageAdapter(Context context) {
this.context = context;
}
public void setSign(int sign){
this.sign = sign;
}
@NonNull
@Override
public MyHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(viewGroup.getContext(), R.layout.circle_image_item, null);
return new MyHodler(view);
}
@Override
public void onBindViewHolder(@NonNull MyHodler myHodler, final int position) {
if (mList.get(position) instanceof String) {
String imageUrl = (String) mList.get(position);
if (imageUrl.startsWith("http:")||imageUrl.startsWith("https:")) {//加载http
myHodler.image.setImageURI(Uri.parse(imageUrl));
} else {//加载sd卡
Uri uri = Uri.parse("file://" + imageUrl);
myHodler.image.setImageURI(uri);
}
} else {//加载资源文件
int id = (int) mList.get(position);
Uri uri = Uri.parse("res://com.dingtao.rrmmp/" + id);
myHodler.image.setImageURI(uri);
}
myHodler.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sign == 1) {//自定义点击
if (position == 0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
WDActivity.getForegroundActivity().startActivityForResult(intent, WDActivity.PHOTO);
} else {
}
}else{
Intent intent = new Intent(context, CommentdetailsActivity.class);
intent.putExtra("dyname",beanList.get(0));
context.startActivity(intent);
}
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return mList.size();
}
public void clear() {
mList.clear();
}
public void add(Object image) {
if (image != null) {
mList.add(image);
}
}
public List<Object> getList() {
return mList;
}
public void addAoo(DynamBean dynamBean) {
beanList.add(dynamBean);
}
class MyHodler extends RecyclerView.ViewHolder {
SimpleDraweeView image;
public MyHodler(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.circle_image);
}
}
}
//好了到这,不用谢我
推荐阅读
-
android实现多图文分享朋友圈功能
-
Android中RecyclerView实现多级折叠列表效果(TreeRecyclerView)
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android控件RecyclerView实现混排效果仿网易云音乐
-
Android 中RecyclerView通用适配器的实现
-
Android实现九宫格解锁的实例代码
-
Android中RecyclerView实现分页滚动的方法详解
-
Android用RecyclerView实现动态添加本地图片
-
用Kotlin实现Android点击事件的方法
-
Android Recyclerview实现水平分页GridView效果示例