如何使用按钮从片段切换标签?

时间:2017-10-08 14:42:40

标签: java android android-tabactivity

我有一个带有多个标签Activity的标签Fragments。 (如艺术家,播放列表,专辑)。我想在我的主页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;
}

enter image description here

2 个答案:

答案 0 :(得分:1)

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

private HomeFragmentActionListener mListener;

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

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

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)); 
.. 

答案 1 :(得分:0)

您需要通过回调通知父活动:

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();
    }
}