简体   繁体   中英

How to switch from one tab to another tab by clicking on button in Android?

I trying to write a code in Android, to switch from one tab to another tab by click on a button.

I know to by clicking on tab we can switch from one tab to another but can it be possible to switch from one tab to another tab by clicking on one button.

I tried the following tutorial.

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

In this tutorial i have edited the MovieFragment.java file in following manner.

MovieFragment.java

package info.androidhive.tabsswipe;

import info.androidhive.tabsswipe.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MoviesFragment extends Fragment {

ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{

    View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

    Button btn = (Button) rootView.findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {

            viewPager.setCurrentItem(0);

        }
    }); 


    return rootView;
}
}

XML Code

   <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" 
      android:background="#17df0d">

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Design Movies Screen"
    android:textSize="20dp"
    android:layout_centerInParent="true"/>

<Button 
    android:id="@+id/btn"
    android:layout_width="100dp"
    android:layout_height="80dp"
    android:text="Click"
    />

I have created a button in the xml layout and trying switch to tab -1 but i am getting Null Pointer Exception.

Error :

05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
05-20 09:07:10.995: E/AndroidRuntime(1117):     at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:4240)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at  android.view.View$PerformClick.run(View.java:17721)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:730)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:137)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:525) 
05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)

Please suggest me some solution or some good technique to do it.

Have a look at Creating Swipe Views with Tabs and at the documentation for ViewPager.setCurrentItem() : you can put a call to this method on the event listener of your Button. Please share some code of yours to get a more targeted solution to your problem.

You need to amend your code like this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_event, container, false);

        btn = (Button) rootView.findViewById(R.id.btn);
        btnWeb.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View v) {
               Intent newFrag = new Intent(getActivity(), NewFragment.class);
               startActivity(newFrag);
            });
        }
    });

    return rootView;

I would also suggest you use ViewPager to do that. A great tutorial for this can be found here :

Hope this helps you :)

    Button registerButton = (Button) rootView
            .findViewById(R.id.register_btn);
    registerButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            viewPager = (ViewPager) getActivity().findViewById(
                    R.id.viewpager);
            viewPager.setCurrentItem(1);

            // set tab postion 1  to move next tab 

        }

If you are in a fragment page(lets say first page) which has a button to switch to another fragment page(lets say at position x) in view pager then you can use :

ViewPager viewPager = getParentFragment().getView().findViewById(R.id.viewpager_fragment);
        viewPager.setCurrentItem(position);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM