在notifyDataSetChanged()

时间:2017-03-21 08:47:18

标签: android gridview baseadapter notifydatasetchanged redraw

我试图学习在一个免费的Android课程中为一个作业实现一个endlessscrolllistener,我在Coursera上关注。我遇到的主要问题是当我尝试运行notifyDataSetChanged()时,我的Gridview没有更新;在我的适配器。

这是我尝试实现的Endlessscrollistener。

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
    // The minimum number of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;

    public EndlessScrollListener() {
    }

    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    public EndlessScrollListener(int visibleThreshold, int startPage) {
        this.visibleThreshold = visibleThreshold;
        this.startingPageIndex = startPage;
        this.currentPage = startPage;
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) { this.loading = true; }
        }
        // If it's still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
            currentPage++;
        }

        // If it isn't currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
            loading = onLoadMore(currentPage + 1, totalItemCount);
        }
    }

    // Defines the process for actually loading more data based on page
    // Returns true if more data is being loaded; returns false if there is no more data to load.
    public abstract boolean onLoadMore(int page, int totalItemsCount);

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // Don't take any action on changed
    }
}

在我的主片段中,我通过以下方式设置Gridview的scrolllistener来实现这一点:

gridview.setOnScrollListener(new EndlessScrollListener() {
            @Override
            public boolean onLoadMore(int page, int totalItemsCount) {
                // Triggered only when new data needs to be appended to the list
                // Add whatever code is needed to append new items to your AdapterView
                loadNextDataFromApi(page);
                // or loadNextDataFromApi(totalItemsCount);
                return true; // ONLY if more data is actually being loaded; false otherwise.
            }
        });

应该运行此函数来创建一个Imageloader,用于从API获取数据:

// Append the next page of data into the adapter
// This method probably sends out a network request and appends new data items to your adapter.
    public void loadNextDataFromApi(int page) {
        /* Check if there is an network connection available */
        if(networkAvailable())
        {
            /* Load an images from network */
            gridview.setVisibility(GridView.VISIBLE);
            new ImageLoader(posterWidth, getContext(), page, gridadapter).execute();
        }
        else
        {
            gridview.setVisibility(GridView.GONE);
        }
        System.out.println("pagenumber: " + page);
    }

这个Imageloader使用onPostExecute从我的gridadapter调用addItems()函数。 (此gridadapter从主片段提供给ImageLoader)

@Override
    protected void onPostExecute(ArrayList<String> result)
    {
        if(result!=null)
        {
            if (page == 1)
            {
                ImageAdapter adapter = new ImageAdapter(this.context,result, posterWidth);
                gridview.setAdapter(adapter);
            }
            else
            {
                gridadapter.addItems(result);
            }
        }
    }

在我的适配器中,我有一个名为addItems的函数:

public void addItems(ArrayList<String> addedAPIPaths) {
        apiPaths.addAll(addedAPIPaths);
        notifyDataSetChanged();
    }

我遇到的问题是,当我调用此函数来更新gridview中的项目时,我希望我的gridview重绘自己。我尝试了很多不同的方法,并尝试了notifyDataSetChanged();应该自动为我做这件事,但由于某种原因它对我不起作用。

我在我的适配器中使用此getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iView;
        if(convertView == null)
        {
            iView = new ImageView(context);
        }
        else
        {
            iView= (ImageView) convertView;
        }

        Drawable d = resizeDrawable(ContextCompat.getDrawable(context, R.drawable.noposter));
        Picasso.with(context).load("http://image.tmdb.org/t/p/w200/" + apiPaths.get(position)).
                resize(posterWidth, (int)(posterWidth*)).placeholder(d).into(iView);

        return iView;
    }

有谁看到我做错了什么?我现在在这个问题上工作了好几个小时。我尝试在StackOverflow上搜索,有很多人也有他们的GridView没有重绘的问题,但我还没有找到解决方案来了解我做错了什么。

0 个答案:

没有答案