RecyclerView 切换内容展示样式,每行3列展示缩略图和列表展示相互切换
2017-09-11 16:02
531 查看
效果:
主要实现方法是通过使用RecyclerView中的GridLayoutManager,通过setSpanCount()方法,设置每行1列或每行三列。点击切换按钮后通过adapter的notifyDateSetChange方法,刷新列表,重走getItemViewType,onBindViewHolder,onCreateViewHolder的逻辑。达到切换视图的显示方式的目的。
点击切换按钮执行的方法:
两种样式的layout:
每行3图样式:
每行1图样式:
主要实现方法是通过使用RecyclerView中的GridLayoutManager,通过setSpanCount()方法,设置每行1列或每行三列。点击切换按钮后通过adapter的notifyDateSetChange方法,刷新列表,重走getItemViewType,onBindViewHolder,onCreateViewHolder的逻辑。达到切换视图的显示方式的目的。
点击切换按钮执行的方法:
private class ChangeShowTypeListener implements View.OnClickListener { @Override public void onClick(View v) { if (gridLayoutManager.getSpanCount() == SPAN_COUNT_ONE) { gridLayoutManager.setSpanCount(SPAN_COUNT_THREE); // user_homepage_change.setImageDrawable(getResources().getDrawable(R.mipmap.homepage_show_threepic)); recyclerView.scrollToPosition(0); // loadData(2); } else { gridLayoutManager.setSpanCount(SPAN_COUNT_ONE); // user_homepage_change.setImageDrawable(getResources().getDrawable(R.mipmap.homepage_show_onepic)); recyclerView.scrollToPosition(0); // loadData(2); } // ugcWebItemList.clear(); homePageAdapter.notifyItemRangeChanged(0, homePageAdapter.getItemCount()); } }
@Override public int getItemViewType(int position) { // if (position == 0) return TYPE_HEADER; int spanCount = mLayoutManager.getSpanCount(); if (spanCount == SPAN_COUNT_ONE) { return VIEW_TYPE_ONE; } else { return VIEW_TYPE_THREE; } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; /* if (viewType == TYPE_HEADER) { view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header_homepage, parent, false); return new HeaderViewHolder(view); }*/ if (viewType == VIEW_TYPE_ONE) { view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_onepic_homepage, parent, false); return new HomePageOnePicViewHolder(view); } else { view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_threepic_homepage, parent, false); return new HomePageThreePicViewHolder(view); } } @Override public void onBindViewHolder(final ViewHolder holder, int position) { final UserHomePageBean userHomePageItem = userHomePageBeanList.get(position); if (holder.getItemViewType() == VIEW_TYPE_THREE) { HomePageThreePicViewHolder homePageThreePicViewHolder = (HomePageThreePicViewHolder) holder; //设置每张小图的宽高为屏幕宽度的1/3 ViewGroup.LayoutParams layoutParams = homePageThreePicViewHolder.imageView.getLayoutParams(); layoutParams.height = layoutParams.width = (ScreenUtils.getScreenWidth(mContext)) / 3; //设置图片之间的间隔 homePageThreePicViewHolder.imageView.setLayoutParams(layoutParams); if (position == 0 || position% 3 == 0) { homePageThreePicViewHolder.itemView.setPadding(0, 0, ScreenUtils.dp2px(mContext, 2), ScreenUtils.dp2px(mContext, 3)); } else if (position- 1 == 0 || (position- 1) % 3 == 0) { homePageThreePicViewHolder.itemView.setPadding(ScreenUtils.dp2px(mContext, 1), 0, ScreenUtils.dp2px(mContext, 1), ScreenUtils.dp2px(mContext, 3)); } else if ((position+ 1) % 3 == 0) { homePageThreePicViewHolder.itemView.setPadding(ScreenUtils.dp2px(mContext, 2), 0, 0, ScreenUtils.dp2px(mContext, 3)); } ...... } else if (holder.getItemViewType() == VIEW_TYPE_ONE) { HomePageOnePicViewHolder homePageOnePicViewHolder = (HomePageOnePicViewHolder) holder; ...... } }
class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } class HomePageOnePicViewHolder extends ViewHolder { HomePageOnePicViewHolder(View itemView) { super(itemView); ...... } } class HomePageThreePicViewHolder extends ViewHolder { HomePage 4000 ThreePicViewHolder(View itemView) { super(itemView); imageView = (ImageView) itemView.findViewById(R.id.homepage_image); imageTag = (ImageView) itemView.findViewById(R.id.homepage_image_tag); } }
两种样式的layout:
每行3图样式:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/homepage_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" /> <ImageView android:id="@+id/homepage_image_tag" android:layout_width="25dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:src="@mipmap/homepage_video" /> </RelativeLayout>
每行1图样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/ll_root" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/homepage_rl_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" android:paddingTop="16dp"> .... 图片上面的控件.... <RelativeLayout android:id="@+id/homepage_rl_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/homepage_txt_content" android:layout_marginTop="12dp"> <ImageView android:id="@+id/homepage_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" /> <TextView android:id="@+id/homepage_image_toolong" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:background="#88000000" android:gravity="center" android:text="@string/view_full_image" android:textColor="#ffffff" android:textSize="12sp" android:visibility="gone" /> <TextView android:id="@+id/homepage_txt_count" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="#88000000" android:gravity="center" android:padding="7dp" android:text="5P" android:textColor="#ffffff" android:textSize="12sp" /> </RelativeLayout> <com.gif.gifchannel.videoplayer.TextureVideoPlayer android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/homepage_txt_content" android:layout_marginTop="12dp" android:visibility="gone" /> <LinearLayout android:id="@+id/ll_operate" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="8dp" android:paddingRight="8dp"> ...图片下面的控件... </LinearLayout> </LinearLayout> </LinearLayout>
相关文章推荐
- Android Kotlin(二)—— Kotlin与Retrofit进行网络请求RecyclerView展示图片列表
- 练习项目 一款新闻app的开发 (四):通过RecyclerView来展示新闻列表
- MVP登录+recyclerview列表展示(带刷新效果)
- 搜索商品+XRecyclerview展示列表
- 用RecyclerView展示列表时注意的问题
- android利用recyclerview展示带有日期的图片列表
- RecyclerView+ImageLoader图片列表的加载与展示【从listview加载图片性能优化引出的解决方法】
- 使用ScrollView制作Title渐变,配合RecyclerView展示列表
- Kotlin实现Retrofit网络请求RecyclerView展示图片列表
- Android使用OKHTTP网络框架请求数据,RecyclerView结合Glide展示图片,瀑布流布局样式
- GreenDao,注解,RecyclerView,EventBus,Retrofit展示列表
- 展示数据使用:recyclerview,retrofit,greendao,butterknife,eventbus,fresco。实现效果图列表。MVP模式。
- Android客户端之“微服私访”App的系统学习(七)XRecyclerView快速实现列表界面+自定义Search输入框,软键盘搜索按钮监听+TextView部分样式改变
- RecyclerView展示数据 点击图标切换界面布局
- Android 动态设置列表样式,不用ListView或者RecyclerView
- Android RecyclerView一键快速切换布局样式
- android studio recyclerview基础 自定义列表样式
- android标题栏RadioButton实现,下面展示切换viewpager,fragment,内容是listview
- RecyclerView切换布局(列表→网格)
- Android中RecyclerView点击item展开列表详细内容(超简单实现)