码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView 可以与CollapsingToolbarLayout一起使用

时间:2018-05-13 10:39:24      阅读:1417      评论:0      收藏:0      [点我收藏+]

标签:super   hold   sch   分享图片   XML   count()   position   override   xmlns   

Item  布局

技术分享图片

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="120dp">
 6     <ImageView
 7         android:id="@+id/relevantrecommendations_iv"
 8         android:layout_width="150dp"
 9         android:layout_height="120dp"
10         android:scaleType="center"
11         android:src="@drawable/psb"
12         />
13     <TextView
14         android:id="@+id/relevantrecommendations_tv"
15         android:layout_width="match_parent"
16         android:layout_height="match_parent"
17         android:gravity="center_vertical"
18         android:text="title"
19         android:textSize="20sp"
20         />
21 </LinearLayout>

 

 

MyRecyclerView >> MyAdapter >> ViewHolder

 1 /*  Project  为一个javaBean*/
 2 public class MyRecyclerView extends RecyclerView{
 3     private  Context context;
 4     private  List<Project> projectList = new ArrayList<>();
 5     public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
 6         super(context, attrs);
 7         this.context = context;
 8         initProject();
 9         LinearLayoutManager layoutManager = new LinearLayoutManager(context);
10         this.setLayoutManager(layoutManager);
11 
12         MyAdapter myAdapter = new MyAdapter(projectList);
13         this.setAdapter(myAdapter);
14     }
15     private void initProject() {
16         /**
17          *
18          *   获取数据
19          */
20         String imageURL = "http://f.hiphotos.baidu.com/image/pic/item/35a85edf8db1cb13f423dfa0d154564e92584b3f.jpg";
21         String videoURL = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
22         Project project = new Project(1,"AA",imageURL,videoURL,0,0,"BB");
23         projectList.add(project);
24         projectList.add(project);
25         projectList.add(project);
26         projectList.add(project);
27         projectList.add(project);
28         projectList.add(project);
29         projectList.add(project);
30         projectList.add(project);
31     }
32   class MyAdapter extends  RecyclerView.Adapter<MyAdapter.ViewHolder>{
33     private List<Project> projectList;
34 
35       public MyAdapter(List<Project> projectList) {
36           this.projectList = projectList;
37       }
38 
39       @NonNull
40       @Override
41       public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
42           View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.relevantrecommendationsitem,parent,false);
43           ViewHolder holder = new ViewHolder(view);
44           return holder;
45       }
46 
47       @Override
48       public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
49             Project project = projectList.get(position);
50           Glide.with(context).load(project.getImageURL()).into(holder.projectImage);
51           holder.projectText.setText(project.getTitle());
52       }
53       @Override
54       public int getItemCount() {
55           return projectList.size();
56       }
57 
58       public class ViewHolder extends RecyclerView.ViewHolder {
59           ImageView projectImage;
60           TextView projectText;
61           public ViewHolder(View itemView) {
62               super(itemView);
63               projectImage = itemView.findViewById(R.id.relevantrecommendations_iv);
64               projectText  = itemView.findViewById(R.id.relevantrecommendations_tv);
65           }
66       }
67   }
68 }

 

RecyclerView 可以与CollapsingToolbarLayout一起使用

标签:super   hold   sch   分享图片   XML   count()   position   override   xmlns   

原文地址:https://www.cnblogs.com/the-wang/p/9031147.html

(2)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!