Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Recyclerview在循环中加载相同的数据_Java_Android_Api_Pagination_Reddit - Fatal编程技术网

Java Recyclerview在循环中加载相同的数据

Java Recyclerview在循环中加载相同的数据,java,android,api,pagination,reddit,Java,Android,Api,Pagination,Reddit,我面临的问题是,每当recyclerview到达列表底部时,它都会加载相同的数据,而不是下一个Reddit页面。在AsyncTask中,我传递字符串变量“after”,这是使用RedditAPI进行分页所需的。我希望发生的事情是,当页面重新加载时,将字符串“after”添加到url中,并用接下来的页面图像填充图库。但从我实现的情况来看,这似乎只是在循环中加载相同的数据。这里似乎有什么不对,我做错了什么 RedditParser.java public List<DesktopItems&g

我面临的问题是,每当recyclerview到达列表底部时,它都会加载相同的数据,而不是下一个Reddit页面。在AsyncTask中,我传递字符串变量“after”,这是使用RedditAPI进行分页所需的。我希望发生的事情是,当页面重新加载时,将字符串“after”添加到url中,并用接下来的页面图像填充图库。但从我实现的情况来看,这似乎只是在循环中加载相同的数据。这里似乎有什么不对,我做错了什么

RedditParser.java

public List<DesktopItems> fetchItems(String after)
{
    List<DesktopItems> items = new ArrayList<>();
    int count = 0;
    int counter = count + 25;

    try
    {
        String url = Uri.parse("https://www.reddit.com/r/battlestations/hot.json")
                .buildUpon()
                .appendQueryParameter("?count", String.valueOf(counter))
                .appendQueryParameter("&after", after)
                .build()
                .toString();
        String jsonString = getUrlString(url);
        Log.i(TAG, "Received JSON: " + jsonString);
        JSONObject jsonBody = new JSONObject(jsonString);
        parseItems(items, jsonBody);
    } catch (JSONException je)
    {
        Log.e(TAG, "Failed to parse JSON", je);
    } catch (IOException ioe)
    {
        Log.e(TAG, "Failed to fetch items", ioe);
    }
    return items;
}

