Android中RecyclerView上拉下拉,分割线,多条目的实例代码
//activity的xml <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns: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就一个背景颜色不同 <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns: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 <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns: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,有上啦下拉,添加分割线, packagebaway.com.cj.myrecyclerview2; importandroid.graphics.Color; importandroid.os.Handler; importandroid.support.v4.widget.SwipeRefreshLayout; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.support.v7.widget.DefaultItemAnimator; importandroid.support.v7.widget.LinearLayoutManager; importandroid.support.v7.widget.RecyclerView; importandroid.util.Log; importandroid.view.View; importandroid.widget.ProgressBar; importandroid.widget.Toast; importjava.util.ArrayList; publicclassMainActivityextendsAppCompatActivity{ privateRecyclerViewrecyclerView; privateMainAdaptermainAdapter; privateSwipeRefreshLayoutswipeRefreshLayout; privateLinearLayoutManagerlinearLayoutManager; privateProgressBarprogress; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } privatevoidinitView(){ recyclerView=(RecyclerView)findViewById(R.id.recyclerView); swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout); progress=(ProgressBar)findViewById(R.id.progress); linearLayoutManager=newLinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); mainAdapter=newMainAdapter(this); mainAdapter.setOnClickListener(newMyItemOnClickListener(){ @Override publicvoidonItemClick(Viewview,intposition){ Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show(); } }); recyclerView.setAdapter(mainAdapter); recyclerView.addItemDecoration(newMyItemDecoration()); recyclerView.setItemAnimator(newDefaultItemAnimator()); swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE); swipeRefreshLayout.setProgressViewOffset(true,0,200); swipeRefreshLayout.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener(){ @Override publicvoidonRefresh(){ newHandler().postDelayed(newRunnable(){ @Override publicvoidrun(){ swipeRefreshLayout.setRefreshing(false); } },3000); } }); recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener(){ @Override publicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){ super.onScrolled(recyclerView,dx,dy); intlastVisibleItemPosition=linearLayoutManager.findLastVisibleItemPosition(); intitemCount=linearLayoutManager.getItemCount(); if(lastVisibleItemPosition+1>=itemCount&&dy>0){ progress.setVisibility(View.VISIBLE); newHandler().postDelayed(newRunnable(){ @Override publicvoidrun(){ progress.setVisibility(View.GONE); } },3000); } } @Override publicvoidonScrollStateChanged(RecyclerViewrecyclerView,intnewState){ super.onScrollStateChanged(recyclerView,newState); } }); } privatevoidinitData(){ ArrayList<String>strings=newArrayList<>(); for(inti=0;i<50;i++){ strings.add("name"+i); } mainAdapter.setData(strings); mainAdapter.notifyDataSetChanged(); } } //adpter有多条目展示 packagebaway.com.cj.myrecyclerview2; importandroid.content.Context; importandroid.support.v7.widget.RecyclerView; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; importjava.util.ArrayList; /** *Createdbycjon2017/1/12. */ publicclassMainAdapterextendsRecyclerView.Adapter{ privateContextcontext; privateArrayList<String>datas=newArrayList<>(); privateMyItemOnClickListenermyItemOnClickListener; privatefinalintTYPE_0=0; privatefinalintTYPE_1=1; publicMainAdapter(Contextcontext){ this.context=context; } @Override publicintgetItemViewType(intposition){ if(position%3==0){ returnTYPE_0; }else{ returnTYPE_1; } } publicvoidsetOnClickListener(MyItemOnClickListeneronClickListener){ this.myItemOnClickListener=onClickListener; } @Override publicRecyclerView.ViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ if(viewType==TYPE_0){ Viewinflate=LayoutInflater.from(context).inflate(R.layout.item_main,null); returnnewMyViewHolder(inflate); }else{ Viewinflate=LayoutInflater.from(context).inflate(R.layout.item_main2,null); returnnewMyViewHolder2(inflate); } } @Override publicvoidonBindViewHolder(RecyclerView.ViewHolderholder,finalintposition){ if(getItemViewType(position)==TYPE_0){ MyViewHoldermyViewHolder=(MyViewHolder)holder; myViewHolder.name.setText(datas.get(position)); myViewHolder.name.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ myItemOnClickListener.onItemClick(v,position); } }); }else{ MyViewHolder2myViewHolder=(MyViewHolder2)holder; myViewHolder.name.setText(datas.get(position)); myViewHolder.name.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ myItemOnClickListener.onItemClick(v,position); } }); } } @Override publicintgetItemCount(){ returndatas.size(); } publicvoidsetData(ArrayList<String>data){ if(data==null){ return; } this.datas.addAll(data); } publicclassMyViewHolderextendsRecyclerView.ViewHolder{ publicTextViewname; publicMyViewHolder(ViewitemView){ super(itemView); name=(TextView)itemView.findViewById(R.id.name); } } publicclassMyViewHolder2extendsRecyclerView.ViewHolder{ publicTextViewname; publicMyViewHolder2(ViewitemView){ super(itemView); name=(TextView)itemView.findViewById(R.id.name); } } } //自定义分割线继承系统的RecyclerView.ItemDecoration packagebaway.com.cj.myrecyclerview2; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Rect; importandroid.support.v7.widget.RecyclerView; importandroid.util.Log; importandroid.view.View; /** *Createdbycjon2017/1/12. */ publicclassMyItemDecorationextendsRecyclerView.ItemDecoration{ privatePaintpaint; publicMyItemDecoration(){ paint=newPaint(); paint.setColor(Color.BLUE); } @Override publicvoidonDraw(Canvasc,RecyclerViewparent,RecyclerView.Statestate){ super.onDraw(c,parent,state); //c.drawCircle(300,300,300,paint); intchildCount=parent.getChildCount(); for(inti=0;i<childCount;i++){ ViewchildAt=parent.getChildAt(i); c.drawRect(childAt.getLeft(),childAt.getBottom(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint); } } @Override publicvoidonDrawOver(Canvasc,RecyclerViewparent,RecyclerView.Statestate){ super.onDrawOver(c,parent,state); //intchildCount=parent.getChildCount(); //for(inti=0;i<childCount;i++){ //ViewchildAt=parent.getChildAt(i); //c.drawRect(childAt.getLeft(),childAt.getTop(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint); //} } @Override publicvoidgetItemOffsets(RectoutRect,Viewview,RecyclerViewparent,RecyclerView.Statestate){ super.getItemOffsets(outRect,view,parent,state); outRect.set(20,0,20,5); } }
以上所述是小编给大家介绍的Android中RecyclerView上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!