【问题标题】:Android, Open activity from Specific TabBar itemAndroid,从特定的 TabBar 项打开活动
【发布时间】:2019-06-10 19:56:53
【问题描述】:

我有一个包含 5 个项目的标签栏,其中 4 个打开了一个新片段,而其中 1 个应该打开一个新活动,我不知道如何打开这个新活动。

我需要第 3 项来打开一个新活动(案例 2)。

感谢您在此问题上的任何帮助。谢谢。

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    toolbar.setSubtitle("");
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new Frag1();
                break;
            case 1:
                fragment = new Frag2();
                break;
            case 2:
                fragment = new Frag3();

                break;
            case 3:
                fragment = new Frag4();
                break;
            case 4:
                fragment = new Frag5();
                break;

        }
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;
    }
}

public static Bitmap TrimBitmap(Bitmap bmp) {
    int imgHeight = bmp.getHeight();
    int imgWidth  = bmp.getWidth();


    //TRIM WIDTH - LEFT
    int startWidth = 0;
    for(int x = 0; x < imgWidth; x++) {
        if (startWidth == 0) {
            for (int y = 0; y < imgHeight; y++) {
                if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
                    startWidth = x;
                    break;
                }
            }
        } else break;
    }


    //TRIM WIDTH - RIGHT
    int endWidth  = 0;
    for(int x = imgWidth - 1; x >= 0; x--) {
        if (endWidth == 0) {
            for (int y = 0; y < imgHeight; y++) {
                if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
                    endWidth = x;
                    break;
                }
            }
        } else break;
    }

    //TRIM HEIGHT - TOP
    int startHeight = 0;
    for(int y = 0; y < imgHeight; y++) {
        if (startHeight == 0) {
            for (int x = 0; x < imgWidth; x++) {
                if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
                    startHeight = y;
                    break;
                }
            }
        } else break;
    }



    //TRIM HEIGHT - BOTTOM
    int endHeight = 0;
    for(int y = imgHeight - 1; y >= 0; y--) {
        if (endHeight == 0 ) {
            for (int x = 0; x < imgWidth; x++) {
                if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
                    endHeight = y;
                    break;
                }
            }
        } else break;
    }


    return Bitmap.createBitmap(
            bmp,
            startWidth,
            startHeight,
            endWidth - startWidth,
            endHeight - startHeight
    );

}
}

xml 用于标签栏(不确定是否需要):

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@color/colorPrimaryDark"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@android:color/background_light"
        app:tabTextAppearance="@style/TabLayoutStyle"
        app:tabTextColor="@android:color/background_light"

        >

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />

    </android.support.design.widget.TabLayout>

【问题讨论】:

    标签: java android android-fragments android-activity tabbar


    【解决方案1】:

    使用你自己的OnTabSelectedListener

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        void onTabSelected(TabLayout.Tab tab) {
            if (tab.getPosition() == 2) {   // 3rd item selected
                // Open your activity here
            }
        }
    
        void onTabUnselected(TabLayout.Tab tab) {}
    
        void onTabReselected(TabLayout.Tab tab) {}
    });
    

    【讨论】:

      【解决方案2】:

      检查你想要的标签项(如果我是对的,我假设你想在case 2上打开活动,如果不是,请相应地更改代码):

      @Override
      public Fragment getItem(int position) {
          Fragment fragment = null;
          switch (position){
              case 0:
                  fragment = new Frag1();
                  break;
              case 1:
                  fragment = new Frag2();
                  break;
              case 2:
                  //open new activity
                  startActivity(new Intent(CurrentClassName.this, OtherClassName.class));
                  break;
              case 3:
                  fragment = new Frag4();
                  break;
              case 4:
                  fragment = new Frag5();
                  break;
      
          }
          return fragment;
      }
      

      请务必将CurrentClassName 更改为TabLayout 活动的类名,将OtherClassName 更改为您要打开的类!

      【讨论】:

      • 谢谢,我目前每个 Fragment 有 1 个类,我的 MainActivity 如上面的代码所示,我的“OtherActivity”是目的地。我没有任何用于 TabLayout 的类,我应该在 CurrentClassName 中输入什么?
      • 你是哪个标签活动名称。例如名称是 Home,键入Home.this。对于其他任何内容,请输入名称,然后输入 .this
      • @olle 您应该在OtheClassName 部分中输入要打开的活动的名称。就像名称是Activity2.java 一样,输入Activity2.class。在CurrentClassName 部分中键入您正在编码的当前类的名称
      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多