java gridlayout 行高,RecyclerView GridLayoutManager和動態的行高

初始化:

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 postList

int postline2h = 0;

int postline1h = 0;

try{

Post ps;

for(int i = 0; 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 {

private ArrayList posts;

public PostsAdapter(ArrayList 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是要添加的意見到佈局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值