我有一个BaseActivity,它扩展了包含RecyclerView的活动。
我想在所有活动中继承这个recyclerview。因此,对于im使用的每个活动,它都扩展了baseactivity。
但如果在子活动上使用setContentView使用不同的XML布局,则baseActivity中的recyclerView不可见。
基本活动.java

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);


        baseRecyclerModel itemsData[] = { new baseRecyclerModel("Help",R.drawable.help),
                new baseRecyclerModel("Delete",R.drawable.delete),
                new baseRecyclerModel("Cloud",R.drawable.cloud)};


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        baseRecyclerAdapter mAdapter = new   baseRecyclerAdapter(itemsData);

        recyclerView.setAdapter(mAdapter);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

儿童活动1.java
public class ChildActivity1 extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_mobile_number);



    }

可能的解决方法或解决方案是什么?

最佳答案

您不必在baseactivity.java中调用setContentView()
在baseactivity.java中创建一个用于初始化RecyclerView的单独函数(例如create())。在childactivity1中,调用setContentView()create()
然后将activity_base.xml包含在activity_get_mobile_number.xml中

public class BaseActivity extends Activity {

    @Override
    protected void create() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        baseRecyclerModel itemsData[] = { new baseRecyclerModel("Help",R.drawable.help),
            new baseRecyclerModel("Delete",R.drawable.delete),
            new baseRecyclerModel("Cloud",R.drawable.cloud)};


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        baseRecyclerAdapter mAdapter = new   baseRecyclerAdapter(itemsData);

        recyclerView.setAdapter(mAdapter);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}

public class ChildActivity1 extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_mobile_number);
        create();
    }
}

关于android - 在Android的所有子 Activity 中显示来自一个父 Activity 的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34856210/

10-09 15:48