Item 布局
![](https://images2018.cnblogs.com/blog/1074973/201805/1074973-20180513095815680-703238898.png)
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 }