簡體   English   中英

如何在Android的自定義視圖中創建自定義視圖?

[英]How to create custom view in a custom view in Android?

我創建了兩個海關視圖:一個是從SwipeRefreshLayout擴展(名稱:ColorSwipeRefreshLayout),另一個是從RecyclerView擴展(名稱:ColorRecyclerView)。

我只想在ColorSwipeRefreshLayout中包括ColorRecyclerView,最后在我的Fragment中包括ColorSwipeRefreshLayout。 但是每次我想在我的Fragment類中調用我的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中添加了通貨膨脹,並且它起作用:)

您的SwipeRefreshLayout不應在其構造initializeView中調用initializeView

由於您是從布局中將其放大到片段中,因此在調用構造函數時尚未添加任何視圖。 所以

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

將找不到任何回收者視圖。

一種選擇是在您的initializeView方法中膨脹/創建recyclerview,或者稍后再查找參考。

進行初始化的一個好方法可能是onFinishInflate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM