在安卓开发中,都会遇到listview刷新的功能,下面就是listview的分页功能
首先3个xml布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
items.xml
<?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="80dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="80dp" android:layout_weight="1" android:gravity="center" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:background="@color/colorAccent" android:id="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="80dp" android:gravity="center" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:background="@color/colorPrimary" android:textSize="19sp" android:id="@+id/textView2" /> </LinearLayout>loading.xml
<?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="30dp" android:gravity="center" android:orientation="horizontal"> <ProgressBar style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressBar" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text=" 正在拼命加载中......" android:id="@+id/textView3" /> </LinearLayout>最后java代码:
首先创建Fruit数据对象然后
public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener{ private ListView listView; private List<Fruit> fruits; private MyAdapter adapter; private static int DATA_UPDATE = 0X1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); listView.setOnScrollListener(this); adapter = new MyAdapter(); fruits = new ArrayList<>(); initData(); View loadbooter = getLayoutInflater().inflate(R.layout.loading,null); listView.addFooterView(loadbooter); listView.setAdapter(adapter); } private int indexs=1; private void initData() { for (int i = 0; i <16 ; i++) { Fruit fruit = new Fruit(); fruit.setName("--name-- "+indexs); fruit.setContent("--content-- "+indexs); fruits.add(fruit); indexs++; } } private int visibleLastIndex;//可显示的最后一条数据索引 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(adapter.getCount()==visibleLastIndex&&scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){ new LoadThread().start(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { visibleLastIndex = firstVisibleItem + visibleItemCount-1; } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==DATA_UPDATE){ adapter.notifyDataSetChanged(); } } }; class LoadThread extends Thread{ @Override public void run() { initData(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(DATA_UPDATE); } } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return fruits.size(); } @Override public Object getItem(int position) { return fruits.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHoder hoder; if(convertView==null){ convertView = getLayoutInflater().inflate(R.layout.items,null); hoder = new ViewHoder(); hoder.name = (TextView)convertView.findViewById(R.id.textView); hoder.content = (TextView) convertView.findViewById(R.id.textView2); convertView.setTag(hoder); }else { hoder = (ViewHoder) convertView.getTag(); } Fruit data = fruits.get(position); hoder.name.setText(data.getName()); hoder.content.setText(data.getContent()); return convertView; } class ViewHoder{ TextView name; TextView content; } } }