Android中RecyclerView上拉下拉,分割线,多条目的实例代码

作者:孙宇波 时间:2022-10-14 06:05:07 


//activity的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="baway.com.cj.myrecyclerview2.MainActivity">
 <android.support.v4.widget.SwipeRefreshLayout
   android:id="@+id/swipeRefreshLayout"
   android:layout_width="match_parent"
   android:layout_above="@+id/progress"
   android:layout_height="match_parent">
   <android.support.v7.widget.RecyclerView
     android:id="@+id/recyclerView"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
   </android.support.v7.widget.RecyclerView>
 </android.support.v4.widget.SwipeRefreshLayout>
 <ProgressBar
   android:id="@+id/progress"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:visibility="gone"
   android:layout_alignParentBottom="true"/>
</RelativeLayout>
item1的xml和item2的xml就一个背景颜色不同
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#f00000"
 tools:context="baway.com.cj.myrecyclerview2.MainActivity">
 <TextView
   android:id="@+id/name"
   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:textSize="20sp"
   android:gravity="center"
   android:textColor="#00ff00"/>
</RelativeLayout>
item2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#f000ff"
 tools:context="baway.com.cj.myrecyclerview2.MainActivity">
 <TextView
   android:id="@+id/name"
   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:textSize="20sp"
   android:gravity="center"
   android:textColor="#00ff00"/>
</RelativeLayout>
//activity,有上啦下拉,添加分割线,
package baway.com.cj.myrecyclerview2;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
 private RecyclerView recyclerView;
 private MainAdapter mainAdapter;
 private SwipeRefreshLayout swipeRefreshLayout;
 private LinearLayoutManager linearLayoutManager;
 private ProgressBar progress;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   initView();
   initData();
 }
 private void initView() {
   recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
   swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
   progress = (ProgressBar)findViewById(R.id.progress);
   linearLayoutManager = new LinearLayoutManager(this);
   recyclerView.setLayoutManager(linearLayoutManager);
   mainAdapter = new MainAdapter(this);
   mainAdapter.setOnClickListener(new MyItemOnClickListener() {
     @Override
     public void onItemClick(View view, int position) {
       Toast.makeText(MainActivity.this, " "+position, Toast.LENGTH_SHORT).show();
     }
   });
   recyclerView.setAdapter(mainAdapter);
   recyclerView.addItemDecoration(new MyItemDecoration());
   recyclerView.setItemAnimator(new DefaultItemAnimator());
   swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
   swipeRefreshLayout.setProgressViewOffset(true,0,200);
   swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
     @Override
     public void onRefresh() {
       new Handler().postDelayed(new Runnable() {
         @Override
         public void run() {
           swipeRefreshLayout.setRefreshing(false);
         }
       },3000);
     }
   });
   recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
     @Override
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
       super.onScrolled(recyclerView, dx, dy);
       int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
       int itemCount = linearLayoutManager.getItemCount();
       if (lastVisibleItemPosition + 1 >= itemCount && dy > 0) {
         progress.setVisibility(View.VISIBLE);
         new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
             progress.setVisibility(View.GONE);
           }
         },3000);
       }
     }
     @Override
     public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
       super.onScrollStateChanged(recyclerView, newState);
     }
   });
 }
 private void initData() {
   ArrayList<String> strings = new ArrayList<>();
   for (int i = 0; i < 50; i++) {
     strings.add("name "+i);
   }
   mainAdapter.setData(strings);
   mainAdapter.notifyDataSetChanged();
 }
}
//adpter有多条目展示
package baway.com.cj.myrecyclerview2;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by cj on 2017/1/12.
*/
public class MainAdapter extends RecyclerView.Adapter {
 private Context context;
 private ArrayList<String> datas = new ArrayList<>();
 private MyItemOnClickListener myItemOnClickListener;
 private final int TYPE_0 = 0;
 private final int TYPE_1 = 1;
 public MainAdapter(Context context) {
   this.context = context;
 }
 @Override
 public int getItemViewType(int position) {
   if (position %3 == 0) {
     return TYPE_0;
   } else {
     return TYPE_1;
   }
 }
 public void setOnClickListener(MyItemOnClickListener onClickListener) {
   this.myItemOnClickListener = onClickListener;
 }
 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   if (viewType == TYPE_0) {
     View inflate = LayoutInflater.from(context).inflate(R.layout.item_main, null);
     return new MyViewHolder(inflate);
   } else {
     View inflate = LayoutInflater.from(context).inflate(R.layout.item_main2, null);
     return new MyViewHolder2(inflate);
   }
 }
 @Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
   if (getItemViewType(position) == TYPE_0) {
     MyViewHolder myViewHolder = (MyViewHolder)holder;
     myViewHolder.name.setText(datas.get(position));
     myViewHolder.name.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
         myItemOnClickListener.onItemClick(v,position);
       }
     });
   } else {
     MyViewHolder2 myViewHolder = (MyViewHolder2)holder;
     myViewHolder.name.setText(datas.get(position));
     myViewHolder.name.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
         myItemOnClickListener.onItemClick(v,position);
       }
     });
   }
 }
 @Override
 public int getItemCount() {
   return datas.size();
 }
 public void setData(ArrayList<String> data) {
   if (data == null) {
     return;
   }
   this.datas.addAll(data);
 }
 public class MyViewHolder extends RecyclerView.ViewHolder{
   public TextView name;
   public MyViewHolder(View itemView) {
     super(itemView);
     name = (TextView) itemView.findViewById(R.id.name);
   }
 }
 public class MyViewHolder2 extends RecyclerView.ViewHolder{
   public TextView name;
   public MyViewHolder2(View itemView) {
     super(itemView);
     name = (TextView) itemView.findViewById(R.id.name);
   }
 }
}
//自定义分割线继承系统的 RecyclerView.ItemDecoration
package baway.com.cj.myrecyclerview2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
/**
* Created by cj on 2017/1/12.
*/
public class MyItemDecoration extends RecyclerView.ItemDecoration {
 private Paint paint;
 public MyItemDecoration() {
   paint = new Paint();
   paint.setColor(Color.BLUE);
 }
 @Override
 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
   super.onDraw(c, parent, state);
   //c.drawCircle(300,300,300,paint);
   int childCount = parent.getChildCount();
   for (int i = 0; i < childCount; i++) {
     View childAt = parent.getChildAt(i);
     c.drawRect(childAt.getLeft(),childAt.getBottom(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
   }
 }
 @Override
 public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
   super.onDrawOver(c, parent, state);
//    int childCount = parent.getChildCount();
//    for (int i = 0; i < childCount; i++) {
//      View childAt = parent.getChildAt(i);
//      c.drawRect(childAt.getLeft(),childAt.getTop(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
//    }
 }
 @Override
 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
   super.getItemOffsets(outRect, view, parent, state);
   outRect.set(20,0,20,5);
 }
}

