1、先创建 activity_recycle_view.xml 和 activity_recycler_linear_item.xml

如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".RecycleViewActivity">
  8.  
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/re1"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. tools:layout_editor_absoluteX="202dp"
  14. tools:layout_editor_absoluteY="184dp" />
  15. </androidx.constraintlayout.widget.ConstraintLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. tools:context=".RecyclerLinearItemActivity"
  8. android:background="@color/colorBg">
  9.  
  10. <TextView
  11. android:id="@+id/textView2"
  12. android:layout_width="0dp"
  13. android:layout_height="60dp"
  14. android:text="@string/textview"
  15. app:layout_constraintBottom_toBottomOf="parent"
  16. app:layout_constraintEnd_toEndOf="parent"
  17. app:layout_constraintStart_toStartOf="parent"
  18. app:layout_constraintTop_toTopOf="parent"
  19. app:layout_constraintVertical_bias="0.0" />
  20. </androidx.constraintlayout.widget.ConstraintLayout>

2、创建LinearAdapter 适配器:

  1. package com.example.myapplication;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. import androidx.annotation.NonNull;
  11. import androidx.recyclerview.widget.RecyclerView;
  12.  
  13. public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
  14.  
  15. private Context context;
  16. public LinearAdapter(Context context){
  17. this.context = context;
  18. }
  19.  
  20. @NonNull
  21. @Override
  22. public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  23. return new LinearViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_recycler_linear_item,parent,false));
  24. }
  25.  
  26. @Override
  27. public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, final int position) {
  28. holder.tv.setText("Hello World");
  29.  
  30. holder.tv.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View view) {
  33. Toast.makeText(context,"click..."+position,Toast.LENGTH_SHORT).show();
  34. }
  35. });
  36. }
  37.  
  38. @Override
  39. public int getItemCount() {
  40. return 30;
  41. }
  42.  
  43. class LinearViewHolder extends RecyclerView.ViewHolder{
  44.  
  45. private TextView tv;
  46.  
  47. public LinearViewHolder(@NonNull View itemView) {
  48. super(itemView);
  49.  
  50. tv = itemView.findViewById(R.id.textView2);
  51. }
  52. }
  53. }

3、RecycleViewActivity.java 代码

  1. package com.example.myapplication;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.recyclerview.widget.LinearLayoutManager;
  6. import androidx.recyclerview.widget.RecyclerView;
  7.  
  8. import android.graphics.Rect;
  9. import android.os.Bundle;
  10. import android.view.View;
  11.  
  12. public class RecycleViewActivity extends AppCompatActivity {
  13.  
  14. private RecyclerView rv;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_recycle_view);
  19.  
  20. rv = findViewById(R.id.re1);
  21.  
  22. rv.setLayoutManager(new LinearLayoutManager(RecycleViewActivity.this));
  23. //设置分割线
  24. rv.addItemDecoration( new MyDecoration());
  25. rv.setAdapter(new LinearAdapter(RecycleViewActivity.this));
  26. }
  27.  
  28. class MyDecoration extends RecyclerView.ItemDecoration {
  29.  
  30. @Override
  31. public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
  32. super.getItemOffsets(outRect, view, parent, state);
  33.  
  34. outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.height));
  35.  
  36. }
  37. }
  38. }

4、效果如下:

android RecyclerView的Linear布局案例的更多相关文章

  1. android RecyclerView的Grid布局案例

    1.先创建activity_grid.xml 和 activity_grid_item.xml <?xml version="1.0" encoding="utf- ...

  2. android RecyclerView的瀑布流布局案例

    1.先创建 activity_water_fall.xml 和 activity_water_fall_item.xml <?xml version="1.0" encodi ...

  3. Android(java)学习笔记164:Relativelayout相对布局案例

    我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...

  4. Android开发---网格布局案例

     Android开发---网格布局案例 效果图: 1.MainActivity.java package com.example.android_activity; import android.ap ...

  5. android开发 RecyclerView 瀑布列表布局

    1.写一个内容的自定义小布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...

  6. 【Android】15.0 UI开发(六)——列表控件RecyclerView的网格布局排列实现

    1.0 列表控件RecyclerView的网格布局排列实现,关键词GridLayoutManager. LinearLayoutManager 实现顺序布局 GridLayoutManager 实现网 ...

  7. 【Android】14.0 UI开发(五)——列表控件RecyclerView的瀑布布局排列实现

    1.0 列表控件RecyclerView的瀑布布局排列实现,关键词StaggeredGridLayoutManager LinearLayoutManager 实现顺序布局 GridLayoutMan ...

  8. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  9. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

随机推荐

  1. 视频色彩空间RGB、YUV、YCbCr

    RGB.YUV和YCbCr都是人为规定的彩色模型或颜色空间(有时也叫彩色系统或彩色空间).它的用途是在某些标准下用通常可接受的方式对彩色加以描述.本质上,彩色模型是坐标系统和子空间的阐述. RGB R ...

  2. /etc/bashrc

    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[3 ...

  3. 一个简单的java爬虫

    直接上代码: package com.jeecg.util; import java.io.BufferedReader; import java.io.IOException; import jav ...

  4. hive中function函数查询

    1. desc function [函数名] desc function xpath; 查询用法: 2. desc function extended [函数名] desc function exte ...

  5. activiti 自定义用户

    https://blog.csdn.net/meng564764406/article/details/53789958 此文目的: 对网络上的关于对activiti 使用做一个总结,因为很难找到一个 ...

  6. opengl读取灰度图生成三维地形并添加光照

    转自:https://www.cnblogs.com/gucheng/p/10152889.html 准备第三方库 glew.freeglut.glm.opencv 准备一张灰度图 最终效果 代码如下 ...

  7. sort_buffer_size, Sort_merge_passes关系

    对于事务性工作负载是通常最快这个大小设置为32K,并且也是允许的最小尺寸.您应该谨慎使用它设置为较大的值,因为这可以很容易地降低性能. 如果所有的数据进行排序不适合在指定缓冲区大小的MySQL第一种类 ...

  8. 不借助其他任何软件防止QQ被盗的小技巧

    分享一个小技巧(防止QQ被盗号): 在登录的时候前面加个0,点击登录,如果显示账号不存在,是因为你没有在添加或注册账号这儿登录,也就是切换账号.PC端也可以,如下图:                 ...

  9. 跨域和CORS

    一 跨域 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...

  10. 17.Python略有小成(包,logging模块)

    Python(包,logging模块) 一.包 什么是包 官网解释 : 包是一种通过使用'.模块名'来组织python模块名称空间的方式 , 具体来讲 , 包就是一个包含有__ init __.py文 ...