2017-10-08 139 views
0

我有一個標籤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

回答

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

homeFragment.setListener(new HomeFragmentListener() );因爲它還沒有在 – Alpoe

+0

之前的其他地方創建過@ @ Alpoe不,它應該可以工作。 –

+0

錯誤 - 無法解析符號HomeFragmentListener。沒關係,似乎沒有任何工作,所以我認爲生病就放棄了。 – Alpoe

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

我看來尋呼機位於我MainFragment不MainActivity下。我把它放在我的主要片段中,如上所示,但它崩潰,在logcat中它說「必須在mainactivity中實現監聽器」。 所以我拿出並運行它,但它仍然墜毀後,我點擊播放列表。它說java.lang.NullPointerException:試圖調用空對象的接口方法'void com.naman14.timber.fragments.HomeFragment $ Listener.onPlaylistButtonClicked()'參考 – Alpoe

相關問題