当用户滚动到页面底部时,我正在尝试将项目添加到我的列表视图中。我正在尝试将项目添加到 AsyncTask 中的数组,然后在现有适配器上使用 notifyDataSetChanged 来显示新项目,但它似乎不起作用。但是,如果我在添加项目后创建一个新适配器,它确实有效,但这会导致跳转到列表顶部,并且也可能是不好的做法?任何想法我做错了什么。这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab1, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View footerView = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer,
null, false);
this.getListView().addFooterView(footerView);
items.add(new HomeListItem(1, getResources().getDrawable(R.drawable.test), "Item Description Here",
testImages));
items.add(new HomeListItem(2, getResources().getDrawable(R.drawable.test), "Item Description Here",
testImages));
items.add(new HomeListItem(3, getResources().getDrawable(R.drawable.test), "Item Description Here",
testImages));
items.add(new HomeListItem(4, getResources().getDrawable(R.drawable.test), "Item Description Here",
testImages));
items.add(new HomeListItem(5, getResources().getDrawable(R.drawable.test), "Item Description Here",
testImages));
//Populate list view with array objects
adapter = new HomeListItemAdapter(getActivity(), 1, items);
listView = getListView();
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastInScreen = (firstVisibleItem + visibleItemCount);
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
new addItem().execute();
}
}
private class addItem extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
loadingMore = true;
}
@Override
protected void onPostExecute(Void arg3) {
Toast toast = Toast.makeText(getActivity(), "ADD ITEM", Toast.LENGTH_SHORT);
toast.show();
adapter.notifyDataSetChanged();
//int currentPosition = listView.getFirstVisiblePosition();
//setListAdapter(new HomeListItemAdapter(getActivity(), 1, items));
loadingMore = false;
}
@Override
protected Void doInBackground(Void... params) {
for (int i=0; i<3; i++) {
items.add(new HomeListItem((items.size()+i), getResources().getDrawable(R.drawable.logo), "NEW ITEM " + (i),
testImages));
}
return null;
}
}
// 菜单项的自定义列表项类
public class HomeListItemAdapter extends ArrayAdapter<HomeListItem> {
private List<HomeListItem> items;
private Context context;
private int numItems = 0;
private ImageView image1;
private ImageView image2;
private ImageView image3;
static class ViewHolder {
ImageView sourceImage;
TextView itemName;
ImageView itemImage1;
ImageView itemImage2;
ImageView itemImage3;
ImageView itemImage4;
ImageView itemImage5;
}
public HomeListItemAdapter(Context context, int id, List<HomeListItem> items) {
super(context, id, items);
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public HomeListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return items.indexOf(getItem(position));
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
//Set up ViewHolder for item caching purposes
holder = new ViewHolder();
holder.sourceImage = (ImageView) convertView.findViewById(R.id.source_image);
holder.itemName = (TextView) convertView.findViewById(R.id.item_description);
holder.itemImage1 = (ImageView) convertView.findViewById(R.id.item_image);
holder.itemImage2 = (ImageView) convertView.findViewById(R.id.item_image2);
holder.itemImage3 = (ImageView) convertView.findViewById(R.id.item_image3);
holder.itemImage4 = (ImageView) convertView.findViewById(R.id.item_image4);
holder.itemImage5 = (ImageView) convertView.findViewById(R.id.item_image5);
//Set holder tag
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//Set item elements
holder.sourceImage.setImageDrawable(items.get(position).getSourcePic());
holder.itemName.setText(items.get(position).getText());
//Get ArrayList of item images
ArrayList<Bitmap> itemImages = items.get(position).getItemImages();
//Set ImageView bitmaps (Load in AsyncTask)
holder.itemImage1.setImageBitmap(itemImages.get(0));
holder.itemImage2.setImageBitmap(itemImages.get(1));
holder.itemImage3.setImageBitmap(itemImages.get(2));
holder.itemImage4.setImageBitmap(itemImages.get(3));
holder.itemImage5.setImageBitmap(itemImages.get(4));
return convertView;
}