如何在Android listview中实现分页 [英] How to implement pagination in Android listview

查看:29
本文介绍了如何在Android listview中实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 应用工作,我需要在其中显示带有项目的列表视图.但是在列表视图中还有更多元素要显示.

我决定实施分页.我尝试在 Google 中搜索,但没有找到任何相关信息.

谁能帮帮我..

解决方案

实现分页非常简单.

看看这个...

public class MainActivity extends Activity {私有 ListView 列表视图;私有 TextView 标题;私有 ArrayList数据;ArrayAdapter标准差;公共 int TOTAL_LIST_ITEMS = 1030;公共 int NUM_ITEMS_PAGE = 100;私人 int noOfBtns;私人按钮[] btns;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listview = (ListView)findViewById(R.id.list);标题 = (TextView)findViewById(R.id.title);Btnfooter();数据 = 新的 ArrayList();/** ArrayList 数据包含所有列表项*/for(int i=0;isort = new ArrayList();int start = number * NUM_ITEMS_PAGE;for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++){if(i<data.size()){sort.add(data.get(i));}别的{休息;}}sd = new ArrayAdapter(this,android.R.layout.simple_list_item_1,种类);listview.setAdapter(sd);}}

Xml 文件:

<文本视图android:id="@+id/标题"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textColor="@android:color/black"机器人:重力=中心"android:textSize="16sp"android:background="@android:color/darker_gray"android:padding="10dp"/><列表视图android:id="@+id/列表"android:divider="#000"机器人:dividerHeight="1dp"android:cacheColorHint="#00000000"android:layout_width="fill_parent"android:layout_height="0dp"机器人:layout_weight="1"android:fadingEdge="none"/><水平滚动视图android:layout_width="fill_parent"android:layout_height="wrap_content"><线性布局android:id="@+id/btnLay"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:方向=水平"></LinearLayout></Horizo​​ntalScrollView></LinearLayout>

更清晰的解释和源代码访问这个链接

ListView 分页 Ex-1

ListView 分页 Ex-2

I am working for Android app, in which I need to show listview with items. But There are more elements to show in listview.

I decided to implement pagination . I tried searching in Google but does not find any related information.

Can anybody help me please..

解决方案

Implementing pagination is very simple.

Take look at this...

public class MainActivity extends Activity {  

    private ListView listview;
    private TextView title;

   private ArrayList<String> data;
    ArrayAdapter<String> sd;


    public int TOTAL_LIST_ITEMS = 1030;
    public int NUM_ITEMS_PAGE   = 100;
    private int noOfBtns;
    private Button[] btns;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView)findViewById(R.id.list);
        title    = (TextView)findViewById(R.id.title);

        Btnfooter();

        data = new ArrayList<String>();

        /*
         * The ArrayList data contains all the list items
         */
        for(int i=0;i<TOTAL_LIST_ITEMS;i++)
        {
            data.add("This is Item "+(i+1));
        }

        loadList(0);

        CheckBtnBackGroud(0);

    }

    private void Btnfooter()
    {
        int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
        val = val==0?0:1;
        noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;

        LinearLayout ll = (LinearLayout)findViewById(R.id.btnLay);

        btns = new Button[noOfBtns];

        for(int i=0;i<noOfBtns;i++)
        {
            btns[i] =   new Button(this);
            btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
            btns[i].setText(""+(i+1));

            LinearLayout.LayoutParams lp = new     LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            ll.addView(btns[i], lp);

            final int j = i;
            btns[j].setOnClickListener(new OnClickListener() {

                public void onClick(View v)
                {
                    loadList(j);
                    CheckBtnBackGroud(j);
                }
            });
        }

    }

    /**
     * Method for Checking Button Backgrounds
     */
    private void CheckBtnBackGroud(int index)
    {
        title.setText("Page "+(index+1)+" of "+noOfBtns);
        for(int i=0;i<noOfBtns;i++)
        {
            if(i==index)
            {
                btns[index].setBackgroundDrawable(getResources().getDrawable(R.drawable.box_green));
                btns[i].setTextColor(getResources().getColor(android.R.color.white));
            }
            else
            {
                btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
                btns[i].setTextColor(getResources().getColor(android.R.color.black));
            }
        }

    }

    /**
     * Method for loading data in listview
     * @param number
     */
    private void loadList(int number)
    {
        ArrayList<String> sort = new ArrayList<String>();

        int start = number * NUM_ITEMS_PAGE;
        for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
        {
            if(i<data.size())
            {
                sort.add(data.get(i));
            }
            else
            {
                break;
            }
        }
        sd = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                sort);
        listview.setAdapter(sd);
    }
}

Xml file:

<?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="vertical" >

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:gravity="center"
    android:textSize="16sp"
    android:background="@android:color/darker_gray"
    android:padding="10dp"/>

<ListView
    android:id="@+id/list"
    android:divider="#000"
    android:dividerHeight="1dp"
    android:cacheColorHint="#00000000"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fadingEdge="none"/>

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/btnLay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</HorizontalScrollView>
</LinearLayout>

For more clear explanation and source code visit this links

ListView Pagination Ex-1

ListView Pagination Ex-2

这篇关于如何在Android listview中实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