• ListView下拉加载一(分页)


    首先创建在主xml里放置一个listview列表,代码如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".MainActivity" >
    
        <ListView
            android:id="@+id/menuList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        </ListView>
    
    </LinearLayout>

    再创建2个xml,分别为列表项(menu.xml)和加载界面(loadmore.xml)

    menu.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="match_parent"
        android:orientation="vertical" >
        
        <TextView
            android:id="@+id/showView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:padding="10dp"
            style="@style/normalText"
            android:text="title"
            />
        <TextView 
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="123"
            />
    </LinearLayout>

    loadmore.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
         >
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@drawable/list_bg"
             >
        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="25dp"
            android:layout_height="25dp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载数据..." />
        </LinearLayout>
    </LinearLayout>

    loadmore的背景样式list_bg.xml:

    <?xml version="1.0" encoding="utf-8"?>  
    <shape xmlns:android="http://schemas.android.com/apk/res/android">  
          <gradient  
               android:startColor="#ccc"  
               android:endColor="#ccc"  
               android:angle="45"  
          />  
    </shape>  

    入口代码如下:

    package com.tp.soft.app;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MainActivity extends Activity{
    
        private ListView mListView;
        
        private View mLoadMoreView;
        
        private ProgressBar mLoadBtn;
        
        private PageAdapter adapter;
        
        private Handler handler = new Handler();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mLoadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
            
            mLoadBtn = (ProgressBar) mLoadMoreView.findViewById(R.id.progressBar1);
            //mLoadBtn.setOnClickListener(this);
            
            mListView = (ListView) findViewById(R.id.menuList);
            
            //下拉滚动触发事件
            mListView.setOnScrollListener(new OnScrollListener() {
                
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    //不滚动
                    if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
                        //滚动最底部
                        if(view.getLastVisiblePosition() == view.getCount() -1){
                            //mLoadBtn.setText("正在加载中...");
                            MyRunnable r = new MyRunnable();
                            handler.postDelayed(r, 2000);
                        }
                    }
                }
                
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                }
            });
            
            mListView.addFooterView(mLoadMoreView);
            List<String> itemList = new ArrayList<String>();
            for (int i = 0; i < 10; i++) {
                itemList.add("title"+i);
            }
            
            adapter = new PageAdapter(itemList);
            mListView.setAdapter(adapter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        /*@Override
        public void onClick(View v) {
            mLoadBtn.setText("正在加载中...");
            MyRunnable r = new MyRunnable();
            handler.postDelayed(r, 2000);
        }*/
        
        
        class MyRunnable implements Runnable{
    
            @Override
            public void run() {
                Log.e("提示", "123");
                loadMoreDate();
                //更新UI
                adapter.notifyDataSetChanged();
                //mLoadBtn.setText("查看更多...");
            }
        }
        
        
        
        class PageAdapter extends BaseAdapter {
            
            List<String> itemList;
            
            public PageAdapter(List<String> itemList){
                this.itemList = itemList;
            }
            
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(convertView == null){
                    convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.menu, null);
                }
                TextView titleView = (TextView) convertView.findViewById(R.id.showView);
                titleView.setText(itemList.get(position));
                return convertView;
            }
            
            @Override
            public long getItemId(int position) {
                return position;
            }
            
            @Override
            public Object getItem(int position) {
                return itemList.get(position);
            }
            
            @Override
            public int getCount() {
                return itemList.size();
            }
            
            public void addItem(String str){
                itemList.add(str);
            }
        }
        
        private void loadMoreDate() {
            Log.e("总数", ""+adapter.getCount());
            int count = adapter.getCount();
            for(int i = count+1; i < count+10; i++){
                adapter.addItem("title"+i);
            }
        }
    }

    运行结果:

  • 相关阅读:
    Java NIO类库Selector机制解析(上)
    SWT Display.getDefault() 和Display.getCurrent()的区别
    VSS 2005 复位 工作目录(Reset Working Folder)
    转:理解javascript中的delete机制(2)
    在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
    CSS样式
    Character Animator不显示NDI无法OBS直播
    【LoadRunner】基础使用教程:录制第一个脚本(包含遇到的错误问题解决)
    流式传输 之四流式协议
    全局变量,静态变量,局部变量
  • 原文地址:https://www.cnblogs.com/tplovejava/p/3953168.html
Copyright © 2020-2023  润新知