在android中的所有子Activity中显示来自一个Parent活动的View

时间:2016-01-18 13:42:47

标签: android inheritance android-recyclerview

我有一个BaseActivity,它扩展了Activity,里面有RecyclerView。 我想在所有活动中继承这个RecyclerView。因此,对于我正在使用的每个活动,它都扩展了BaseActivity。

但如果我在Child Activity上使用 setContentView 使用不同的XML布局,则BaseActivity中的RecyclerView不可见。

BaseActivity.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());
    }

ChildActivity1.java

public class ChildActivity1 extends BaseActivity {

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



    }

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

2 个答案:

答案 0 :(得分:2)

您没有必要在BaseActivity.java中调用thread no:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 Runnable:Ravindra:1 。 创建一个单独的函数来初始化BaseActivity.java中的setContentView()(例如RecyclerView)。在ChildActivity1中调用create()setContentView()。 然后在activity_get_mobile_number.xml

中包含activity_base.xml
create()

答案 1 :(得分:1)

通过调用setContentView中的ChildActivity,您可以有效地将BaseActivity布局替换为新布局。要在RecyclerView中提供ChildActivity,您还需要将其activity_get_mobile_number.xml包含在其中。

有关includemerge标记,请参阅this page

我们的想法是拥有一个单独的通用布局文件,您可以将其包含在BaseActivityChildActivity布局中。然后,将所有代码移到方法中,并在子类中调用它:

public class BaseActivity extends Activity {

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

    protected void setupRecyclerView(){
        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);
        super.setupRecyclerView(),
    }

}