2016-11-17 71 views
-2

我關注此文章http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/以實現滑動視圖,並且它很好用。所以現在我想在Tab1上添加按鈕,點擊時必須向Tab2發送數據。 我知道有使用接口的問題,但老實說,我不知道如何將它添加到這些代碼和工作提供完全新的android。 到目前爲止,我已經盡力在這些鏈接Communication between SlidingTabLayout tabs,How to pass data from one swipe tab to another?How can I communicate/pass data through different Fragments with Swipe Tab?但我失敗。 任何一個幫助,請讓它在每一個階段的工作,我從TAB1的意思 - > Mainactivity-> TAB2滑動視圖之間的通信選項卡

非常感謝

這裏是代碼後,我編輯的問題

片段1

import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    public class TabFragment1 extends Fragment implements AdapterView.OnItemSelectedListener{ 
Button send; 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View v= inflater.inflate(R.layout.tab_fragment_1, container, false); 
    send=(Button)v.findViewById(R.id.send); 
     send.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
    // value to be sent to the TabFragment2 on button click 
      String value=" My Data"; 

       } 
      }); 

    return v; 
     } 

    } 

Fragment2

import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    public class TabFragment2 extends Fragment { 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.tab_fragment_2, container, false); 
//Display value from TabFragment1 
    textview.setText(value); 
    } 
} 

接口類

public Interface FragmentCommunication 
    { 
    public void printMessage(String message); 

    } 

MainActivity

import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends AppCompatActivity implements FragmentCommunication{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final PagerAdapter adapter = new PagerAdapter 
      (getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 
    } 
      public void printMessage(String message) 
     { 
    Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();  
      } 


    } 

適配器類別

import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 

    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
} 

@Override 
public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      TabFragment1 tab1 = new TabFragment1(); 
      return tab1; 
     case 1: 
      TabFragment2 tab2 = new TabFragment2(); 
      return tab2; 
     default: 
      return null; 
    } 
} 

@Override 
public int getCount() { 
    return mNumOfTabs; 
} 
} 
+0

無需任何代碼,你可能會只-1,而不是回答... – snachmsm

+0

可以使用setArguments功能捆綁數據傳遞給片段 – SaravInfern

+0

snachmsm和SaravInfern感謝迴應我對這個問題作出了更新 – f2k

回答

0

所以張貼更多的代碼,我回答後:

實現一些FragmentCommunication爲您的Fragment S:

//public class TabFragment1 extends Fragment implements FragmentCommunication{ 
public class TabFragment2 extends Fragment implements FragmentCommunication{ 

    //public static final String TAG="TabFragment1"; 
    public static final String TAG="TabFragment2"; 

    ... 

    @Override 
    public void onActivityCreated (Bundle savedInstanceState){ 
     super.onActivityCreated(savedInstanceState); 
     if(getActivity() instanceOf MainActivity) 
      ((MainActivity) getActivity()).registerFragmentCommunication(
       TAG, this); 
    } 

    @Override 
    public void printMessage(String message){ 
     Toast.makeText(getActivity(), TAG+" says: "+message, 
      Toast.LENGTH_SHORT).show(); 
    } 

} 

注意到這一行裏面onActivityCreated:實施FragmentCommunication

((MainActivity) getActivity()).registerFragmentCommunication(TAG, this); 

this手段,TAG將是Fragment唯一鍵/名稱,整個方法是Activity你的界面註冊。所以現在您必須將registerFragmentCommunication方法添加到您的MainActivity並保持對傳遞的接口的引用,例如在HashMap

HashMap<String, FragmentCommunication> fragmentCommunications = 
    new HashMap<String, FragmentCommunication>(); 
... 
public void registerFragmentCommunication(String key, FragmentCommunication fc){ 
    fragmentCommunications.put(key, fc); 
} 

所以現在你可以訪問兩個片段的每一個FragmentCommunicationMainActivity,例如通過使用方法是這樣的:

public void callFragmentCommunication(String key, String msg){ 
    if(fragmentCommunications.get(key)!=null) 
     fragmentCommunications.get(key).printMessage(); 
} 

callFragmentCommunication(TabFragment1.TAG, "hi!"); 
callFragmentCommunication(TabFragment2.TAG, "hi!"); 

方法是公共的,所以Fragment S可稱之爲單純的喜歡這樣的:

//inside TabFragment1 
if(isAdded() && getActivity()!=null && 
    getActivity() instanceOf MainActivity && 
     !getActivity().isFinishing()) 
    ((MainActivity) getActivity()).callFragmentCommunication(
     TabFragment2.TAG, "hi!"); 

這將顯示Toast文字: 「TabFragment2說:嗨!」,這是CA在TabFragment1裏面。現在你可以通過另一類型的數據以及:)

有更多的方法,如通過通信FragmentStatePagerAdapterlike herepassing Bundle arguments其中片段被初始化,附加的反饋接口(例如返回真/假時傳遞消息)等。以上只是非常簡化的方式,祝你好運! :)

+0

非常感謝拯救我的生命@snachmsm – f2k

+0

upvote和/或標記爲回答我的文章,如果它是有用的:)祝你好運!編輯:我編輯了我的答案與適當的地圖init'HashMap ()' – snachmsm