如何从活动发送的数据片段 [英] How to send data from Activity to Fragment
问题描述
我知道有这个位置很多话题。我也看过文件很多次,但我找不到从活动数据传递给片段的最佳方式。
我希望能够展现我的检索活动使用的刷卡次数与标签。我不得不2数据传递到片段:currentLocation,它是当前用户位置和结果,它是一个对象列表
我已经省略了我的code某些部分,以使其更容易理解。
SearchableActivity.java
公共类SearchableActivity扩展ActionBarActivity实现TabListener {
名单< PlaceModel>结果=新的ArrayList< PlaceModel>();
私人搜索查看mSearchView;
私人字符串currentLocation;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_searchable);
最后的动作条动作条= getSupportActionBar();
actionBar.setHomeButtonEnabled(真正的);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAppSectionsPagerAdapter =新AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置);
}
});
actionBar.addTab(actionBar.newTab()的setText(目录)setTabListener(本)。);
actionBar.addTab(actionBar.newTab()的setText(地图)setTabListener(本)。);
//获取currentLocation这里
handleIntent(getIntent());
}
@覆盖
保护无效onNewIntent(意向意图){
handleIntent(意向);
}
私人无效handleIntent(意向意图){
如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
最终的查询字符串= intent.getStringExtra(SearchManager.QUERY);
//这里得到结果
}
}
@覆盖
公共无效onTabReselected(标签为arg0,FragmentTransaction ARG1){
// TODO自动生成方法存根
}
@覆盖
公共无效onTabSelected(TAB键,FragmentTransaction ARG1){
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabUnselected(标签为arg0,FragmentTransaction ARG1){
// TODO自动生成方法存根
}
}
PlaceListFragment.java
公共类PlaceListFragment扩展片段{
ListView控件listViewData;
PlaceAdapter placeAdapter;
名单< PlaceModel>结果=新的ArrayList< PlaceModel>();
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_list,集装箱,假);
捆绑的args = getArguments();
listViewData =(ListView控件)rootView.findViewById(android.R.id.list);
//我会通过结果和currentLocation这里
placeAdapter =新PlaceAdapter(getActivity(),R.layout.fragment_list_item,结果,currentLocation);
listViewData.setAdapter(placeAdapter);
返回rootView;
}
}
AppSectionsPagerAdapter.java
公共类AppSectionsPagerAdapter扩展FragmentPagerAdapter {
最终诠释PAGE_COUNT = 2;
公共AppSectionsPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT为arg0){
捆绑数据=新包();
开关(将arg0){
情况下0:
PlaceListFragment片段1 =新PlaceListFragment();
fragment1.setArguments(数据);
返回片段1;
默认:
PlaceListFragment fragment2 =新PlaceListFragment();
fragment2.setArguments(数据);
返回fragment2;
}
}
@覆盖
公众诠释getCount将(){
返回PAGE_COUNT;
}
}
通常的活动,将有一个参考的片段。在你的 SearchableActivity.java
你是不是也装载 PlaceListFragment.java
无论是在的setContentView(activity_searchable。 XML);
或你需要创建片段的实例,并添加/替换使用的片段 FragmentTransaction
你可以找到一个很好的例子,在这里就如何片段之间或活动与放大器之间的沟通;片段。
I know there are many topics about this here. I have also read documentation many times but I can't find the best way to pass data from activity to fragment.
I want to be able to show the results of my Searchable activity in two differents layouts (list and map) using swipe Views with tabs. I have to pass 2 data to the fragments: "currentLocation" which is the current user location and "result" which is a list of objects.
I have omited some parts of my code to make it more understandable.
SearchableActivity.java
public class SearchableActivity extends ActionBarActivity implements TabListener {
List<PlaceModel> result = new ArrayList<PlaceModel>();
private SearchView mSearchView;
private String currentLocation;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setText("List").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this));
// get currentLocation here
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
final String query = intent.getStringExtra(SearchManager.QUERY);
// get result here
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
PlaceListFragment.java
public class PlaceListFragment extends Fragment {
ListView listViewData;
PlaceAdapter placeAdapter;
List<PlaceModel> result = new ArrayList<PlaceModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
Bundle args = getArguments();
listViewData = (ListView) rootView.findViewById(android.R.id.list);
// I will pass result and currentLocation here
placeAdapter = new PlaceAdapter(getActivity(), R.layout.fragment_list_item, result, currentLocation);
listViewData.setAdapter(placeAdapter);
return rootView;
}
}
AppSectionsPagerAdapter.java
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
Bundle data = new Bundle();
switch(arg0) {
case 0:
PlaceListFragment fragment1 = new PlaceListFragment();
fragment1.setArguments(data);
return fragment1;
default:
PlaceListFragment fragment2 = new PlaceListFragment();
fragment2.setArguments(data);
return fragment2;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
Usually the activities will have a reference to their fragments. In your SearchableActivity.java
are you also loading PlaceListFragment.java
either in setContentView(activity_searchable.xml);
or you need to create a instance of the fragment and add/replace a fragment using FragmentTransaction
.
you can find a good example here on how to communicated between fragments or between activity & fragment.
这篇关于如何从活动发送的数据片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!