以上所述是小编给大家介绍的Android中RecyclerView上拉下拉,分割线,多条目的实例代码网站的支持!

来源:http://blog.csdn.net/qq_36508024/article/details/54405455

标签:recyclerview,分割线
0
投稿

猜你喜欢

  • java9中gc log参数迁移

    2022-06-28 03:21:01
  • Android开发使用Message对象分发必备知识点详解

    2022-02-08 21:32:49
  • springboot如何实现自动装配源码解读

    2023-11-10 15:44:20
  • java与c#的语法区别详细介绍

    2022-05-02 14:01:10
  • c#基于opencv,开发摄像头播放程序

    2023-06-20 11:54:29
  • springboot 整合fluent mybatis的过程,看这篇够了

    2022-09-14 01:16:53
  • Java利用File类创建文件的示例代码

    2022-02-09 20:53:54
  • android动态布局之动态加入TextView和ListView的方法

    2022-11-25 11:28:16
  • Android Lottie实现中秋月饼变明月动画特效实例

    2023-06-19 12:41:17
  • 适配android7.0获取文件的Uri的方法

    2022-03-13 17:32:06
  • C#虚方法的声明和使用实例教程

    2022-09-26 16:07:55
  • SpringBoot中使用Servlet三大组件的方法(Servlet、Filter、Listener)

    2021-07-19 18:41:43
  • Java实现的对称加密算法AES定义与用法详解

    2021-10-16 22:16:05
  • C#游戏开发之实现贪吃蛇游戏

    2023-01-28 01:48:48
  • Java实现Excel导入导出的步骤详解

    2022-09-05 05:20:32
  • Spring Boot 文件上传与下载的示例代码

    2021-08-30 02:04:53
  • Android 中 SwipeLayout一个展示条目底层菜单的侧滑控件源码解析

    2022-03-10 14:25:33
  • Android基于TextView属性android:ellipsize实现跑马灯效果的方法

    2022-09-28 15:59:55
  • Android 如何从零开始写一款书籍阅读器的示例

    2022-10-20 17:52:08
  • C#图像处理之木刻效果实现方法

    2022-05-20 04:28:13
  • asp之家 软件编程 m.aspxhome.com