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 scrollToPositionWithOffset not working
- 3. GridLayoutManager中的不同(動態)項目高度
- 4. 如何設置RecyclerView GridLayoutManager的最大行
- 5. RecyclerView - GridLayoutManager - 在兩個方向上滾動
- 6. 動態設置RecyclerView高度
- 7. RecyclerView GridLayoutManager項目match_parent寬度
- 8. Android - 設置RecyclerView GridLayoutManager列寬
- 9. 帶GridLayoutManager的RecyclerView左對齊視圖
- 10. 編程滾動與GridLayoutManager
- 11. GridLayoutManager設置列和行計數
- 12. 如何使用GridLayoutManager刪除RecyclerView中的行之間的間距
- 13. RecyclerView的GridLayoutManager - 行在較短的屏幕上重疊
- 14. 帶GridLayoutManager的RecyclerView和底部的反向佈局堆棧
- 15. Android RecyclerView GridLayoutManager onClick現在正在工作
- 16. 如何設置setStackFromEnd(true);在GridLayoutManager for Recyclerview?
- 17. RecyclerView GridLayoutManager隨機移動位置的項目
- 18. 爲什麼RecyclerView項目會隨機移動GridLayoutManager中的位置?
- 19. 動態行高
- 20. GridLayoutManager列高度問題
- 21. GridLayoutManager每行相同的背景
- 22. RecyclerView GridLayoutManager:如何自動檢測跨度計數?
- 23. 安卓:RecyclerView GridLayoutManager - 恢復滾動位置不正常工作
- 24. 如何改變GridLayoutManager列動態
- 25. 使用GridLayoutManager自定義視圖/行在RecyclerView
- 26. Android Recyclerview GridLayoutManager當行給定佈局不包裝其內容
- 27. 爲什麼只有第一行是使用Recyclerview gridlayoutmanager
- 28. 帶動態內容和動態行高的UITableViewCell
- 29. 高級ASP.NET WebGrid - 動態列和行
- 30. 動態更改Textview-和TableviewCell行高度
歡迎堆棧溢出。請通過鏈接瞭解如何提出有效問題。 https://stackoverflow.com/help/how-to-ask – pvpkiran
在這裏發佈您的代碼爲java和xml文件 – mdb
@ mdb_5203發佈 –