如何在扩展“Fragment”的主类中扩展Activity?

时间:2015-05-24 01:06:58

标签: java android android-fragments switch-statement navigation-drawer

我有一个Android项目,我在其中使用了导航抽屉,在MainActivity中,我有一个开关盒,带有选项和各自的位置,以便在用户点击时打开。但是,在一个名为TelaQUEMSOMOS的类中,有那些参数:

public class TelaQUEMSOMOS extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.telaquemsomos, container, false);
        return rootView;
    }
}

从Fragment而不是Activity扩展。这很糟糕,因为我需要放一个Toast.makeText或findViewById,我不能。我怎么处理呢?

下载我的项目的链接(该项目是葡萄牙语):https://goo.gl/pm5dIC

2 个答案:

答案 0 :(得分:0)

在您的片段中,您可以调用<%= button_tag 'Submit', class: 'btn btn-primary', data: { disable_with: "<i class='fa fa-refresh fa-spin'> </i> Submitting Changes..."} %> 来获取活动的实例。

答案 1 :(得分:0)

请检查以下代码。首先,我们将有一个活动Home.java

final String[] fragments = {
        "com.locate.Establishment", "com.locate.Settings"};
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawer;

public class Home extends Home {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
   .
   .//your other code here. 
   .
   // Here I am going to call the Fragment called Establishment
   FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
   tx.replace(R.id.main, Fragment.instantiate(Home.this, fragments[0]));
   tx.commit();
   setToolbar(0);
 }
}

片段的实现。在这里,我将向用户支持Fragment V4。

public class Establishment extends android.support.v4.app.Fragment{
SupportMapFragment mapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_establishment, null);
    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
//Now will able to all you need here. By using getActivity().
}
}