private void parseItems(List<DesktopItems> items, JSONObject jsonBody)
        throws IOException, JSONException
{
    DesktopItems di1 = new DesktopItems();
    JSONObject dataPost = jsonBody.getJSONObject("data");
    di1.setAfter(dataPost.getString("after"));
    items.add(di1);
    JSONArray childPost = dataPost.getJSONArray("children");
    for (int i = 0; i < childPost.length(); i++)
    {
        JSONObject dataPost2 = childPost.getJSONObject(i).getJSONObject("data");
        DesktopItems di2 = new DesktopItems();
        di2.setThumbnail(dataPost2.getString("thumbnail"));
        di2.setAuthor(dataPost2.getString("author"));
        di2.setPermalink(dataPost2.getString("permalink"));
        di2.setTitle(dataPost2.getString("title"));
        di2.setDomain(dataPost2.getString("domain"));

        JSONObject previewPost = dataPost2.getJSONObject("preview");
        JSONArray imagesPost = previewPost.getJSONArray("images");
        for (int y = 0; y < imagesPost.length(); y++)
        {
            if (!Objects.equals(di2.getDomain(), "self.battlestations"))
            {
                JSONObject sourcePost = imagesPost.getJSONObject(y).getJSONObject("source");
                di2.setUrl(sourcePost.getString("url"));
                items.add(di2);
            }
        }
    }
}
public List fetchItems(后面的字符串)
{
列表项=新建ArrayList();
整数计数=0;
int计数器=计数+25;
尝试
{
字符串url=Uri.parse(“https://www.reddit.com/r/battlestations/hot.json")
.buildon()
.appendQueryParameter(“?计数”,字符串.valueOf(计数器))
.appendQueryParameter(“&after”,after)
.build()
.toString();
字符串jsonString=getUrlString(url);
i(标记“receivedJSON:+jsonString”);
JSONObject jsonBody=新的JSONObject(jsonString);
解析项(items,jsonBody);
}捕获(JSONException je)
{
Log.e(标记“未能解析JSON”,je);
}捕获(ioe异常ioe)
{
Log.e(标记“无法获取项目”,ioe);
}
退货项目;
}
私有void parseItems(列表项,JSONObject jsonBody)
抛出IOException、JSONException
{
DesktopItems di1=新的DesktopItems();
JSONObject dataPost=jsonBody.getJSONObject(“数据”);
setAfter(dataPost.getString(“after”);
项目。添加(di1);
JSONArray childPost=dataPost.getJSONArray(“儿童”);
对于(int i=0;i
DesktopGalleryFragment.java

    public class DesktopGalleryFragment extends Fragment
{
    private RecyclerView mDesktopRecyclerView;
    private DesktopItems mItems = new DesktopItems();
    private List<DesktopItems> mList = new ArrayList<>();
    private boolean loading = true;
    private EndlessRecyclerViewScrollListener scrollListener;
    private int count = 0;
    private String after;

    public static DesktopGalleryFragment newInstance()
    {
        return new DesktopGalleryFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        new FetchItemsTask().execute(after);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_desktop_gallery, container, false);
        mDesktopRecyclerView = (RecyclerView) v
                .findViewById(R.id.fragment_desktop_gallery_recycler_view);

        final GridLayoutManager mLayoutManager;
        mLayoutManager = new GridLayoutManager(getActivity(), 3);
        mDesktopRecyclerView.setLayoutManager(mLayoutManager);
        mDesktopRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState)
            {
                DesktopItems desktopItems = new DesktopItems();
                after = desktopItems.getAfter();
                DesktopItems items = new DesktopItems();
                after = items.getAfter();
                PhotoAdapter adapter = (PhotoAdapter) recyclerView.getAdapter();
                int lastPostion = adapter.getLastBoundPosition();
                GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                int loadBufferPosition = 1;
                if (lastPostion >= adapter.getItemCount() - layoutManager.getSpanCount() - loadBufferPosition)
                {
                    after = desktopItems.getAfter();
                    new FetchItemsTask().execute(after);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy)
            {
                super.onScrolled(recyclerView, dx, dy);
            }
        });


    private void setupAdapter()
    {
        if (isAdded())
        {
            mDesktopRecyclerView.setAdapter(new PhotoAdapter(mList));
        }
    }

    private class PhotoAdapter extends RecyclerView.Adapter<PhotoHolder>
    {
        private List<DesktopItems> mGalleryItems;
        private int lastBoundPosition;

        public int getLastBoundPosition()
        {
            return lastBoundPosition;
        }

        PhotoAdapter(List<DesktopItems> galleryItems)
        {
            mGalleryItems = galleryItems;
        }

        @Override
        public PhotoHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
        {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View view = inflater.inflate(R.layout.gallery_item, viewGroup, false);
            return new PhotoHolder(view);
        }

        @Override
        public void onBindViewHolder(PhotoHolder photoHolder, int position)
        {
            DesktopItems desktopItems = mGalleryItems.get(position);
            photoHolder.bindGalleryItem(desktopItems);
            lastBoundPosition = position;
            Log.i(TAG, "Last bound position is " + Integer.toString(lastBoundPosition));
        }

        @Override
        public int getItemCount()
        {
            return mGalleryItems.size();
        }
    }

    private class PhotoHolder extends RecyclerView.ViewHolder
    {
        private ImageView mImageView;
        private DesktopItems mDesktopItems;

        PhotoHolder(View itemView)
        {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.fragment_desktop_gallery_image);
        }

        void bindGalleryItem(DesktopItems desktopItems)
        {
            mDesktopItems = desktopItems;
            Glide.with(getActivity())
                    .load(desktopItems.getUrl())
                    .into(mImageView);
        }
    }

    private class FetchItemsTask extends AsyncTask<String, Void, List<DesktopItems>>
    {
        @Override
        protected List<DesktopItems> doInBackground(String... params)
        {
            DesktopItems desktopItems = new DesktopItems();
            after = desktopItems.getAfter();
            return new RedditParser().fetchItems(after);
        }

        @Override
        protected void onPostExecute(List<DesktopItems> items)
        {

            if (count > 1)
            {
                mList.addAll(items);
                mDesktopRecyclerView.getAdapter().notifyDataSetChanged();
            } else
            {
                mList = items;
                setupAdapter();
            }
            count++;
        }
    }
}
公共类DesktopGalleryFragment扩展片段
{
私有回收视图mDesktopRecyclerView;
private DesktopItems mItems=新DesktopItems();
private List mList=new ArrayList();
私有布尔加载=真;
私有endlessRecycleViewScrollListener滚动Listener;
私有整数计数=0;
私有字符串之后;
公共静态DesktopGalleryFragment newInstance()
{
返回新的DesktopGalleryFragment();
}
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(真);
新建FetchItemsTask().execute(之后);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图v=充气机。充气(R.layout.fragment\u desktop\u gallery,container,false);
mDesktopRecyclerView=(RecyclerView)v
.findViewById(R.id.fragment\u桌面\u画廊\u回收器\u视图);
最终网格布局经理mLayoutManager;
mLayoutManager=新的GridLayoutManager(getActivity(),3);
mDesktopRecyclerView.setLayoutManager(mLayoutManager);
mDesktopRecyclerView.addOnScrollListener(新的RecyclerView.OnScrollListener()
{
@凌驾
CrollStateChanged上的公共无效(RecyclerView RecyclerView,int newState)
{
DesktopItems DesktopItems=新DesktopItems();
after=desktopItems.getAfter();
DesktopItems=新的DesktopItems();
after=items.getAfter();
PhotoAdapter=(PhotoAdapter)recyclerView.getAdapter();
int lastposition=adapter.getLastBoundPosition();
GridLayoutManager布局管理器=(GridLayoutManager)recyclerView.getLayoutManager();
int loadBufferPosition=1;
if(lastposition>=adapter.getItemCount()-layoutManager.getSpanCount()-loadBufferPosition)
{
after=desktopItems.getAfter();
新建FetchItemsTask().execute(之后);
}
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy)
{
super.onScrolled(recyclerView、dx、dy);
}
});
私有void setupAdapter()
{
if(isAdded())
{
mDesktopRecyclerView.setAdapter(新的光电适配器(mList));
}
}
私有类PhotoAdapter扩展了RecyclerView.Adapter
{
私有列表管理项目;
私人职位;
public int getLastBoundPosition()
{
返回最后一个边界位置;
}
PhotoAdapter(列出galleryItems)
{
mGalleryItems=galleryItems;
}
@凌驾
公共照片持有者onCreateViewHolder(视图组视图组,int-viewType)
{
LayoutInflater充气器=LayoutInflater.from(getActivity());
视图=充气机。充气(R.layout.gallery_项,视图组,false);
返回新的照片夹(视图);
}
@凌驾
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_desktop_gallery_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>