我的目标是从服务器下载图像并在ViewPager中显示它而不需要任何UI冻结。最初从网络加载数据,那个时候用户界面在下载所有工作正常后冻结片刻。但我需要从ViewPager中的网络冻结UI解析初始加载数据。
这是我的viewPagerActivity类:
public class ViewPagerActivity extends Activity {
ViewPager pager;
String[] URL;
PagerAdapter objPagerAdapter;
ArrayList<String> _arrayImagePath=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
pager=(ViewPager)findViewById(R.id.pagerView);
// Setting the 70 images URL
URL= new String[] { "http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg"
};
objPagerAdapter=new ViewPagerAdapter(ViewPagerActivity.this, _arrayImagePath);
pager.setAdapter(objPagerAdapter);
// Calling Volley Library to Download the Image for each URLi
for(int i=0;i<URL.length;i++)
{
DownloadImages(URL[i], i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_pager, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
void DownloadImages(String imageURL_in,final int counter_in)
{
final String counter=String.valueOf(counter_in);
ImageRequest imageRequest = new ImageRequest(imageURL_in,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(final Bitmap response) {
try{
Log.v("Response Received =",""+counter_in);
// Saved in SD Card and getting the Path
String imagePath = SavedInSDCard(response, counter + "_hires");
//Added ImagePath
_arrayImagePath.add(imagePath);
// Notifying the Adapter
objPagerAdapter.notifyDataSetChanged();
} catch (Exception e) {e.printStackTrace();}
}
}, 0, 0, null, null);
ApplicationController.getInstance().addToRequestQueue(imageRequest);
}
public String SavedInSDCard(Bitmap bitmap2, String dishId) {
File dir = new File(ApplicationController.getAppContext()
.getExternalFilesDir("ViewPager_POC") + "/Cache");
// Create the storage directory if it does not exist
if (!dir.exists()) {
dir.mkdirs();
}
String stDishID = dishId;
String formatedDsihID = stDishID.replace(":", "R");
File file = new File(dir, formatedDsihID);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String imgPath = dir + "/" + formatedDsihID;
return imgPath;
}
}
这是我的ViewPagerAdapter类:
public class ViewPagerAdapter extends PagerAdapter{
Context _context;
LayoutInflater inflater;
ArrayList<String> _arrayImagePath;
public ViewPagerAdapter(Context context_in, ArrayList<String> imagePath_in) {
_arrayImagePath=imagePath_in;
_context = context_in;
}
@Override
public int getCount() {
return _arrayImagePath.size();
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtCounter;
ImageView imgView;
inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.view_pager_item, container,
false);
txtCounter = (TextView) itemView.findViewById(R.id.counter);
imgView= (ImageView) itemView.findViewById(R.id.images);
//Setting the values for Image and textview
txtCounter.setText(""+(position+1));
BitmapWorkerTask task = new BitmapWorkerTask(imgView);
task.execute(_arrayImagePath.get(position));
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
String path = params[0];
return BitmapFactory.decodeFile(path);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
}
这是我的xml view_pager_activity.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.poc_viewpager.ViewPagerActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pagerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是我的查看分页器项目xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
有人可以提出任何解决这个问题的想法吗? 在此先感谢
答案 0 :(得分:0)
您应该在FragmentPagerAdapter
中实施FragmentStatePagerAdapter
或ViewPagerActivity
。
之后,将您的图片加载到自定义Fragment
的{{1}}方法中。
UI冻结是由于onCreateView
中的instantiateItem
仍在等待用户界面准备就绪,然后才会让您的图片加载任务异步。
按照Android Developers documentation中提到的那样保留ViewPager
的实施情况总是更好:
ViewPager
最常与ViewPager
结合使用,这是一种提供和管理每个页面生命周期的便捷方式。有一些标准适配器用于使用Fragment
的片段,它涵盖了最常见的用例。这些是ViewPager
和FragmentPagerAdapter
;这些类中的每一个都有简单的代码,展示了如何使用它们构建完整的用户界面。
答案 1 :(得分:0)