Android Studio如何将此活动放在extends Fragment中?

时间:2018-02-07 00:21:35

标签: android android-studio android-fragments fragment navigation-drawer

我在setContentViewfindViewByIdgetSupportFragmentManager()时遇到错误。这个代码在扩展AppCompatActivity时运行正常,但是当我尝试将其更改为片段时,它会给出错误。我需要将Activity的这段代码转换为片段,以便我可以在navigationDrawer中使用它。

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

public class PerdidosActivity extends Fragment {

    FrameLayout simpleFrameLayout;
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perdidos);
        // get the reference of FrameLayout and TabLayout
        simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
        tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
        // Create a new Tab named "First"
        TabLayout.Tab firstTab = tabLayout.newTab();
        firstTab.setText("First"); // set the Text for the first Tab
        firstTab.setIcon(R.drawable.android_icon); // set an icon for the
        // first tab
        tabLayout.addTab(firstTab); // add  the tab at in the TabLayout
        // Create a new Tab named "Second"
        TabLayout.Tab secondTab = tabLayout.newTab();
        secondTab.setText("Second"); // set the Text for the second Tab
        secondTab.setIcon(R.drawable.android_icon); // set an icon for the second tab
        tabLayout.addTab(secondTab); // add  the tab  in the TabLayout



        // perform setOnTabSelectedListener event on TabLayout
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // get the current selected tab's position and replace the fragment accordingly
                Fragment fragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        fragment = new FirstFragment();
                        break;
                    case 1:
                        fragment = new SecondFragment();
                        break;

                }
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.simpleFrameLayout, fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

它将是public class PerdidosActivity extends AppCompatActivity,您的错误将会消失。 解释:您正在扩展一个活动,您将要使用TabLayout,并且您将使用适配器连接到TabLayout的视图将是片段而不是PerdidosActivity。

答案 1 :(得分:0)

<强>的setContentView()

在活动中,您可以通过覆盖onCreate()并调用setContentView()来创建用户界面。在片段中,您可以采用不同的方式。而不是覆盖onCreate(),覆盖onCreateView(),然后写下这样的内容:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.my_layout, container, false);
    ...
    return root;
}

<强> findViewById()

在活动中,您只需在任意位置拨打findViewById(),它就会在您发送到setContentView()的布局中搜索视图。在片段中,您需要View个实例来调用findViewById()

onCreateView()内,您只需拨打root.findViewById(R.id.my_view)即可。在其他方法中,只要您已经从onCreateView()返回了某些内容,就可以撰写getView().findViewById(R.id.my_view)

<强> getSupportFragmentManager()

这很简单:只需使用getFragmentManager()即可。