CarouselLayoutManager的使用
来源:互联网 发布:csgo 知乎 编辑:程序博客网 时间:2024/06/03 13:14
先上图:
使用时使用 RecyclerView,除了 LayoutManager 使用 CarouselLayoutManager外,其余用法全和 RecyclerView 一样。
下面介绍具体的使用方法:
(1)添加依赖:
compile 'com.azoft.carousellayoutmanager:carousel:1.2.1'
(2)主要逻辑代码:
//垂直显示还是水平显示
CarouselLayoutManager layoutManager = new
CarouselLayoutManager(CarouselLayoutManager.VERTICAL);
CarouselLayoutManager(CarouselLayoutManager.VERTICAL);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter.setUrl(urls);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
//这两个必须设置
layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
recyclerView.addOnScrollListener(new CenterScrollListener());
(3)全部代码:
1)适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private Context context; private List<String> urls; public MyAdapter(Context context) { this.context = context; } public void setUrl(List<String> url) { this.urls = url; notifyDataSetChanged(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { Glide.with(context).load(urls.get(position)).into(holder.imageView); holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "position:" + position, Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return urls.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private ImageView imageView; public MyViewHolder(View itemView) { super(itemView); imageView = (ImageView) itemView.findViewById(R.id.iv); } }}
2)MainActivity:
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List<String> urls; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new MyAdapter(this); urls = new ArrayList<>(); for (int i = 0; i < 8; i++) { urls.add("http://cdn.indata3.com/images/20170630062508d2ib32lBty95o5RS/20170818/1503034118_10.jpeg"); } CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.VERTICAL); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); adapter.setUrl(urls); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(layoutManager); recyclerView.setHasFixedSize(true); layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener()); recyclerView.addOnScrollListener(new CenterScrollListener()); }}3)activity_main.xml:
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="sosee.demoglarypic.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>4)item.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_centerInParent="true" android:id="@+id/iv" android:layout_width="200dp" android:layout_height="260dp" /></RelativeLayout>
GitHub项目地址:https://github.com/Azoft/CarouselLayoutManager
阅读全文
1 0
- CarouselLayoutManager的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- linux 笔记(五)(Shell基础)
- 《程序员》2014年11月刊:电商峰值系统架构设计
- LINQ教程
- 类似淘宝页面的星星评价可复用
- 剑指offer_10 矩形覆盖
- CarouselLayoutManager的使用
- Unity3D实践学习(一)
- 移动端页面开发总结(一)
- 香蕉煮水(失眠)
- centos7 make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_jpeg2000.cpp.o] 错误 1
- swift as/as?/as!的区别
- 程序员表情包
- 小米网抢购系统开发实践和我的个人观察
- 移动App和远程服务器笔记(3)