首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android的自定义视图中创建自定义视图?

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

Stack Overflow用户
提问于 2016-01-17 17:23:12
回答 1查看 52关注 0票数 0

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

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

这是守则:

ColorRecyclerView.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
@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

代码语言:javascript
复制
<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" />

谢谢:)

编辑:我在initializeView of ColorSwipeRefreshLayout中添加了通货膨胀,它起作用了:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-17 17:36:04

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

由于您正在从布局中将其膨胀到片段中,因此在调用构造函数时还没有添加任何视图。所以

代码语言:javascript
复制
colorRecyclerView = (ColorRecyclerView) findViewById(R.id.colorRecyclerView);`

不会发现任何回收商的看法。

一个选项是在您的initializeView方法中膨胀/创建回收视图,或者只是在后面查找引用。

onFinishInflate很可能是你不受重视的一个好点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34841248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档