Android,从特定的TabBar项目打开活动

时间:2019-01-16 17:06:43

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

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

我需要第三个项目来打开一个新活动(案例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>

2 个答案:

答案 0 :(得分: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) {}
});

答案 1 :(得分:1)

检查所需的标签项(如果我正确,我假设您想在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更改为要打开的类!