0
A
回答
1
嘗試&檢查更換您的行
postLine.setLayoutManager(new GridLayoutManager(getActivity(),2));
與
postLine.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
+0
它的工作,thakns –
0
初始化:
postLine.setAdapter(postsAdapter);
postLine.setLayoutManager(new GridLayoutManager(getActivity(),2));
,準備後添加他們recyclerView功能
private void preparePosts(JSONArray posts){
listOfLine1.clear(); //SparseArray
listOfLine2.clear(); //SparseArray
postLine.removeAllViews(); //RecyclerView
postList.clear(); //ListArray<Post> postList
int postline2h = 0;
int postline1h = 0;
try{
Post ps;
for(int i = 0; i<posts.length();i++){
ps = new Post(posts.getJSONObject(i));
if(postline1h>postline2h){
listOfLine2.put(listOfLine2.size(),ps);
postline2h += ps.getHeight();
}else{
postline1h += ps.getHeight();
listOfLine1.put(listOfLine1.size(),ps);
}
}
int i =0;
boolean firstnull,secondnull;
while (i!=listOfLine2.size()-1 || i!=listOfLine1.size()-1){
if(listOfLine1.get(i)!=null){
firstnull = false;
postList.add(listOfLine1.get(i));
listOfLine1.remove(i);
}else firstnull = true;
if(listOfLine2.get(i)!=null){
secondnull = false;
postList.add(listOfLine2.get(i));
listOfLine2.remove(i);
}else secondnull = true;
if(secondnull && firstnull) break;
i++;
}
postsAdapter.notifyDataSetChanged();
}catch (Exception e){
log(e);
}
}}
適配器:
public class PostsAdapter extends RecyclerView.Adapter<PostsHolder> {
private ArrayList<Post> posts;
public PostsAdapter(ArrayList<Post> postslist){
posts = postslist;
}
@Override
public int getItemCount() {
return posts.size();
}
@Override
public PostsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new PostsHolder(new LinearLayout(parent.getContext()));
}
@Override
public void onBindViewHolder(PostsHolder holder, int position) {
holder.setPost(posts.get(position));
}
@Override
public void onViewRecycled(PostsHolder holder) {
super.onViewRecycled(holder);
holder.getPost().die();
}
}
onBindViewHolder post.setPost是要添加的意見到佈局
相關問題
- 1. Android RecyclerView和GridLayoutManager佈局
- 2. RecyclerView與GridLayoutManager內RecyclerView與LinearLayoutManager
- 3. Recyclerview GridLayoutManager scrollToPositionWithOffset not working
- 4. 如何設置RecyclerView GridLayoutManager的最大行
- 5. RecyclerView的GridLayoutManager - 交替1個2列行
- 6. GridLayoutManager中的不同(動態)項目高度
- 7. RecyclerView - GridLayoutManager - 在兩個方向上滾動
- 8. 動態設置RecyclerView高度
- 9. RecyclerView GridLayoutManager項目match_parent寬度
- 10. Android - 設置RecyclerView GridLayoutManager列寬
- 11. 帶GridLayoutManager的RecyclerView - 佈局項目
- 12. 帶GridLayoutManager的RecyclerView左對齊視圖
- 13. 使用gridlayoutmanager查看recyclerview中的位置
- 14. 編程滾動與GridLayoutManager
- 15. RecyclerView的GridLayoutManager - 行在較短的屏幕上重疊
- 16. 如何使用GridLayoutManager刪除RecyclerView中的行之間的間距
- 17. GridLayoutManager設置列和行計數
- 18. 自定義GridLayoutManager提供與WRAP_CONTENT RecyclerView
- 19. Android RecyclerView GridLayoutManager onClick現在正在工作
- 20. 如何設置setStackFromEnd(true);在GridLayoutManager for Recyclerview?
- 21. 帶GridLayoutManager的RecyclerView和底部的反向佈局堆棧
- 22. 爲什麼RecyclerView項目會隨機移動GridLayoutManager中的位置?
- 23. GridLayoutManager每行相同的背景
- 24. GridLayoutManager列高度問題
- 25. RecyclerView GridLayoutManager:如何自動檢測跨度計數?
- 26. 安卓:RecyclerView GridLayoutManager - 恢復滾動位置不正常工作
- 27. 使用GridLayoutManager自定義視圖/行在RecyclerView
- 28. 爲什麼只有第一行是使用Recyclerview gridlayoutmanager
- 29. Android Recyclerview GridLayoutManager當行給定佈局不包裝其內容
- 30. 動態行高
歡迎堆棧溢出。請通過鏈接瞭解如何提出有效問題。 https://stackoverflow.com/help/how-to-ask – pvpkiran
在這裏發佈您的代碼爲java和xml文件 – mdb
@ mdb_5203發佈 –