Android 如何在自定义视图中创建自定义视图?

How to create custom view in a custom view in Android?

我创建了两个自定义视图:一个扩展自 SwipeRefreshLayout(名称:ColorSwipeRefreshLayout),另一个扩展自 RecyclerView(名称:ColorRecyclerView)。

我只想在 ColorSwipeRefreshLayout 中包含 ColorRecyclerView,最后在我的 Fragment 中包含 ColorSwipeRefreshLayout。 但是每次我想在我的片段 Class 中调用我的 ColorRecyclerView 时,都会在对象上得到一个 NullPointerException 并且不明白为什么...

有代码:

ColorRecyclerView.java

public class ColorRecyclerView extends RecyclerView {
final static int COLUMN_COUNT = 3;
RecyclerView.LayoutManager layoutManager;
RVColorAdapter adapter;
List<HueColor> colors;
ApiHelper apiHelper;

public ColorRecyclerView(Context context) {
    super(context);
    initializeView(context);
}

public ColorRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView(context);
}

public ColorRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initializeView(context);
}

public void setHelper(ApiHelper apiHelper) {
    this.apiHelper = apiHelper;
}

public void update(List<HueColor> colors) {
    this.colors = colors;
    adapter = new RVColorAdapter(apiHelper,colors);
    setAdapter(adapter);
}

private void initializeView(Context context) {
    layoutManager = new GridLayoutManager(context,COLUMN_COUNT);
    setLayoutManager(layoutManager);
}

colorrecyclerview.xml

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

ColorSwipeRefreshLayout.java

public class ColorSwipeRefreshLayout extends SwipeRefreshLayout {
ColorRecyclerView colorRecyclerView;

public ColorSwipeRefreshLayout(Context context) {
    super(context);
    initializeView(context);
}

public ColorSwipeRefreshLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView(context);
}

private void initializeView(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.swiperefreshlayout_color, this, true);
    colorRecyclerView = (ColorRecyclerView) v.findViewById(R.id.colorRecyclerView);

    setColorSchemeResources(R.color.color_scheme_1_1, R.color.color_scheme_1_2,
            R.color.color_scheme_1_3, R.color.color_scheme_1_4);

}

public ColorRecyclerView getColorRecyclerView() {
    return this.colorRecyclerView;
}

colorswiperefreshlayout.xml

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.color.hue.ui.view.ColorRecyclerView
        android:id="@+id/colorRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


ColorFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // ...code...
    View view = inflater.inflate(R.layout.fragment_color, container, false);
    ButterKnife.bind(this, view);

    mRecyclerView = mSwipeRefreshLayout.getColorRecyclerView();
    mRecyclerView.setHelper(mApiHelper); // Got NullPointerEx here

    // ...code...
    return view;
}

colorfragment.xml

<com.color.hue.ui.view.ColorSwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/colorSwipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

谢谢:)


编辑: 我在 ColorSwipeRefreshLayout 的 initializeView 中添加了 inflation 并且它有效:)

您的 SwipeRefreshLayout 不应在其构造函数中调用 initializeView

由于您是通过布局在您的片段中膨胀它,因此在调用构造函数时尚未添加任何视图。所以

colorRecyclerView = (ColorRecyclerView) findViewById(R.id.colorRecyclerView);`

不会找到任何回收站视图。

一个选项是在您的 initializeView 方法中扩充/创建 recyclerview 或稍后查找参考。

初始化的一个好点可能是 onFinishInflate