2016-08-08 3 views
0

は私が両方でXMLファイルを使用して(RecyclerView.Adapterを拡張し、RecyclerViewで使用されている)、カスタムアダプタでいEventItemViewと呼ばれるカスタムビューを持っています。インスタンス化カスタムビュープログラムで正しい方法

enter image description here

  1. XML
  2. 私のアダプタで動的にインスタンス

を使用して宣言し、私はこれを信じる:私の問題は私のRecyclerViewで、その外観は奇妙ですが、私のXMLファイルではないということですnew EventItemView(context)を実行して、XMLバージョンのようにレンダリングするためにおそらく必要な追加の引数AttributeSet, int, intを渡さずに、自分のアダプタでビューをインスタンス化する方法によるものです。問題は、私がアダプターでそれらを取得する方法を知らないということです。

EventItemView.java

public class EventItemView extends FrameLayout { 
    public EventItemView(Context context) { 
     this(context, null); 
    } 

    public EventItemView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public EventItemView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr, 0); 
     init(context, attrs, defStyleAttr, 0); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public EventItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(context, attrs, defStyleAttr, defStyleRes); 
    } 

    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     eventItem = new EventItem(); 

     inflate(context, R.layout.view_event_item, this); 
     ButterKnife.bind(this); 

     TypedArray values = context.getTheme().obtainStyledAttributes(attrs, R.styleable.EventItemView, defStyleAttr, defStyleRes); 

     try { 
      setSubtitle(values.getString(R.styleable.EventItemView_subtitle)); 
      ... 
     } 
     finally { 
      values.recycle(); 
     } 
    } 
} 

レイアウト/ view_event_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@drawable/selector_item_event" 
    android:clickable="true" 
    android:gravity="center" 
    android:layout_height="72dp" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:padding="16dp"> 

    ... 

</LinearLayout> 
このビューは、その後1で使用されている

アクティビティ:

はここでカスタムビューの宣言です.xmlファイル:

Adapter.java
<com.app.package.view.EventItemView 
    android:id="@+id/current_event_view" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 

および2):

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    EventItemView itemView = new EventItemView(context); 

    return new ViewHolder(itemView); 
} 

どのように適切に関連するattrsdefStyleAttrdefStyleResコンストラクタのパラメータで私のカスタムビューをインスタンス化することができますか?あるいは、まったく違う理由で見方が変わったのでしょうか?

答えて

0

は、私はちょうど理由を理解していない

public class EventItemView extends RelativeLayout 

public class EventItemView extends FrameLayout 

を変更することで、私の問題を解決しました。

0

measure()に電話し、次にlayout()に電話する必要があります。

インスタンス化されたカスタムビューでは、膨らませる必要があるサイズがわからないため、これらの呼び出しで渡す必要があります。

+0

私のアダプタの 'onCreateViewHolder()'で 'View'オブジェクトを取得するために' LayoutInflater.from(parent.getContext())。inflate(R.layout.item_event、parent、false) 'を使用すると、ビューget正しくレイアウトされて表示されています。 – Flawyte

+0

'inflate()'がそれを行います。 – SnapDragon

関連する問題