新闻列表页下拉刷新
1.使用谷歌官方的SwipeFreshLayout控件
简单使用
用<androidx.swiperefreshlayout.widget.SwipeRefreshLayout></androidx.swiperefreshlayout.widget.SwipeRefreshLayout>包裹RecyvlerView
设置颜色
swipeRefreshLayout.setColorSchemeColors(Color.GRAY)可以添加多种颜色
设置监听
swipeRefreshLayout.setOnRefreshListener { var pos:Int=this.intent.getStringExtra("posi").toInt() swipeRefreshLayout.isRefreshing=false }
新闻列表页上拉加载更多
思路是,在RecyclerView中新增一个item,判断如果Recycler滚动到最后一个item。从数据库读取更多数据,添加到原来的新闻列表的末尾。
创建一个布局的类LoadMoreView
class LoadMoreView:RelativeLayout{ constructor(context: Context):super(context) constructor(context: Context,attrs: AttributeSet):super(context,attrs) constructor(context: Context,attrs: AttributeSet,defStyleAttr:Int):super(context,attrs,defStyleAttr) init{ View.inflate(context, R.layout.view_loadmore,this) } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在新闻列表的adapter中,返回articleList.size+1条数据,最后的位置设置为LoadMoreView
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { if(viewType==TYPE_NORMAL) return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.newslist_item,parent,false)) else return MyViewHolder(LoadMoreView(parent.context)) }
在NewsListActivity中添加一下对RecyclerView的监听事件
rv_newslist.addOnScrollListener(object :RecyclerView.OnScrollListener(){ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { if(newState==RecyclerView.SCROLL_STATE_IDLE) { val layoutManager=recyclerView.layoutManager if(layoutManager is LinearLayoutManager){ val manager:LinearLayoutManager=layoutManager val lastPosition=manager.findLastVisibleItemPosition() if(lastPosition==adapter.itemCount-1){ //println("最后一条") //计算page loadMore(pos!!,adapter.itemCount-1) } } } } })
//loadMore
private fun loadMore(pos: Int,offset:Int) { var rows = 10 var page=offset/rows+1 if(offset%rows>0) page+=1 println("offset:$offset,offset${offset+(rows-offset%rows)}") var url = "http://~/JustTest/getArticle?page=" + page + "&rows=" + rows + "&pos=" + pos println(url) var client = OkHttpClient() var request = Request.Builder().get().url(url).build() var resp: Response? = null Thread { Looper.prepare() resp = client.newCall(request).execute() var result = resp!!.body?.string() //Toast.makeText(this,result,Toast.LENGTH_SHORT).show() var gson = Gson() var articleList: ArticleBeen = gson.fromJson(result, ArticleBeen::class.java) //Toast.makeText(this,articleList.article[7].toString(),Toast.LENGTH_SHORT).show() runOnUiThread { adapter.loadMore(articleList.article) } Looper.loop() }.start() }
//adapter.loadMore
fun loadMore(articleList:ArrayList<Article>){ this.articleList.addAll(articleList) notifyDataSetChanged() }
新闻列表条目点击事件,以及点击进入新闻详情页
这个跟展示首页新闻列表的新闻详情页是一样的了。这里不重复记录了。另外,可以给新闻详情页加一个toolbar,增加一个分享的功能
来源:https://www.cnblogs.com/vocus/p/12446597.html