I am working for Android app, in which I need to show listview with items. But There are more elements to show in listview.
我正在为Android应用程序工作,我需要在其中显示带有项目的listview。但是listview中还有更多元素要显示。
I decided to implement pagination . I tried searching in Google but does not find any related information.
我决定实施分页。我尝试在Google搜索,但没有找到任何相关信息。
Can anybody help me please..
有人可以帮帮我吗
2 个解决方案
#1
25
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分页Ex-1
ListView分页Ex-2
#2
2
I have created this custom listview.
我创建了这个自定义列表视图。
package com.pixecon.custom.views;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ListView;
public class PagingListView extends ListView {
boolean needScroll = true;
private String TAG = "CustomListView";
public PagingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNeedScroll(boolean needScroll) {
this.needScroll = needScroll;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!needScroll) {
return false;
}
else {
return super.onInterceptTouchEvent(event);
}
}
@Override
public void setOnScrollListener(OnScrollListener l) {
super.setOnScrollListener(l);
}
public void onScrollStarted() {
//Scroll Started.
}
public void onScrollStopped() {
centralizeTheView();
}
private void centralizeTheView() {
int height = getResources().getDisplayMetrics().heightPixels;
int centerY = height / 2;
int visibleFirstItemPosition = getFirstVisiblePosition();
int visibleLastItemPosition = getLastVisiblePosition();
Log.d(TAG, "visibleFirstVisible = " + visibleFirstItemPosition);
Log.d(TAG, "visibleLastVisible = " + visibleLastItemPosition);
//TODO: Check the calcualted value and reconfirm.
int firstPositionInGroup = visibleFirstItemPosition % 2 - 1;
if (firstPositionInGroup < 0) {
firstPositionInGroup = 0;
}
FrameLayout layout = (FrameLayout) getChildAt(firstPositionInGroup);
int bottom = layout.getBottom();
//Calculate the exact bottom of the view by subtracting
if (bottom > centerY) {
Log.d(TAG, "First Item - " + visibleFirstItemPosition);
/*listView.smoothScrollBy(3, 1);*/
//listView.smoothScrollToPosition(visibleFirstItemPosition);
setSelection(visibleFirstItemPosition);
}
else {
Log.d(TAG, "Last Item - " + visibleLastItemPosition);
/*listView.smoothScrollBy(-3, 1);*/
//listView.smoothScrollToPosition(visibleLastItemPosition);
setSelection(visibleLastItemPosition);
}
}
}
#1
25
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分页Ex-1
ListView分页Ex-2
#2
2
I have created this custom listview.
我创建了这个自定义列表视图。
package com.pixecon.custom.views;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ListView;
public class PagingListView extends ListView {
boolean needScroll = true;
private String TAG = "CustomListView";
public PagingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNeedScroll(boolean needScroll) {
this.needScroll = needScroll;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!needScroll) {
return false;
}
else {
return super.onInterceptTouchEvent(event);
}
}
@Override
public void setOnScrollListener(OnScrollListener l) {
super.setOnScrollListener(l);
}
public void onScrollStarted() {
//Scroll Started.
}
public void onScrollStopped() {
centralizeTheView();
}
private void centralizeTheView() {
int height = getResources().getDisplayMetrics().heightPixels;
int centerY = height / 2;
int visibleFirstItemPosition = getFirstVisiblePosition();
int visibleLastItemPosition = getLastVisiblePosition();
Log.d(TAG, "visibleFirstVisible = " + visibleFirstItemPosition);
Log.d(TAG, "visibleLastVisible = " + visibleLastItemPosition);
//TODO: Check the calcualted value and reconfirm.
int firstPositionInGroup = visibleFirstItemPosition % 2 - 1;
if (firstPositionInGroup < 0) {
firstPositionInGroup = 0;
}
FrameLayout layout = (FrameLayout) getChildAt(firstPositionInGroup);
int bottom = layout.getBottom();
//Calculate the exact bottom of the view by subtracting
if (bottom > centerY) {
Log.d(TAG, "First Item - " + visibleFirstItemPosition);
/*listView.smoothScrollBy(3, 1);*/
//listView.smoothScrollToPosition(visibleFirstItemPosition);
setSelection(visibleFirstItemPosition);
}
else {
Log.d(TAG, "Last Item - " + visibleLastItemPosition);
/*listView.smoothScrollBy(-3, 1);*/
//listView.smoothScrollToPosition(visibleLastItemPosition);
setSelection(visibleLastItemPosition);
}
}
}