代码之家  ›  专栏  ›  技术社区  ›  All Might

Android Studio如何将此活动放入extends片段?

  •  -2
  • All Might  · 技术社区  · 7 年前

    我在遇到错误 setContentView , findViewById getSupportFragmentManager() . 此代码在扩展时工作正常 AppCompatActivity 但当我试图将其更改为一个片段时,就会出现错误。我需要将此活动代码转换为片段,以便在中使用它 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 回复  |  直到 7 年前
        1
  •  0
  •   Ben P.    7 年前

    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() 相反

        2
  •  0
  •   Siva agarwal    7 年前

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