RecyclerView窗口小部件视图与父宽度

时间:2016-03-28 19:00:33

标签: android android-recyclerview

我正在尝试使用一个小部件来显示和匹配RecyclerView中的父宽度,但它要么不显示,要么与宽度不匹配。

布局似乎工作正常,因为当我直接膨胀它(没有小部件)时,它的布局显示完美,但我需要使用MyWidget类,因为它有很多逻辑和设置视图。

这样可行,但未使用窗口小部件类:

//(In RecyclerViewAdapter)
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View groupView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout, parent, false);
        return new ViewHolder(groupView);
}

尝试1: 当我尝试thisfalse时,它在RecyclerView中根本不会显示。它完全是空的:

//(In RecyclerViewAdapter)
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyWidget myWidget = new MyWidget(parent);
        return new ViewHolder(myWidget);
}


public class MyWidget extends LinearLayout {

    public MyWidget(ViewGroup parent) {
        super(parent.getContext());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, this, false);
    }

    //Important widget methods here.
}

尝试2: 如果保持与上面相同,但只是更改inflate方法以使用参数thistrue,则布局宽度太小。它不适合父宽度:

View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, this, true);

尝试3: 当我尝试使用相同的代码,但使用parentfalse时,它又完全为空:

View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, parent, false);

尝试4: 当我尝试相同的代码,但它也附加到LinearLayout超类,然后它显示,但再次不匹配宽度:

View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, parent, false);
addView(view);

尝试5: 当我尝试使用相同的代码时,parenttrue

View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, parent, true);

我收到此错误(向右滚动):

FATAL EXCEPTION: main                                                                                       
Process: PID: 19221
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference                                                                                            

    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2913)                             
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)                                                                  

    at android.view.View.layout(View.java:16636)                                                                                               

    at android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                                 

    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)                                                                                                 

    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)                                                                                                 

    at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
    at android.view.View.layout(View.java:16636)

我还能尝试什么?我怎样才能让它像第一个例子一样正常工作,但是使用小部件?

1 个答案:

答案 0 :(得分:0)

我终于开始工作了。我只需要在onCreateViewHolder中手动设置布局参数,然后在inflate方法中使用参数thistrue。特别感谢this stack overflow answer我找到了在onCreateViewHolder方法中手动设置布局参数的代码。

//(In RecyclerViewAdapter) 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
        MyWidget myWidget = new MyWidget(parent);

        //Set layout parameters
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, //width
        ViewGroup.LayoutParams.WRAP_CONTENT);//height
        myWidget.setLayoutParams(lp);//override default layout params

        return new ViewHolder(myWidget); 
} 


public class MyWidget extends LinearLayout {

    public MyWidget(ViewGroup parent) {
        super(parent.getContext());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, this, true);
    } 

    //Important widget methods here. 
}