代码之家  ›  专栏  ›  技术社区  ›  julian-l

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

  •  0
  • julian-l  · 技术社区  · 10 年前

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

    我只想在ColorSwipeRefreshLayout中包含ColorRecyclerView,最后在我的Fragment中包括ColorSwapeRefresh Layout。 但每次我想在片段类中调用ColorRecyclerView时,对象上都会出现NullPointerException,不明白为什么。。。

    代码如下:

    颜色回收视图.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" />
    

    彩色滑动刷新布局.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"/>
    


    颜色片段.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;
    }
    

    颜色碎片.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中添加了膨胀,它可以工作:)

    1 回复  |  直到 10 年前
        1
  •  0
  •   David Medenjak    10 年前

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

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

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

    找不到任何回收器视图。

    一种选择是在您的 初始化视图 方法或稍后查找引用。

    你的初衷可能是 onFinishInflate .