【发布时间】:2010-04-08 06:19:28
【问题描述】:
是否可以有一个包含 2 列或更多列的列表视图,可使用分页属性进行操作(即一次列表视图将在单列中仅显示 4 个项目,按下右箭头将显示接下来的 4 个项目)..
您能告诉我实施它的程序或任何想法吗?
谢谢
普奈特
【问题讨论】:
-
您能否详细说明您的问题或提供一些屏幕截图。
是否可以有一个包含 2 列或更多列的列表视图,可使用分页属性进行操作(即一次列表视图将在单列中仅显示 4 个项目,按下右箭头将显示接下来的 4 个项目)..
您能告诉我实施它的程序或任何想法吗?
谢谢
普奈特
【问题讨论】:
不确定这是否是您要查找的内容,但基本上此代码会根据您用于填充列表的主数据集创建对象子集。它是这样的:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
// Constant for limiting array to match desired number of values in column
private final int NUMBER_OF_ITEMS_IN_COLUMN = 4;
// Index for starting point of array subset
private int mStartingIndex = 0;
// Data set array for list
private String[] mDataSet = new String[]{
"One", "Two", "Three", "Four", "Five",
"Six","Seven","Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eightteen", "Nineteen", "Twenty",
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changeListViewModel(0);
}
private void changeListViewModel(int startingIndex) {
// Check staring index meets certain criteria
if(startingIndex < 0)
startingIndex = 0;
else if(startingIndex >= mDataSet.length)
startingIndex -= NUMBER_OF_ITEMS_IN_COLUMN;
// Set starting and ending index
mStartingIndex = startingIndex;
int endingIndex = startingIndex + NUMBER_OF_ITEMS_IN_COLUMN;
// Make sure ending index isn't outside the bounds of the data set array
if(endingIndex > mDataSet.length) endingIndex = mDataSet.length;
// Create subset and set listview adapter
String[] subSet = getDataSubset(startingIndex, endingIndex);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subSet));
}
private String[] getDataSubset(int startingIndex, int endingIndex){
String[] toRet = new String[endingIndex - startingIndex];
int index = -1;
for(int x = startingIndex; x < endingIndex; x++)
toRet[++index] = mDataSet[x];
return toRet;
}
/*
* Called from layout main.xml file
*/
public void backButtonClicked(View v) {
changeListViewModel(mStartingIndex - NUMBER_OF_ITEMS_IN_COLUMN);
}
/*
* Called from layout main.xml file
*/
public void nextButtonClicked(View v) {
changeListViewModel(mStartingIndex + NUMBER_OF_ITEMS_IN_COLUMN);
}
}
这是非常基本的,但它应该能够让你开始。另外,使用类似的方法,您还可以通过一个数据库类将列表视图与 SQLite 数据库联系起来,该数据库类将对象列表作为列表视图的子集返回。
您可以在这里下载源代码:Download Source
【讨论】: