我是Android编程的新手。
我用Google搜索和stackoverflowed,阅读了android文档,但没有任何帮助,或者我不太了解。
我正在尝试AsyncTask用户界面通过ViewPager中的onPageSelected进行分段。
就像AndroMoney应用程序的ViewPager(在onPageSelected之前,它显示为“正在加载...”,当onPageSelected时,它会进行AsyncTasked并加载。):
AndroMoney ViewPager Fragment Screenshoot
但我的代码是:它在滑动时异步任务,有时会在两个片段之间停止,如下所示:
my code viewpager screenshot
与AndroMoney应用程序不同,我的代码不会在onPageSelected上执行异步任务。
这是MyFragment.java:
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
final StringBuilder buffer=new StringBuilder();
View v;
public static final MyFragment newInstance(int message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putInt(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.myfragment_layout, container, false);
new AsyncDatabase().execute();
return v;
}
public class AsyncDatabase extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params){
int message = getArguments().getInt(EXTRA_MESSAGE);
BibleService bService=new BibleServiceImpl(getActivity().getApplication());
List<Bible> bible=bService.getBibleByChapterNumber("Salm", message);
for(Bible b: bible){
for(int i=0;i<10;i++){
buffer.append(b);
}
}
return null;
}
@Override
protected void onPostExecute(Void result){
TextView txt1=(TextView)v.findViewById(R.id.textView1);
TextView txt2=(TextView)v.findViewById(R.id.textView2);
txt1.setText("Done");
txt2.setText(buffer.toString());
}
@Override
protected void onPreExecute(){
TextView txt1=(TextView)v.findViewById(R.id.textView1);
txt1.setText("Loading...");
}
@Override
protected void onProgressUpdate(Void... values){
}
}
}
这是PageViewActivity.java:
public class PageViewActivity extends FragmentActivity{
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setOffscreenPageLimit(1);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(11);
pager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
public void onPageSelected(int position) {
}
});
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
for (int j = 2; j < 150; j++) {
fList.add(MyFragment.newInstance(j));
}
return fList;
}
private class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
我通过添加以下代码进行了愚蠢的测试:
public void onPageSelected(int position) {
new MyFragment().new AsyncDatabase().execute();
}
它没有用。
因此,如何异步任务从AndroMoney应用程序的PageViewActivity的onPageSelected片段化。
最佳答案
在您的片段中公开一个新方法:
public void executeAsyncTask() {
new AsyncDatabase().execute();
}
在活动中,将片段列表定为最终列表,以便可以在适配器中访问它:
final List<MyFragment> fragments = getFragments();
然后将您的onPageSelected()方法更改为:
public void onPageSelected(int position) {
fragments.get(position).executeAsyncTask();
}
您还需要更新MyPagerAdapter以使用MyFragment而不是Fragment:
private class MyPageAdapter extends FragmentStatePagerAdapter {
private List<MyFragment> fragments;
public MyPageAdapter(FragmentManager fm, List<MyFragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
private List<MyFragment> getFragments() {
List<MyFragment> fList = new ArrayList<MyFragment>();
for (int j = 2; j < 150; j++) {
fList.add(MyFragment.newInstance(j));
}
return fList;
}