时间:2021-05-20
//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.ItemDecorationpackage 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上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android自定义SpaceItemDecoration分割线的具体代码,供大家参考,具体内容如下此线是在RecyclerView中的Gr
androidRecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现RecyclerView的水平/垂直分割线,则需要继承
最近遇到一个问题,用RecyclerView显示数据,纵向列表显示,添加默认分割线。问题是:底部也会显示分割线,这很影响美观。怎么解决这个问题呢?我想了很多办法
1、前言刚开始学习RecyclerView的时候我跟着一个视频学的,当时添加分割线是从外面导入一个Java类,然后使用里面的函数来创建分割线的,所以一直以来我都
复制代码代码如下:for/rd:\\目录下的文件==========================华丽的分割线========================