は私が両方でXMLファイルを使用して(RecyclerView.Adapter
を拡張し、RecyclerView
で使用されている)、カスタムアダプタでいEventItemView
と呼ばれるカスタムビューを持っています。インスタンス化カスタムビュープログラムで正しい方法
- XML 私のアダプタで動的にインスタンス
を使用して宣言し、私はこれを信じる:私の問題は私の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);
}
どのように適切に関連するattrs
、defStyleAttr
とdefStyleRes
コンストラクタのパラメータで私のカスタムビューをインスタンス化することができますか?あるいは、まったく違う理由で見方が変わったのでしょうか?
私のアダプタの 'onCreateViewHolder()'で 'View'オブジェクトを取得するために' LayoutInflater.from(parent.getContext())。inflate(R.layout.item_event、parent、false) 'を使用すると、ビューget正しくレイアウトされて表示されています。 – Flawyte
'inflate()'がそれを行います。 – SnapDragon