正确调整RecyclerView中的视图大小

时间:2014-12-21 23:45:48

标签: android android-recyclerview

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

任何帮助将不胜感激。

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;
    }

}

1 个答案:

答案 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;
}

我希望它可以帮到你。