如何使用按钮从片段切换选项卡?

How to switch tabs from a fragment using a button?

我有一个带有多个选项卡 Fragments 的选项卡 Activity。 (如艺术家、播放列表、专辑)。我想在我的主页 Fragment 添加一个按钮,单击后将导航到其他选项卡。例如:我点击播放列表,它需要我 3 个选项卡到播放列表。我不知道该怎么做。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_home, container, false);
    playlists = (Button) rootView.findViewById(R.id.playlists);
    playlists.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //getActivity().getTabHost().setCurrentTabByTag("TAB2");
            //viewPager.setCurrentItem(2);


        }
    });


    return rootView;
}

您需要通过回调通知家长activity:

public class MainActivity extends AppCompatActivity implements HomeFragment.Listener {

    private ViewPager viewPager;
    ...

    @Override
    public void onPlaylistButtonClicked() {
        // Show playlist tab (assuming playlist is the third tab).
        viewPager.setCurrentItem(2);
    }
}

你的片段:

public class HomeFragment extends Fragment {

    private Listener callback;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        // This makes sure that the container activity has implemented the callback interface.
        try {
            callback = (Listener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement Listener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_home, container, false);
        playlists = (Button) rootView.findViewById(R.id.playlists);
        playlists.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onPlaylistButtonClicked();
            }
        });

        return rootView;
    }

    public interface Listener {
        void onPlaylistButtonClicked();
    }
}

在HomeFragment中声明接口并声明监听变量

private HomeFragmentActionListener mListener;

    public interface HomeFragmentActionListener { 
            void onButtonClick(int pos);
        }

在 HomeFragment 中设置此侦听器变量 class。

public void setListener(
            HomeFragmentActionListener listener) {
        mListener = listener;
    }

和onButton点击通知监听器

playlists.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //getActivity().getTabHost().setCurrentTabByTag("TAB2");
            //viewPager.setCurrentItem(2);
          mListener.onButtonClick(2);
        }
    });

在MainFragment下实现该接口

public class MainFragment extends Fragment {

 private class HomeFragmentListener implements HomeFragment.HomeFragmentActionListener {
public void onButtonClick(int pos) {
 //Change the tabs.
}
}
and set the listener in setupviewpager
private void setupViewPager(ViewPager viewPager) { 
Adapter adapter = new Adapter(getChildFragmentManager()); 
HomeFragment homeFragment = new HomeFragment(); 
homeFragment.setListener( new HomeFragmentListener()); 
adapter.addFragment(homeFragment, this.getString(R.string.Homecap)); 
..