*我希望从WebService显示完整图像,并在前向和后向创建滑动图像。我使用过ViewPager,PagerAdapter Class。 片段类是,
package com.Fragment.Classes;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.PersianBeauty.Adapter.ImagePagerAdapter;
import com.persianbeauty.R;
import com.utility.ConnectionDetector;
import com.utility.JSONfunctions;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
@SuppressLint("ValidFragment")
public class ModelPortFoliolFragment extends Fragment
{
ImagePagerAdapter pagerAdapter;
ViewPager pager;
String[] imageUrls;
View rootView;
ArrayList<String> imageList;
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
// JSON Node names
static String TAG_MODEL = "model";
static String TAG_MODELIMAG1 = "ModelImage";
static String TAG_MODEL_IMAGE2 = "ModelImage_2";
static String TAG_MODEL_IMAGE3= "ModelImage_3";
ProgressDialog mProgressDialog;
String ID;
public ModelPortFoliolFragment(String ID)
{
this.ID=ID;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment_model_portfolio, container, false);
if(!ConnectionDetector.networkStatus(getActivity()))
{
ConnectionDetector.displayNoNetworkDialog(getActivity());
}
else
{
new ParseModelPortFolioJSonData().execute();
}
pager = (ViewPager) rootView.findViewById(R.id.pager);
// pager.setAdapter(new ImagePagerAdapter(imageList,getActivity()));
//pager.setCurrentItem(pagerPosition);
pagerAdapter=new ImagePagerAdapter(imageList,getActivity());
pager.setAdapter(pagerAdapter);
//pager.setAdapter(new ImagePagerAdapter(imageList,getActivity()));
//pager.setCurrentItem(pagerPosition);
return rootView;
}
// @Override
// public void onDestroyView()
// {
// super.onDestroyView();
//
// }
// DownloadJSON AsyncTask
public class ParseModelPortFolioJSonData extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Please wait...");
// mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0)
{ imageList=new ArrayList<String>();
// Create an array
try {
//http://www.persianbeautyawards.com/services/GetModelProfile.php?model_id=56
jsonobject = JSONfunctions.getJSONfromURL(getResources().getString(R.string.base_url).trim() +"GetModelPortfolio.php?model_id="+ID);
System.out.println(getResources().getString(R.string.base_url).trim() +"GetModelPortfolio.php?model_id="+ID+".....................");
// Locate the array name in JSON
System.out.println(jsonobject+".................");
if(jsonobject !=null)
{
jsonarray = jsonobject.getJSONArray(TAG_MODEL);
for (int i = 0; i < jsonarray.length(); i++)
{
jsonobject = jsonarray.getJSONObject(i);
imageList.add(jsonobject.getString(TAG_MODELIMAG1));
System.out.println(jsonobject.getString(TAG_MODELIMAG1)+"...........");
imageList.add(jsonobject.getString(TAG_MODEL_IMAGE2));
System.out.println(jsonobject.getString(TAG_MODEL_IMAGE2)+"...........");
imageList.add(jsonobject.getString(TAG_MODEL_IMAGE3));
System.out.println(jsonobject.getString(TAG_MODEL_IMAGE3)+"...........");
}
// Set the JSON Objects into the array
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args)
{
if(imageList !=null)
{
pagerAdapter.notifyDataSetChanged();
if (rootView != null) {
ViewGroup parentViewGroup = (ViewGroup) rootView.getParent();
if (parentViewGroup != null) {
parentViewGroup.removeAllViews();
}
}
}
else
{
Toast.makeText(getActivity(),"Server Error try Again", Toast.LENGTH_LONG).show();
}
mProgressDialog.dismiss();
}
}
}
我使用了从PageAdapter扩展的Adapter Class, 我的PageAdapter类是,
PageAdapter:
package com.PersianBeauty.Adapter;
import java.util.ArrayList;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.persianbeauty.R;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImagePagerAdapter extends PagerAdapter
{
LayoutInflater inflator;
ImageView imgDisplay;
Context context;
ArrayList<String> slideShowImages;
private ImageLoader imageLoader;
public ImagePagerAdapter(ArrayList<String> slideShowImages,Context context)
{
this.context=context;
this.slideShowImages =slideShowImages;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public int getCount()
{
return slideShowImages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflator.inflate(R.layout.item_pager_view, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inPreferredConfig = Bitmap.Config.ARGB_8888;
// Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
// imgDisplay.setImageBitmap(bitmap);
//
System.out.println("ImageUrl---------------------"+slideShowImages.get(position));
imageLoader.displayImage(slideShowImages.get(position).trim(), imgDisplay);
((ViewPager) container).addView(imgDisplay, 0);
return imgDisplay;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((ImageView) object);
}
}
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
图像布局,它会使imageLoader类膨胀,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
/>
</FrameLayout>
当我运行此代码时,它给出了以下错误,并且未在视图页面上加载任何类型的图像,
02-12 09:13:22.034: E/AndroidRuntime(3050): FATAL EXCEPTION: main
02-12 09:13:22.034: E/AndroidRuntime(3050): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.view.ViewGroup.addView(ViewGroup.java:3210)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.addView(ViewPager.java:1304)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.view.ViewGroup.addView(ViewGroup.java:3155)
02-12 09:13:22.034: E/AndroidRuntime(3050): at com.PersianBeauty.Adapter.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:62)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:908)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2819)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
02-12 09:13:22.034: E/AndroidRuntime(3050): at com.Fragment.Classes.ModelPortFoliolFragment$ParseModelPortFolioJSonData.onPostExecute(ModelPortFoliolFragment.java:151)
02-12 09:13:22.034: E/AndroidRuntime(3050): at com.Fragment.Classes.ModelPortFoliolFragment$ParseModelPortFolioJSonData.onPostExecute(ModelPortFoliolFragment.java:1)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.os.Looper.loop(Looper.java:137)
02-12 09:13:22.034: E/AndroidRuntime(3050): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-12 09:13:22.034: E/AndroidRuntime(3050): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 09:13:22.034: E/AndroidRuntime(3050): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 09:13:22.034: E/AndroidRuntime(3050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 09:13:22.034: E/AndroidRuntime(3050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 09:13:22.034: E/AndroidRuntime(3050): at dalvik.system.NativeStart.main(Native Method)
请给我一些建议。
答案 0 :(得分:2)
在Activity中创建一个名为adapter的属性:
private ImagePagerAdapter adapter;
和onCreate
:
adapter= new ImagePagerAdapter(imageList,getActivity());
只需致电adapter.notifyDataSetChange()
:
if(imageList !=null)
{
adapter.notifyDataSetChange();
}