2017-08-24 48 views
0

我正在使用parse-server來開發使用RecyclerView顯示圖像項目的應用程序。當我每次上下滾動時,Android RecyclerView顯示項目發生變化

但問題是,每當我上下滾動時,視圖中顯示的項目都會發生變化。

我想知道我的代碼有什麼問題。

如果你看到下面的圖片,你可以找到物品正在改變它們的位置。

我試圖讓持有人圖像變成空,然後再次打電話給持有人。但它不起作用。我想這again.but我無法找到的情況

enter image description here enter image description here

原因RecyclerParseAdapter.java

public class MyTimelineAdapter extends RecyclerParseAdapter { 

private interface OnQueryLoadListener<ParseObject> { 

    public void onLoading(); 
    public void onLoaded(List<ParseObject> objects, Exception e); 

} 

private static ParseQueryAdapter.QueryFactory<ParseObject> queryFactory; 
private static List<OnQueryLoadListener<ParseObject>> onQueryLoadListeners; 
private static List<List<ParseObject>> objectPages; 
private static ArrayList<ParseObject> items; 
private static int currentPage; 


private static RequestManager requestManager; 


public MyTimelineAdapter(Context context, RequestManager requestManager) { 

    super(context); 
    this.requestManager = requestManager; 

    this.onQueryLoadListeners = new ArrayList<>(); 
    this.currentPage = 0; 
    this.objectPages = new ArrayList<>(); 
    this.items = new ArrayList<>(); 



    this.queryFactory = new ParseQueryAdapter.QueryFactory<ParseObject>() { 
     @Override 
     public ParseQuery<ParseObject> create() { 

      ParseQuery<ParseObject> query = ParseQuery.getQuery("ImageClassName"); 
      query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK); 
      query.whereEqualTo("status", true); 
      query.orderByDescending("createdAt"); 

      return query; 
     } 
    }; 

    loadObjects(currentPage); 

} 



@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View timelineView; 

    timelineView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_timeline_item2, parent, false); 

    TimelineItemViewHolder timelineItemViewHolder = new TimelineItemViewHolder(timelineView); 

    return timelineItemViewHolder; 

} 



@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    final ParseObject timelineOb = getItem(position); 

    FunctionPost functionPost = new FunctionPost(context); 
    functionPost.TimelineArtistPostAdapterBuilder(timelineOb, holder, requestManager); 

    //기능 추가 

} 



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

@Override 
public ParseObject getItem(int position) { 
    return items.get(position); 
} 

@Override 
public void loadObjects(final int page) { 

    final ParseQuery<ParseObject> query = this.queryFactory.create(); 

    if (this.objectsPerPage > 0 && this.paginationEnabled) { 

     this.setPageOnQuery(page, query); 

    } 

    this.notifyOnLoadingListeners(); 


    if (page >= objectPages.size()) { 

     objectPages.add(page, new ArrayList<ParseObject>()); 

    } 


    query.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> foundObjects, ParseException e) { 

      if ((e != null) && ((e.getCode() == ParseException.CONNECTION_FAILED) || (e.getCode() != ParseException.CACHE_MISS))) { 

       hasNextPage = true; 

      } else if (foundObjects != null) { 

       // Only advance the page, this prevents second call back from CACHE_THEN_NETWORK to 
       // reset the page. 
       if (page >= currentPage) { 
        currentPage = page; 

        // since we set limit == objectsPerPage + 1 
        hasNextPage = (foundObjects.size() > objectsPerPage); 
       } 

       if (paginationEnabled && foundObjects.size() > objectsPerPage) { 
        // Remove the last object, fetched in order to tell us whether there was a "next page" 
        foundObjects.remove(objectsPerPage); 
       } 

       List<ParseObject> currentPage = objectPages.get(page); 
       currentPage.clear(); 
       currentPage.addAll(foundObjects); 
       syncObjectsWithPages(items, objectPages); 

       // executes on the UI thread 
       notifyDataSetChanged(); 

      } 

      notifyOnLoadedListeners(foundObjects, e); 


     } 
    }); 
} 


public void loadNextPage() { 

    if (items.size() == 0) { 

     loadObjects(0); 

    } else { 

     loadObjects(currentPage + 1); 

    } 
} 



public void syncObjectsWithPages(ArrayList<ParseObject> items, List<List<ParseObject>> objectPages) { 

    items.clear(); 

    for (List<ParseObject> pageOfObjects : objectPages) { 

     items.addAll(pageOfObjects); 

    } 
} 

protected void setPageOnQuery(int page, ParseQuery<ParseObject> query) { 

    query.setLimit(this.objectsPerPage + 1); 

    query.setSkip(page * this.objectsPerPage); 

} 

public void addOnQueryLoadListener(OnQueryLoadListener<ParseObject> listener) { 

    this.onQueryLoadListeners.add(listener); 

} 

public void removeOnQueryLoadListener(OnQueryLoadListener<ParseObject> listener) { 

    this.onQueryLoadListeners.remove(listener); 

} 

public void notifyOnLoadingListeners() { 

    for (OnQueryLoadListener<ParseObject> listener : this.onQueryLoadListeners) { 

     listener.onLoading(); 

    } 

} 

public void notifyOnLoadedListeners(List<ParseObject> objects, Exception e) { 

    for (OnQueryLoadListener<ParseObject> listener : this.onQueryLoadListeners) { 

     listener.onLoaded(objects, e); 

    } 

} 


} 

回答

0

我做的項目的位置號碼,當我打電話的項目改變找到問題

我在適配器中添加overide方法然後它工作查找。

@Override 
public int getItemViewType(int position) { 
    return position; 
} 

我不知道爲什麼現在發生。任何人都可以幫助我瞭解問題的原因?

0

我有一個類似的問題,前一天看到this post。 onBindViewHolder需要知道如何在被調用時顯示該行。根據getItemViewType的需要,我返回了兩種不同的視圖類型,在onCreateViewHolder中有條件地誇大了視圖類型,然後我可以根據需要在ViewHolder上設置數據。

+0

謝謝〜@布萊恩真的很有幫助.. – ssamkyu

相關問題