• RecyclerView 可以与CollapsingToolbarLayout一起使用


    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 }
  • 相关阅读:
    C#中使用SendMessage在进程间传递数据的实例
    解决C# WINFORM程序只允许运行一个实例的几种方法详解
    实现WinForm窗体的美化(借助第三方控件)
    sql2008拒绝了对对象 (数据库 ,架构'dbo')的SELECT权限
    在一个SQL Server表中一行的多个列找出最大值
    如何搭建MVC + EF 框架
    灵活运用 SQL SERVER FOR XML PATH
    CentOS7切换到root用户和退回普通用户
    Zuul Read Time out 错误
    neo4j---删除关系和节点
  • 原文地址:https://www.cnblogs.com/the-wang/p/9031147.html
Copyright © 2020-2023  润新知