Fragment1 と Fragment2 の 2 つのフラグメントがあります。アプリケーションを起動すると、両方のフラグメント内で定義された関数が実行されます。そのため、起動に時間がかかります (両方のフラグメントで XML 解析とリスト ビューの作成が実行されるため)。アプリケーションの起動時間を短縮するには、起動時にFragment1を実行し、Fragment2の実行をバックグラウンドにする方法はありますか? ユーザーが Fragment1.And Fragment2 とやり取りできるように、ユーザーの邪魔をすることなくその機能を実行します。
ここに私のコードがあります、
フラグメント1=>
public class Fragment1 extends Fragment{
public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
GridView list;
HeadlinesAdapter adapter;
private TextView mMessageView;
private Button mClearButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlines.parse();
populate_listview();
}
public void populate_listview()
{
URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_HEAD);
NodeList itemLst = doc.getElementsByTagName("item");
String MarqueeStr="";
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
newsList.add(map);
}
list=(GridView)getActivity().findViewById(R.id.grid);
adapter=new Adapter1(getActivity(), newsList);
list.setAdapter(adapter);
}
フラグメント2=>
public class Fragment2 extends Fragment{
public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
GridView list;
HeadlinesAdapter adapter;
private TextView mMessageView;
private Button mClearButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_fragment, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sports.parse();
populate_listview();
}
public void populate_listview()
{
URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_HEAD);
NodeList itemLst = doc.getElementsByTagName("item");
String MarqueeStr="";
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
newsList.add(map);
}
list=(GridView)getActivity().findViewById(R.id.grid2);
adapter=new HeadlinesAdapter(getActivity(), newsList);
list.setAdapter(adapter);
}
メインアクティビティ=>
public class MainActivity extends FragmentActivity {
private ViewPager mViewPager;
private MessageLoader mLoader;
private Button mSenderButton, mReceiverButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// We get UI references
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mSenderButton = (Button) findViewById(R.id.sender_button);
mReceiverButton = (Button) findViewById(R.id.receiver_button);
// set pager adapter
mViewPager.setAdapter(new MyAdapter(this));
// set receiver button listener
mReceiverButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(1);
}
});
mSenderButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(0);
}
});
}
private class MyAdapter extends FragmentPagerAdapter{
private Context mContext;
private String[] frags = {Fragment1.class.getName(), Fragment2.class.getName()};
public MyAdapter(FragmentActivity activity) {
super(activity.getSupportFragmentManager());
mContext = activity;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment frag = (Fragment) super.instantiateItem(container, position);
if(frag instanceof MessageLoader){
mLoader = (MessageLoader) frag;
}
return frag;
}
@Override
public Fragment getItem(int pos) {
return Fragment.instantiate(mContext, frags[pos]);
}
@Override
public int getCount() {
return frags.length;
}
}
}