RecyclerView GridLayoutManager和动态行高

时间:2017-06-02 15:12:24

标签: java android android-recyclerview

我想在RecyclerView中创建这样的东西:

Expected Output

但是,我得到了这个:

Actual Output

有没有办法让RecyclerView实现?

2 个答案:

答案 0 :(得分:1)

尝试&检查替换你的行

List<WebElement> results = driver.findElements(By.xpath("XPATH HERE"));   
//Get the Total Count
System.out.println(results.size());
if(results.size() ==100)
{
    System.out.println("100 REsults are found as Expected");
}

//To display the http value for each results
for(WebElement each : results)
{
    System.out.println(each.getAttribute("href"));
}

postLine.setLayoutManager(new GridLayoutManager(getActivity(),2));

答案 1 :(得分:0)

init:

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将添加视图到布局