2017-05-23 98 views
-3

請給我很好的解決方案我需要你的幫助!如何使用Android中的Activity將ArrayList從片段傳遞到片段?

enter image description here

我想用接口類

從乙片段到活動和活動從到C片段傳遞參數。

但服務asynctask我不知道C在等B嗎?

請解釋一下這個碎片做什麼?

編輯:

public class FeedDetailActivity extends AppCompatActivity implements TabFragment_DetailFeed.ArrayPasser { 

private Toolbar toolbar; 
private TabLayout tabLayout; 
private ViewPager viewPager; 

String nameSurname; 
int feedId; 
public ArrayList<String> pathimg; 

Bundle bundle; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_feeddetail); 

    // toolbar = (Toolbar) findViewById(R.id.toolbar); 
    // setSupportActionBar(toolbar); 

    // getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    nameSurname = getIntent().getStringExtra("nameSurname"); 

    Intent i = getIntent(); 
    feedId = i.getIntExtra("feedId",0); // 20 for default value 
    // feedId = getIntent().getStringExtra("feedId"); // Hata burda 

    viewPager = (ViewPager) findViewById(R.id.pager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.setupWithViewPager(viewPager); 

    /* TabFragment_DetailComment EditTextinden otomatik klavye açtırmasını kapatmasını sağlıyor. */ 
    getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
    ); 




} 
public String getNameSurname() { 
    return nameSurname; 
} 


public int getFeedId() { 
    return feedId; 
} 

private void setupViewPager(ViewPager viewPager) { 
    FeedDetailActivity.ViewPagerAdapter adapter = new FeedDetailActivity.ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new TabFragment_DetailFeed(), "DETAIL"); 
    adapter.addFragment(new TabFragment_DetailComment(), "COMMENT"); 
    adapter.addFragment(new TabFragment_DetailImage(), "IMAGE"); 
    adapter.addFragment(new TabFragment_DetailSurvey(), "SURVEY"); 
    viewPager.setAdapter(adapter); 
} 


@Override 
public void sendArray(ArrayList<String> strings) { 
    TabFragment_DetailImage frag = (TabFragment_DetailImage) 
      getSupportFragmentManager().findFragmentById(); 
    frag.method(strings); 
} 

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 

}

回答

1

你應該嘗試使用EventBus。有了它,您可以在碎片中註冊接收器並從代碼的任何部分發布事件。

這裏是你的片段一個小例子:)這樣

@Override 
public void onResume() { 
    super.onResume(); 
    EventBus.getDefault().register(this); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    EventBus.getDefault().unregister(this); 
} 

C語言程序段覆蓋的onResume(),並在onPause(也訂閱您的活動(你需要創建類爲它)

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onResult(OnResultEvent event) { 
    //do stuff in event 
} 

然後在乙片段做這樣

public void doStuff(){ 
    //do stuff here 
    EventBus.getDefault().post(new OnResultEvent(result)); 
} 
1

您的活動實現你在這種情況下接口(ArrayPasser),這是在FragmentA

public class YourActivity implements FragmentA.ArrayPasser{ 
    @Override 
    public void sendArray(ArrayList<String> strings){ 
     // Get instance of Fragment B using FragmentManager 
     FraB frag = (FragB) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_b); 
     frag.someMethod(strings); //passing arraylist to Fragment B from 
             Activity 
    } 
} 


// Fragment A defines an Interface, and calls the method when needed 
public class FragA extends Fragment{ 

    ArrayPasser mCallback; //interface reference 

    public interface ArrayPasser{ 
     public void sendArray(ArrayList<String> strings); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (ArrayPasser) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
       + " must implement ArrayPasser"); 
     } 
    } 

    public void someMethod(ArrayList<String>strings){ 
     mCallback.sendArray(strings); //passing array list to fragment 
    } 

    @Override 
    public void onDetach() { 
     mCallback = null; // => avoid leaking 
     super.onDetach(); 
    } 
} 

// Fragment B has a public method to do something with the Array 
public class FragB extends Fragment{ 

    public void method(ArrayList<String> strings){ 
     // Here you have it 
    } 
} 
+0

定義感謝您迴應,但我不能這樣做。流程圖應該Frag A - > Activity - > Frag B。你能再幫我一次嗎? – TeyteyLan

+0

流程圖在我的回答中也是一樣的 –

+0

再次仔細檢查流程 –

相關問題