0

当用户滚动到页面底部时,我正在尝试将项目添加到我的列表视图中。我正在尝试将项目添加到 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;
}
4

3 回答 3

0

notify() 命令容易混淆。它必须从主线程运行。所以要么你在那里调用它,要么你做一些肮脏的黑客攻击并从 AsyncTask 调用它:

runOnUiThread(new Runnable() {
public void run() {
    adapter.notify();
}

});

于 2013-08-14T09:17:25.743 回答
0

我不知道为什么这不起作用,如果你想调试它,这不是答案。但是,如果您需要此类事情的生产就绪解决方案,您可以使用https://github.com/commonsguy/cwac-endless

于 2013-08-14T09:05:14.300 回答
0
@Override
protected Void doInBackground(Void... params) {
    adapter.clear()//you using this one
    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;
}
于 2013-08-14T09:26:45.263 回答