Android中RecyclerView上拉下拉,分割线,多条目的实例代码
2019/7/7 20:58:37
本文主要是介绍Android中RecyclerView上拉下拉,分割线,多条目的实例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
//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上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!
这篇关于Android中RecyclerView上拉下拉,分割线,多条目的实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路