效果图:
1、activity_main.xml
描述:
定义了一个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListView_2" android:onClick="test_2"/> </LinearLayout>
2、MainActivity.java
描述:
进行了页面跳转
package com.example.android_ui_3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }public void test_2(View view){ Intent intent=new Intent(this,ListView_2Activity.class); startActivity(intent); } }
3、activity_list_view_2.xml
描述:
定义了一个ListView 和一个圆圈式进度条
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_list_view__base_adapter" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView_1" android:listSelector="@color/colorAccent" android:layout_width="match_parent" android:layout_height="match_parent"/> <ProgressBar android:id="@+id/pb" android:background="@android:color/holo_green_dark" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="60dp" android:visibility="invisible" /> </RelativeLayout>
4、ListView_2Activity.java
package com.example.android_ui_3; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ListView_2Activity extends Activity { //获取listView private ListView listView_1;
//声明一个list集合 private List<Map<String,Object>> list;
//声明一个适配器 private MyBaseAdapter adapter; private boolean isBottom=false;//是不是到底底部
//获取进度条 private ProgressBar pb; private int cpage=1;//当前默认第一页 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_2);
listView_1=(ListView)findViewById(R.id.listView_1);
pb=(ProgressBar)findViewById(R.id.pb); //获取数据源 loadData();
//实例化适配器,将list数据传入到适配器中 adapter=new MyBaseAdapter(list,this);
//给listView控件中绑定适配器 listView_1.setAdapter(adapter); //为ListView添加滚动事件监控 listView_1.setOnScrollListener(new AbsListView.OnScrollListener() { //当滚动状态发生改变的时候 @Override public void onScrollStateChanged(AbsListView absListView, int i) {
//scrollState有三种状态,分别是开始滚动(SCROLL_STATE_FLING),正在滚动(SCROLL_STATE_TOUCH_SCROLL), 已经停止(SCROLL_STATE_IDLE) if(i==SCROLL_STATE_IDLE){//停止滚动 Toast.makeText(ListView_2Activity.this,"停止滚动了",Toast.LENGTH_SHORT).show(); } if(i==SCROLL_STATE_IDLE&&isBottom){//停止滚动并且到底了,加载下一页 Toast.makeText(ListView_2Activity.this,"到底部了",Toast.LENGTH_SHORT).show();
//到底部了之后,如果你再往上拉,那么这个圆圈式的进度条将会变得可见
//View.VISIBLE View可见;View.INVISIBLE View不可以见,但仍然占据可见时的大小和位置;View.GONE View不可见,且不占据空间。
pb.setVisibility(View.VISIBLE);
//通过线程来控制进度条 new Thread(new Runnable() { @Override public void run() {
//休眠2秒 SystemClock.sleep(2000);
runOnUiThread(new Runnable() { @Override public void run() {
//进度条不可见 pb.setVisibility(View.INVISIBLE);
//继续加载下一页 cpage++;//下一页
//循换出数据
//因为一页是显示10个数据,所以第二页是从(cpage-1)*10+1开始 for(int i=(cpage-1)*10+1;i<=cpage*10;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("userImage",R.mipmap.ic_launcher); map.put("userName","小明"+i); map.put("userInfo","这是第"+i+"个小明"); list.add(map); }
//刷新UI adapter.notifyDataSetChanged(); } }); } }).start(); } } /** * 滚动中 * @param absListView * @param i 起点 * @param i1 滚动了多少项数 * @param i2 总项数 */ @Override public void onScroll(AbsListView absListView, int i, int i1, int i2) { if(i+i1==i2){ isBottom=true; }else{ isBottom=false; } } }); } //构建数据源 private void loadData(){ list=new ArrayList<Map<String,Object>>(); for(int i=1;i<=10;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("userImage",R.mipmap.ic_launcher); map.put("userName","小明"+i); map.put("userInfo","这是第"+i+"个小明"); list.add(map); } } //构建一个适配器内部类 class MyBaseAdapter extends BaseAdapter { //接收将要绑定给UI的数据源 private List<Map<String,Object>> list; //数据绑定给谁 private Context context; //构造方法 MyBaseAdapter(List<Map<String,Object>> list,Context context){ this.list=list; this.context=context; } @Override public int getCount() { if(list!=null) return list.size(); return 0; } @Override public Object getItem(int i) { if(list!=null) return list.get(i); return null; } @Override public long getItemId(int i) { if(list!=null) return i; return 0; } /** * 构建每一个Item,多次调用,每次构建一项 * @param i 当前第几项 * @param view 被回收的Item * @param viewGroup * @return */ @Override public View getView(final int i, View view, ViewGroup viewGroup) { if(list!=null){ Log.i("create view","创建第"+i+"项"); UserItem userItem; if(view==null){ Log.i("create view","第"+i+"次加载View"); userItem=new UserItem(); //获取要绑定数据的UI[UI描绘在XML文件中,需要将XML解析成一个View对象] view=View.inflate(context,R.layout.layout_userlist,null); userItem.userImage=(ImageView)view.findViewById(R.id.userImage); userItem.userName=(TextView)view.findViewById(R.id.userName); userItem.userInfo=(TextView)view.findViewById(R.id.userInfo); userItem.delete=(TextView)view.findViewById(R.id.deleteText); view.setTag(userItem); }else{ userItem=(UserItem)view.getTag(); } //为UI绑定数据 final Map<String,Object> map=list.get(i); userItem.userImage.setImageResource((Integer) map.get("userImage")); userItem.userName.setText(map.get("userName").toString()); userItem.userInfo.setText(map.get("userInfo").toString()); //为UI绑定事件 userItem.delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Toast.makeText(context,"将要删除:"+map.get("userName").toString(),Toast.LENGTH_SHORT).show(); list.remove(i); adapter.notifyDataSetChanged(); } }); return view; } return null; } } final class UserItem{ ImageView userImage; TextView userName,userInfo,delete; } }
5、ListView_2Activity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:orientation="horizontal"> <ImageView android:id="@+id/userImage" android:layout_width="60dp" android:layout_height="70dp" /> <LinearLayout android:orientation="horizontal" android:layout_marginLeft="3dp" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_weight="5" android:layout_width="0dp" android:layout_height="match_parent"> <TextView android:id="@+id/userName" android:textSize="28sp" android:layout_width="match_parent" android:layout_height="40dp" /> <TextView android:id="@+id/userInfo" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="30dp" /> </LinearLayout> <TextView android:id="@+id/deleteText" android:textSize="25sp" android:gravity="center_horizontal|bottom" android:text="删除" android:layout_weight="1" android:layout_gravity="right" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> </LinearLayout>