我有一个使用 ActionBarSherlock 的选项卡式应用程序。ActionBar 中的选项卡是每个片段。其中一个片段包含一个查看页面,它显示了许多片段 - 每个片段都包含来自 URL 的图像(使用 ImageView 的变体)。
当您第一次单击选项卡时,整个事情就很好地工作了 - 它显示了显示图像的寻呼机。第二次单击选项卡(单击其他选项卡后)没有任何反应 - 寻呼机不显示并且屏幕保持空白(除了 actionBar)。代码附在下面。似乎正在创建 FragmentPagerAdapter,但它没有第二次对项目进行分页。
我究竟做错了什么 ?
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.actionbarsherlock.app.SherlockFragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SurfaceChartFragment extends SherlockFragment {
private ViewPager mPager;
private SurfacePagerAdapter mAdapter;
private static String [] urls = { "gif url 1", "gif url 2 etc." };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.surface_pager, container, false);
mPager = (ViewPager) V.findViewById(R.id.surface_viewpager);
mAdapter = new SurfacePagerAdapter(getActivity().getSupportFragmentManager());
new setAdapterTask().execute();
return V;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
mPager.setAdapter(mAdapter);
}
}
static final class SurfacePagerAdapter extends FragmentPagerAdapter { //
public SurfacePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return urls.length;
}
@Override
public Fragment getItem(int position) {
SurfaceFragment f = new SurfaceFragment();
f.url = urls[position];
f.position = position;
return f;
}
}
public static class SurfaceFragment extends SherlockFragment {
String url = "";
Integer position = 0;
public SurfaceFragment() {
// setRetainInstance(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu(true);
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.e("Exception",e.getMessage());
return null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
Bitmap bimage= getBitmapFromURL(url);
image.setImageBitmap(bimage);
return image;
}
}
}