標籤:mode tor osi oid support encoding str xmlns for
在弄android重新整理的時候,可算是耗費了一番功夫,最後發覺有現成的控制項,並且非常好用,這裡記錄一下。
原文是 78781682 ,這裡是看了之後,結合自己實際遇到的問題寫的。
首先引入包。
//下拉框 implementation ‘com.android.support:recyclerview-v7:28.0.0-beta01‘ implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3‘ implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.0.3‘//沒有使用特殊Header,可以不加這行
recyclerview 是為了資料顯示用,因為在使用中,發現 使用ListView 會報錯,不過我看別人用成功了的,具體我也懶得去查了。
recyclerview 以後具體在學習吧,反正是個比較好用的東西就是了。
首先是分頁檔 activity_main.xml
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/refreshLayout"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:background="#fff" /></com.scwang.smartrefresh.layout.SmartRefreshLayout>
接著是具體的布局顯示檔案: item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center"> <ImageView android:id="@+id/iv_image" android:layout_width="60dp" android:layout_height="60dp" android:gravity="center" android:background="@mipmap/ic_launcher"></ImageView> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="30dp" android:text="title" android:gravity="center" ></TextView> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="30dp" android:text="content1231231231232323" android:gravity="center" ></TextView> </LinearLayout></LinearLayout>
顯示的bean 對象,
package com.example.administrator.shuaxin;public class ItemBean { int itemImage; String itemTitle; String itemContent; public ItemBean(int itemImage , String itemTitle, String itemContent) { this.itemTitle = itemTitle; this.itemContent = itemContent; this.itemImage = itemImage; }}
適配器MyAdapter:
package com.example.administrator.shuaxin;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import java.util.List;public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ private List<ItemBean> mList; static class ViewHolder extends RecyclerView.ViewHolder{ View myView; ImageView imageView; TextView title; TextView content; public ViewHolder(View itemView) { super(itemView); myView = itemView; imageView = (ImageView) itemView.findViewById(R.id.iv_image); title = (TextView) itemView.findViewById(R.id.tv_title); content = (TextView) itemView.findViewById(R.id.tv_content); } } public MyAdapter(List<ItemBean> list){ this.mList = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null); final ViewHolder holder = new ViewHolder(view); return holder; } //將資料繫結到控制項上 @Override public void onBindViewHolder(ViewHolder holder, int position) { ItemBean bean = mList.get(position); holder.imageView.setBackgroundResource(bean.itemImage); holder.title.setText(bean.itemTitle); holder.content.setText(bean.itemContent); } @Override public int getItemCount() { return mList.size(); } //下面兩個方法提供給頁面重新整理和載入時調用 public void add(List<ItemBean> addMessageList) { //增加資料 int position = mList.size(); mList.addAll(position, addMessageList); notifyItemInserted(position); } public void refresh(List<ItemBean> newList) { //重新整理資料 mList.removeAll(mList); mList.addAll(newList); notifyDataSetChanged(); }}
然後是Activity檔案。
package com.example.administrator.shuaxin;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import com.scwang.smartrefresh.header.MaterialHeader;import com.scwang.smartrefresh.layout.api.RefreshLayout;import com.scwang.smartrefresh.layout.footer.ClassicsFooter;import com.scwang.smartrefresh.layout.header.ClassicsHeader;import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;import com.scwang.smartrefresh.layout.listener.OnRefreshListener;import java.util.ArrayList;import java.util.Date;import java.util.List; public class MainActivity extends AppCompatActivity { private List<ItemBean> list; private MyAdapter myAdapter; private RecyclerView recyclerView; RefreshLayout refreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout); //初始資料載入 initDate(); //重新整理資料載入 setPullRefresher(); } private void initDate(){ list = new ArrayList<ItemBean>(); for (int i=0;i<20;i++){ list.add(new ItemBean( R.mipmap.ic_launcher, "initTitle"+i, new Date().toString()+"" )); } myAdapter = new MyAdapter(list); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); LinearLayoutManager layoutManager = new LinearLayoutManager(this);//縱向線性布局 recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(myAdapter); } private void setPullRefresher(){ //設定 Header 為 MaterialHeader //設定 Header 為 ClassicsFooter 比較經典的樣式 refreshLayout.setRefreshHeader(new ClassicsHeader(this)); //設定 Footer 為 經典樣式 refreshLayout.setRefreshFooter(new ClassicsFooter(this)); refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { //在這裡執行上拉重新整理時的具體操作(網路請求、更新UI等) //類比網路請求到的資料 ArrayList<ItemBean> newList = new ArrayList<ItemBean>(); for (int i=0;i<20;i++){ newList.add(new ItemBean( R.mipmap.ic_launcher, "newTitle"+i, new Date().toString()+"" )); } myAdapter.refresh(newList); refreshlayout.finishRefresh(2000/*,false*/); //不傳時間則立即停止重新整理 傳入false表示重新整理失敗 } }); refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { //類比網路請求到的資料 ArrayList<ItemBean> newList = new ArrayList<ItemBean>(); for (int i=0;i<20;i++){ newList.add(new ItemBean( R.mipmap.ic_launcher, "addTitle"+i, new Date().toString()+"" )); } myAdapter.add(newList); //在這裡執行下拉載入時的具體操作(網路請求、更新UI等) refreshlayout.finishLoadmore(2000/*,false*/);//不傳時間則立即停止重新整理 傳入false表示載入失敗 } }); } }
看下效果。
Android 下拉重新整理上啦載入SmartRefreshLayout + RecyclerView