1. 程式人生 > >xamarin下用c#開發安卓的listvew翻頁功能

xamarin下用c#開發安卓的listvew翻頁功能

listview的翻頁用到了幾個知識點,1資料的填充,2翻頁圖片的載入,3頁碼的計算,實際效果如下圖:


axml檔案有兩個,一個是主介面Main.axml,另一個是顯示“載入中。。。。”這個layout1.axml

Main.axml程式碼如下:

<?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:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1" />
</LinearLayout>

layout1.axml程式碼如下:

<?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:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:gravity="center">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/progressBar1" />
        <TextView
            android:text="載入中......"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1" />
    </LinearLayout>

</LinearLayout>

程式檔案MainActivity.CS 如下:

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Android.Runtime;
using Android.Views;


namespace App12
{
    [Activity(Label = "App12", MainLauncher = true)]
    public class MainActivity : Activity
    {
        ListView list_member;
        SimpleAdapter content;
        int visiablecount;
        int totalcount;
        int first;
        View foot ;
        int pageindex =1 ;//當前頁碼
        int pagesize =10; //每頁的條數
        IList<IDictionary<string, object>> data;
      
            protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
             
            SetContentView(Resource.Layout.Main);
            list_member = FindViewById<ListView>(Resource.Id.listView1);
           
            //填充資料
            FillDate();
                  
            //顯示載入更多
            // LayoutInflater inflater = LayoutInflater.UnregisterFromRuntime(this);
            LayoutInflater inflater = LayoutInflater.FromContext(this);
            foot = inflater.Inflate(Resource.Layout.layout1,null);
            list_member.AddFooterView(foot);
          
            //隱藏底部的載入圖示
            foot.Visibility = ViewStates.Gone;


            //listview拉倒底部
            list_member.Scroll += List_member_Scroll;
         
            // 滾動條狀態變化
            list_member.ScrollStateChanged += List_member_ScrollStateChanged;
           
        }


        private void List_member_ScrollStateChanged(object sender, AbsListView.ScrollStateChangedEventArgs e)
        {
           
            // throw new System.NotImplementedException();
            //判斷是否停止滾動
            if (e.ScrollState == ScrollState.Idle && (first+visiablecount)==totalcount )
            {
                //  Toast.MakeText(this, "滾動條停下來了!", ToastLength.Long).Show();
                foot.Visibility = ViewStates.Visible; //顯示載入下一頁
                pageindex++;
                FillDate(); //填充資料到下頁
            }
        }


        private void List_member_Scroll(object sender, AbsListView.ScrollEventArgs e)
        {
            //throw new System.NotImplementedException();
            first = e.FirstVisibleItem; //第一項的編號
            totalcount = e.TotalItemCount;//列表總條數
            visiablecount = e.VisibleItemCount;//每頁條數
                }


        public void FillDate()
        {
            int PageEnd = pageindex * pagesize + 1; //當前頁最後一條資料的編號
            int PageStart = (pageindex - 1) * pagesize;//當前頁最後一項的編號

            data = new List<IDictionary<string, object>>(); 

         //本程式會跟著 pageindex++無限迴圈翻頁

            for (int i = 3; i < PageEnd; i++) 
            {
                //我這裡面的資料是寫死的,有資料庫的同學可以連資料庫
                JavaDictionary<string, object> achievement = new JavaDictionary<string, object>();
                achievement.Add("title", "你好,我是張" + i);
                achievement.Add("number","座號"+i);
                data.Add(achievement);
            }
            //建立資料介面卡
            content = new SimpleAdapter(
                this,
                data,
                Android.Resource.Layout.SimpleListItem2,
                new string[] { "title", "number" },
                new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 });
            //把資料繫結到list_member 這個listview上
            list_member.Adapter = content;
            list_member.SetSelection(PageStart); //當前頁第一項從哪頁開始
           
        }
    }
}

這個程式除錯了一段時間,希望大家玩的開心!

日行一善!

本專案在csdn的下載地址:

https://download.csdn.net/download/u014194297/10328782