将活动包中的捆绑包发送到其中一个“滑动”标签?

时间:2014-03-14 09:54:58

标签: java android rss

我是android的新手。我已经按照tutorial来实现Simple Rss Reader。在SplashActivity期间,它从网络获取并将其传递给下一个活动,即“ListActivity”。

现在,我已经使用3个标签实现了滑动标签,我想从SplashActivity发送包来更新第二个标签页中的列表视图。

以下是一些代码,

SplashActivity.java

public class SplashActivity extends Activity {

private String RSSFEEDURL = "rss feed link goes here";
RSSFeed feed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() == null
            && !conMgr.getActiveNetworkInfo().isConnected()
            && !conMgr.getActiveNetworkInfo().isAvailable()) {
        // No connectivity - Show alert
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Unable to reach server, \nPlease check your connectivity.")
                .setTitle("TD RSS Reader")
                .setCancelable(false)
                .setPositiveButton("Exit",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                finish();
                            }
                        });

        AlertDialog alert = builder.create();
        alert.show();

    } else {
        // Connected - Start parsing
        new AsyncLoadXMLFeed().execute();

    }

}

private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        // Obtain feed
        DOMParser myParser = new DOMParser();
        feed = myParser.parseXml(RSSFEEDURL);
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        Bundle bundle = new Bundle();
        bundle.putSerializable("feed", feed);

        // launch List activity
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);

        // kill this activity
        finish();
    }

}

}

MainActivity

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
RSSFeed feed;

    // Tab titles
private String[] tabs = { "Home", "Updates", "CastandCrew" };

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


    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
//  actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

TabsPagerAdapter

 public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment();
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

这是GamesFragment Class

public class GamesFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_games, container, false);

    return rootView;
}
}

2 个答案:

答案 0 :(得分:3)

你可以传递两种方式

1路。

GamesFragment frag = new GamesFragment();
frag.setArguments(getIntent().getExtras());

第二路。

首先从bundle中读取对象。

Object feed;
Bundle extras = getIntent().getExtras();
    if (extras != null) {
      feed = extras.getParcelable("feed");
    }

mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),feed);

//首先更改TabsPagerAdapter构造函数,如下所示。将对象发送到GamesFragment构造函数。

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm, Object feed) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment(feed);
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

//更改构造函数。你可以从构造函数中获取对象。

public class GamesFragment extends Fragment {

        public GamesFragment(Object feed) {
            // TODO Auto-generated constructor stub
            }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_games, container, false);

            return rootView;
            }
        }

答案 1 :(得分:0)

您正在使用此return new GamesFragment();代码创建第二个屏幕。你应该在这里发送数据。

将数据发送到Fragment的最佳方法是使用setArguments。

你应该这样做:

将PagerAdapter更改为:

public class TabsPagerAdapter extends FragmentPagerAdapter {

Bundle bundle;    

public TabsPagerAdapter(FragmentManager fm, Bundle bundle) {
    super(fm);
    this.bundle = bundle;
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        GamesFragment frag = new GamesFragment();
        frag.setArguments(bundle);
        return frag;
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

像这样使用你的寻呼机适配器

new TabsPagerAdapter(getSupportFragmentManager(), getIntent().getExtras());

onCreate()的{​​{1}}函数中,您可以获得如下数据:

GamesFragment