2016-03-28 9 views
0

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:あなたは上記と同じそれを保つが、ちょうどパラメータthistrueを使用するようにインフレート方法を変更した場合 、レイアウト幅があまりにも小さいです。それは親の幅に収まらない:

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

試みを3:

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

:私は同じコードを試してみましたが、parentfalseで、それが再び完全に空です

試行4: 私がsaを試したとき私のコードが、それが表示されますが、再度、幅と一致しなかった、それはまたのLinearLayout・スーパークラスに付ける行わ:

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

試み5:私は同じコードを試してみました が、parentとそして、trueは、私はこのエラーを得た

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) 

他に何を試すことができますか?最初の例のように正しく動作させるにはどうしたらいいのですが、ウィジェットを使うのですか?

答えて

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. 
} 
関連する問題