gpt4 book ai didi

android - 如何在 Android 的 viewpager 的不可见 fragment 中执行一个函数

转载 作者:行者123 更新时间:2023-11-30 03:55:50 28 4
gpt4 key购买 nike

我有两个 fragment Fragment1 和 Fragment2。当我启动应用程序时,两个 fragment 中定义的函数都会被执行。所以启动需要很长时间(因为 XML 解析和 ListView 的填充在两个 fragment 上执行) .我需要减少应用程序的启动时间。他们是否有任何方式在启动时执行 Fragment1,并将 Fragment2 的执行带到后台?这样用户就可以与 Fragment1 进行交互。而 Fragment2 可以在不打扰用户的情况下执行其功能。

这是我的代码,

fragment 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);

}

fragment 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);

}

主要 Activity =>

 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;
}

}
}

最佳答案

这与您的其他问题有关:

Is their any way to stop execution of Fragment2 when application startup.I have to start it only when I swipe to Fragment2

不,您一开始就不应该尝试这样做。 ViewPager需要第二个Fragment (尚不可见)保持良好状态,以便用户可以根据需要立即滑动到它。如果那个Fragment不会构建那么用户体验会很差,尤其是在您从 Url 解析一些 xml 的情况下。主要UI线。如果您想提高应用程序的速度并避免可能的 ANR,您真的应该考虑在后台线程上进行 xml 解析。

I want to call 'function1' on startup and 'function2' only when I swipe to Fragment2

离开Fragment 1 现在(调用您希望被调用的方法),然后使用 OnPageChangeListenerViewPager 上.我假设您使用自定义 FragmentPagerAdapter所以你可以获得对第二个 Fragment 的引用并直接调用populate_listview()方法:

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Fragment2 = (Fragment2) getSupportFragmentManager()
.findFragmentByTag(
"android:switcher:" + R.id.viewPager + ":" + 1);
if (fragment != null) {
fragment.populate_listview();// remove the call from onViewCreated
}
}

但这只是一个 hack。你想要做的是使用后台线程(AysncTask 或普通线程)进行长时间操作。看看这个blog entry .

关于android - 如何在 Android 的 viewpager 的不可见 fragment 中执行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13377126/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com