在我的android应用程序中,我使用viewpager。我有两个fragment.Fragment1和Fragment2。我在两个fragment上调用了两个不同的函数。即,Fragment1上的'function1'和fragment2上的'function2'。当我启动应用程序时,这两个函数一起调用。我想在启动时调用“function1”,只有当我滑动到 Fragment2 时才调用“function2”。我该怎么做?提前致谢.....
这是我的代码,
片段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);
function1();//here I am calling function1
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);
function2();//here I am calling function2
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;
}
}
}
这是布局,
first_fragment.xml =>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" >
</GridView>
</LinearLayout>
second_fragment.xml =>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<GridView
android:id="@+id/grid2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" >
</GridView>
</LinearLayout>
main.xml=>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#CCCCCC"
android:gravity="center_vertical"
android:paddingTop="3dp" >
<Button
android:id="@+id/receiver_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Headlines" />
<Button
android:id="@+id/sender_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Kerala" />
</TableRow>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>