我创建了一个gridview,我需要在用户向下滚动10个项目后加载更多,例如,一个按钮会出现更多,我需要在网格内的项目列表中追加10个项目,这是我的代码:
MainActivity.java
public class MainActivity extends ActionBarActivity {
private List<GridItem> correspondenceList;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid_view);
correspondenceList = new ArrayList<GridItem>();
correspondenceList.add(new GridItem("1", "subj1"));
correspondenceList.add(new GridItem("2", "subj2"));
correspondenceList.add(new GridItem("3", "subj3"));
correspondenceList.add(new GridItem("4", "subj4"));
correspondenceList.add(new GridItem("5", "subj5"));
correspondenceList.add(new GridItem("6", "subj6"));
correspondenceList.add(new GridItem("7", "subj7"));
correspondenceList.add(new GridItem("8", "subj8"));
correspondenceList.add(new GridItem("9", "subj9"));
correspondenceList.add(new GridItem("10", "subj10"));
correspondenceList.add(new GridItem("11", "subj11"));
MyArrayAdapteradapter = new MyArrayAdapter(this, R.layout.grid_item, correspondenceList);
gridView.setAdapter(adapter);
}
MyArrayAdapter.java
public class MyArrayAdapter extends BaseAdapter{
private Context context;
private int resourceId;
private List<Correspondance> fields;
public MyArrayAdapter(Context context, int resource, List<GridItem> objects) {
this.context = context;
this.resourceId = resource;
this.fields = objects;
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) this.context)
.getLayoutInflater();
convertView = inflater.inflate(this.resourceId, parent, false);
}
GridItem corres = fields.get(position);
TextView gridSubjectTextView = (TextView) convertView
.findViewById(R.id.grid_subj);
gridSubjectTextView.setText(Html.fromHtml("<big><b>Subject</b><br></big><i>" + corres.subject + "</i><br>"));
return convertView;
}
activity_main.xml中
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="2"
android:gravity="center"
android:horizontalSpacing="30dp"
android:verticalSpacing="30dp"
android:paddingTop="30dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:clipToPadding="false"
android:scrollbars="horizontal"
android:stretchMode="columnWidth" />
答案 0 :(得分:2)
你可以这样做。
public class MainActivity extends ActionBarActivity implements OnScrollListener {
private List<GridItem> correspondenceList;
GridView gridView;
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int currentScrollState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid_view);
correspondenceList = new ArrayList<GridItem>();
correspondenceList.add(new GridItem("1", "subj1"));
correspondenceList.add(new GridItem("2", "subj2"));
correspondenceList.add(new GridItem("3", "subj3"));
correspondenceList.add(new GridItem("4", "subj4"));
correspondenceList.add(new GridItem("5", "subj5"));
correspondenceList.add(new GridItem("6", "subj6"));
correspondenceList.add(new GridItem("7", "subj7"));
correspondenceList.add(new GridItem("8", "subj8"));
correspondenceList.add(new GridItem("9", "subj9"));
correspondenceList.add(new GridItem("10", "subj10"));
correspondenceList.add(new GridItem("11", "subj11"));
MyArrayAdapteradapter = new MyArrayAdapter(this, R.layout.grid_item, correspondenceList);
gridView.setAdapter(adapter);
gridView.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
private void isScrollCompleted() {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
/*** In this way I detect if there's been a scroll which has completed ***/
/*** do the work for load more date! ***/
}
}
}
在适配器中有一个覆盖方法
@Override
public int getCount() {
// TODO Auto-generated method stub
return fields!= null ? fields.size() : 0;
}
所以你需要在滚动时更改getCount()
的值,以获得每10条记录的完整记录。
答案 1 :(得分:0)
这是我如何解决我的问题:
首先设置private int startIndex = 0
和private List<Items> currentItemsList;
然后在onCreate中输入此代码:
getItems(startIndex);
GridView gridViewItems = (GridView) findViewById(R.id.grid_view);
gridViewItems.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//leave this empty
}
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - threshold) {
//load more list items:
startIndex = startIndex +
getItems(startIndex);
}
}
});
然后在getItems()方法中:
private void getItems(int index) {
if (PerCurrentItemsData.getTotalItemsNumber() <= index &&
PerCurrentIemsData.getTotalItemsNumber() != 0) {
//do nothing
//NOTE: where PerCurrentItemsData.getTotalItemsNumber() is a static method in
// PerCurrentItemsData class filled on app creations that return the total number of items
} else {
PerCurrentItemsData.getCurrentItems().addAll(//retreive list of items to show next);
currentItemsList = PerCurrentItemsData.getCurrentItems();
}
if (result.getInbox().getCorrespondences().size() != 0) {
ItemsListArrayAdapter adapter =
new ItemsListArrayAdapter(context, R.layout.grid_item, currentItemsList);
gridView.setAdapter(adapter);
gridView.setSelection(index);
} else if (currentItemsList.size() == 0) {
ItemsListArrayAdapter adapter =
new ItemsListArrayAdapter(context, R.layout.grid_item, new ArrayList<Items>());
gridView.setAdapter(adapter);
} else if (currentItemsList.size() == 0) {
}
}
}
我希望这会有所帮助,它对我有用,如有任何进一步的信息,请问我