2014-12-21 181 views
1

我有一个关于RecyclerView的问题。我试图学习RecyclerView,但我坚持一个问题。我正在动态创建所有视图。所以,假设我有一个ContentLinearLayout。我的问题始于“refreshContent”方法。我打了几次电话。但是,即使在第一次调用时,RecyclerView的内部视图仍保留在它们的背景图像大小中(我猜在某处有一些隐式的WRAP_CONTENT)。我无法找到将按钮的高度和宽度均衡到RecyclerView高度的方法。我试着迭代RecyclerView并为每个孩子设置布局参数,但它没有奏效。此外,我试图在“onBindViewHolder”内设置布局参数,但这也不起作用。RecyclerView内视图的正确大小

任何帮助将不胜感激。

public class ContentLinearLayout extends LinearLayout { 

    private RecyclerView rvCon; 
    private LinearLayout llCon; 

    public ContentLinearLayout(Context context) { 
     super(context); 
     this.init(context); 
    } 

    public ContentLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.init(context); 
    } 

    private void init(Context c){ 
     this.setOrientation(LinearLayout.VERTICAL); 
     this.setBaselineAligned(false); 

     rvCon = new RecyclerView(c); 
     LinearLayout.LayoutParams recyclerPrms = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f); 
     recyclerPrms.setMargins(0, 0, 0, 0); 
     rvCon.setLayoutParams(recyclerPrms); 
     LinearLayoutManager manager = new LinearLayoutManager(c, LinearLayoutManager.HORIZONTAL, false); 
     rvCon.setLayoutManager(manager); 
     addView(rvCon); 

     rvCon.setPadding(0, 0, 0, 0); 

     LinearLayout llCon = new LinearLayout(c); 
     llCon.setOrientation(LinearLayout.HORIZONTAL); 
     llCon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 3f)); 
     llCon.setBaselineAligned(false); 
     addView(container); 

     ... 
    } 

    private void refreshContent(Context ctx, Content content){ 
     rvCon.setAdapter(new ContentAdapter(ctx, content)); 
    } 
} 

public class ContentAdapter extends RecyclerView.Adapter<ImageButtonHolder>{ 

    private Context mContext; 
    private Content mContent; 

    public ContentAdapter(Context c, Content content){ 
     mContext = c; 
     mContent = content 
    } 

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

    @Override 
    public void onBindViewHolder(ImageButtonHolder buttonHolder, int pos) { 

    } 

    @Override 
    public ImageButtonHolder onCreateViewHolder(ViewGroup arg0, int arg1) { 
     ImageButton b = new ImageButton(mContext); 
     b.setBackgroundColor(Color.RED); 
     ImageButtonHolder holder = new ImageButtonHolder(b); 
     return holder; 
    } 
} 

public class ImageButtonHolder extends RecyclerView.ViewHolder { 

    public final ImageButton imageButton; 

    public ImageButtonHolder(ImageButton button) { 
     super(button); 
     imageButton = button; 
    } 

} 
+0

......在您的ContentAdapter中是非常错误的。 每次RV需要一个新的视图,你增加'num',并且当调用getItemCount()时,你将返回'num.get()'。这是两件无关的事情。 'onCreateViewHolder(ViewGroup parent,int type)',这里的第二个参数是视图的类型。创建一个新的视图不应该改变适配器的大小。你可以检查[这里](https://developer.android.com/training/material/lists-cards.html)例如房车的使用情况。 – yigit

+0

好吧,我没有想到它会在第一时间有关。所以,我编辑了。 –

回答

4

我有同样的问题:View in RecyclerView does not match parent width

尝试根据设备的宽度设置宽度:

适配器代码:

private Activity activity; 
private int imageWidth = 0; 

public RecommendedVendorsAdapter(Activity activity) { 
    recommendedVendors = VendorListingManager.getInstance().getRecommended(); 
    this.activity = activity; 
    imageWidth = activity.getWindow().getDecorView().getWidth(); 
} 

@Override 
public RecommendedVendorsViewHolder onCreateViewHolder(ViewGroup vg, int viewType) { 
    View v = LayoutInflater.from(vg.getContext()).inflate(R.layout.item_recommended_list, vg, false); 
    RecyclerView.LayoutParams params= (RecyclerView.LayoutParams)v.getLayoutParams(); 
    params.width=imageWidth; 
    v.setLayoutParams(params); 

    RecommendedVendorsViewHolder vh = new RecommendedVendorsViewHolder(v); 
    return vh; 
} 

我希望它可以帮助你。

+1

也可以使用vg.getWidth()(如果它的匹配父项)。 – sagits

相关问题