2016-05-07 4 views
0

内viewpagerに大きなビットマップを扱う私はリストビューの行としてviewpagerに表示しなければならないので、私はダウンロードするためのpiccasoライブラリを使用していますので、問題は画像である要件によると、すべての画像がサーバーから、リストビュー内のviewpagerを持っています私は、これは私のコードリストビュー

でどのように私はこれが私のフラグメントクラスですスムーズ それを達成することができ、私は任意のより良い解決策を教えて、それはメモリの外に出た後、リストビューをスクロールしようとしているときの解像度は、高すぎる

public class home_fragment extends Fragment { 
    Session_manager session; 
    ProgressDialog pDialog; 
    ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); 
    ArrayList<String> Array_img = new ArrayList<String>(); 
    ArrayList<String> Array_single_image = new ArrayList<String>(); 
    SampleAdapter adapter; 
    String accestoken; 
    String device_id; 
    CircularImageView img_profile; 
    private Resources mResources; 
    ListView Lv; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.home_activity, container, false); 
     Lv = (ListView) rootView.findViewById(R.id.listView); 
     mResources = getActivity().getResources(); 
//  img_profile = (CircularImageView) rootView.findViewById(R.id.imageView_circular); 
//  img_profile.setBorderColor(mResources.getColor(R.color.Loginbg)); 
//  img_profile.setBorderWidth(5); 
// 
//  final Bitmap srcBitmap = BitmapFactory.decodeResource(mResources, R.drawable.avatar_girl); 
// 
//  // Display the bitmap in ImageView 
//  img_profile.setImageBitmap(srcBitmap); 
// 
//  Paint paint = new Paint(); 
// 
//  // Get source bitmap width and height 
//  int srcBitmapWidth = srcBitmap.getWidth(); 
//  int srcBitmapHeight = srcBitmap.getHeight(); 
// 
//    /* 
//     IMPORTANT NOTE : You should experiment with border and shadow width 
//     to get better circular ImageView as you expected. 
//     I am confused about those size. 
//    */ 
//  // Define border and shadow width 
//  int borderWidth = 25; 
//  int shadowWidth = 10; 
// 
//  // destination bitmap width 
//  int dstBitmapWidth = Math.min(srcBitmapWidth,srcBitmapHeight)+borderWidth*2; 
//  //float radius = Math.min(srcBitmapWidth,srcBitmapHeight)/2; 
// 
//  // Initializing a new bitmap to draw source bitmap, border and shadow 
//  Bitmap dstBitmap = Bitmap.createBitmap(dstBitmapWidth,dstBitmapWidth, Bitmap.Config.ARGB_8888); 
// 
//  // Initialize a new canvas 
//  Canvas canvas = new Canvas(dstBitmap); 
// 
//  // Draw a solid color to canvas 
//  canvas.drawColor(Color.WHITE); 
// 
//  // Draw the source bitmap to destination bitmap by keeping border and shadow spaces 
//  canvas.drawBitmap(srcBitmap, (dstBitmapWidth - srcBitmapWidth)/2, (dstBitmapWidth - srcBitmapHeight)/2, null); 
// 
//  // Use Paint to draw border 
//  paint.setStyle(Paint.Style.STROKE); 
//  paint.setStrokeWidth(borderWidth * 2); 
//  paint.setColor(Color.WHITE); 
// 
//  // Draw the border in destination bitmap 
//  canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/2, paint); 
// 
//  // Use Paint to draw shadow 
//  paint.setColor(Color.LTGRAY); 
//  paint.setStrokeWidth(shadowWidth); 
// 
//  // Draw the shadow on circular bitmap 
//  canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2,canvas.getWidth()/2,paint); 
// 
//    /* 
//     RoundedBitmapDrawable 
//      A Drawable that wraps a bitmap and can be drawn with rounded corners. You 
//      can create a RoundedBitmapDrawable from a file path, an input stream, or 
//      from a Bitmap object. 
//    */ 
//  // Initialize a new RoundedBitmapDrawable object to make ImageView circular 
//  RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mResources, dstBitmap); 
// 
//    /* 
//     setCircular(boolean circular) 
//      Sets the image shape to circular. 
//    */ 
//  // Make the ImageView image to a circular image 
//  roundedBitmapDrawable.setCircular(true); 
// 
//    /* 
//     setAntiAlias(boolean aa) 
//      Enables or disables anti-aliasing for this drawable. 
//    */ 
//  roundedBitmapDrawable.setAntiAlias(true); 
// 
//  // Set the ImageView image as drawable object 
//  img_profile.setImageDrawable(roundedBitmapDrawable); 






     session = new Session_manager(getActivity().getApplicationContext()); 
     HashMap<String, String> user = session.getUserDetails(); 
     // img_profile.setImageResource(R.drawable.profile_placeholder); 
     // access_token 
     accestoken = user.get(session.Acess_Token); 


     device_id = user.get(session.device_id); 
     new Login().execute(); 

     return rootView; 
    } 
    private class Login extends AsyncTask<Void, String, Void> implements DialogInterface.OnCancelListener { 
     String android_id; 
     String myVersion, Hardware; 
     JSONObject jsonnode, json_User; 
     String str; 
     String Name, access_tocken, Ostype; 


     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      // mProgressHUD = ProgressHUD.show(HomeActivity.this, "Connecting", true, true, this); 
      pDialog = new ProgressDialog(getActivity()); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
      android_id = Settings.Secure.getString(getActivity().getContentResolver(), 
        Settings.Secure.ANDROID_ID); 
      myVersion = android.os.Build.VERSION.RELEASE; 
      Hardware = Build.MANUFACTURER; 
      data.clear(); 
      Array_img.clear(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      publishProgress("Please wait..."); 

      String Response = makeServiceCall(Constants.Fetch_posts); 
      if (Response != null) { 
       try { 
        JSONObject jsonObj = null; 
        try { 
         jsonObj = new JSONObject(Response); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        // Getting JSON Array node 
        // JSONArray array1 = null; 
        str = jsonObj.getString("status"); 
        if (str.contentEquals("true")) { 
         //jsonnode = jsonObj.getJSONObject("data"); 
         JSONArray array1 = null; 
         array1 = jsonObj.getJSONArray("data"); 

         // looping through All Contacts 
         for (int i = 0; i < array1.length(); i++) { 
          JSONObject c = array1.getJSONObject(i); 

          String id = c.getString("id"); 
          String user_id = c.getString("user_id"); 
          String caption = c.getString("caption"); 
          String Name = c.getString("name"); 
          String username =c.getString("username"); 
          String media1_thumb_url = c.getString("media1_thumb_url"); 
          String media2_thumb_url = c.getString("media2_thumb_url"); 
          String media3_thumb_url = c.getString("media3_thumb_url"); 
          String media4_thumb_url = c.getString("media4_thumb_url"); 
          String media1_url = c.getString("media1_url"); 
          String media2_url = c.getString("media2_url"); 
          String media3_url = c.getString("media3_url"); 
          String media4_url = c.getString("media4_url"); 
          String created_at = c.getString("created_at"); 
          String updated_at = c.getString("updated_at"); 
          String like_status = c.getString("liked"); 
          String follow_status = c.getString("follow"); 
          if(caption.contentEquals("null")){ 
           caption=""; 
          } 
          HashMap<String, String> Info = new HashMap<String, String>(); 
          Info.put("Post_id", id); 
          Info.put("username", username); 
          Info.put("user_id", user_id); 
          Info.put("Name", Name); 
          Info.put("caption", caption); 
          Info.put("media1_thumb_url", media1_thumb_url); 
          Info.put("media2_thumb_url", media2_thumb_url); 
          Info.put("media3_thumb_url", media3_thumb_url); 
          Info.put("media4_thumb_url", media4_thumb_url); 
          Info.put("media1_url", media1_url); 
          Info.put("media2_url", media2_url); 
          Info.put("media3_url", media3_url); 
          Info.put("media4_url", media4_url); 
          Info.put("created_at", created_at); 
          Info.put("updated_at", updated_at); 
          Info.put("likestatus", like_status); 
          Info.put("followstatus", follow_status); 
          data.add(Info); 
          Array_img.add(media1_url + "," + media2_url + "," + media3_url + "," + media4_url); 
          Array_single_image.add(media1_url); 


//      String userid = jsonnode.getString("user_id"); 
//      Name = json_User.getString("name"); 
//      access_tocken= jsonnode.getString("access_token"); 
//      Ostype= jsonnode.getString("device_id"); 
         } 
         // looping through All Contacts 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 


      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      pDialog.dismiss(); 
      adapter = new SampleAdapter(getActivity(), 
        android.R.layout.simple_list_item_1, data, Array_img,Array_single_image, 
        getActivity().getApplication()); 
//   RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); 
//   Lv.setLayoutManager(mLayoutManager); 
//   MyRecyclerAdapter adapter2 = new MyRecyclerAdapter(getActivity(), data,Array_img); 

      Lv.setAdapter(adapter); 

     // Lv.setItemAnimator(new DefaultItemAnimator()); 

     } 

     @Override 
     public void onCancel(DialogInterface dialog) { 

     } 
    } 

    public String makeServiceCall(String url) { 
     String response = null; 
     try { 
      // http client 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpEntity httpEntity = null; 
      HttpResponse httpResponse = null; 

      // Checking http request method type 

//    if (params != null) { 
//     String paramString = URLEncodedUtils 
//       .format(params, "utf-8"); 
//     url += "?" + paramString; 
//    } 

      HttpGet httpGet = new HttpGet(url); 
      httpGet.addHeader("X-TOKEN", accestoken); 
      httpGet.addHeader("X-DEVICE", device_id); 


      httpResponse = httpClient.execute(httpGet); 


      httpEntity = httpResponse.getEntity(); 
      response = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return response; 

    } 

と、このアダプタクラス

public class SampleAdapter extends ArrayAdapter<String> implements 
     OnClickListener { 
    private static final String KEY_SELECTED_PAGE = "KEY_SELECTED_PAGE"; 
    private static final String KEY_SELECTED_CLASS = "KEY_SELECTED_CLASS"; 
    private static final ArrayList<TransformerItem> TRANSFORM_CLASSES; 
    private static final String TAG = "SampleAdapter"; 

    private static Context contexts; 
    private Application mAppContext; 
    private LayoutInflater mLayoutInflater = null; 
    private Random mRandom; 
    static String List_img; 

    double km, lat_to, lon_to; 
    String strlat_to, str_long_to; 
    String Str_km = ""; 
    ImageLoader img_loader; 
    private LruCache<String, Bitmap> mMemoryCache; 

    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>(); 

    ArrayList<HashMap<String, String>> imagess = new ArrayList<HashMap<String, String>>(); 
    ArrayList<String> Array_imgadpter = new ArrayList<String>(); 

    static { 
     TRANSFORM_CLASSES = new ArrayList<>(); 


     TRANSFORM_CLASSES.add(new TransformerItem(CubeOutTransformer.class)); 
     //TRANSFORM_CLASSES.add(new TransformerItem(CubeInTransformer.class)); 
    } 

    private int mSelectedItem; 

    private PageAdapter mAdapter; 
    ArrayList<String> Array_single_image = new ArrayList<String>(); 
    static String[] items; 

    public SampleAdapter(Context context, int textViewResourceId, 
         ArrayList<HashMap<String, String>> images, ArrayList<String> Array_img, ArrayList<String> Array_singr, Application app) { 
     super(context, textViewResourceId); 
     this.mLayoutInflater = LayoutInflater.from(context); 
     this.mRandom = new Random(); 
     this.imagess = images; 
     Array_single_image = Array_singr; 
     mAppContext = app; 
     //Array_imgadpter.clear(); 
     this.Array_imgadpter = Array_img; 
     contexts = context; 
    } 

    @SuppressWarnings("static-access") 
    @Override 
    public View getView(final int position, View convertView, 
         final ViewGroup parent) { 
     Resources mResources; 
     final ViewHolder vh; 
     final Context context = parent.getContext(); 
     img_loader = new ImageLoader(mAppContext); 
     // if (convertView == null) { 
     convertView = mLayoutInflater.inflate(R.layout.list_home_lv, 
       parent, false); 

     vh = new ViewHolder(); 
     vh.mPager = (viewpager) convertView.findViewById(R.id.container); 
     vh.Name = (TextView) convertView.findViewById(R.id.textView7); 
     vh.Caption = (TextView) convertView.findViewById(R.id.textView10); 
     vh.Circular_imagview = (CircularImageView) convertView.findViewById(R.id.imageView4); 
     convertView.setTag(vh); 
     //vh.mPager.setId(position + 1); 
//   } else { 
//   vh = (ViewHolder) convertView.getTag(); 
////    vh.mPager.setId(position + 1); 
//  } 
//   vh.Circular_imagview.setBorderColor(mAppContext.getResources().getColor(R.color.Loginbg)); 
//   vh.Circular_imagview.setBorderWidth(5); 
     vh.mPager.setId(position + 1); 
     vh.mPager.setOnItemClickListener(new viewpager.OnItemClickListener() { 
      @Override 
      public void onItemClick(int position) { 
       int id = vh.mPager.getId(); 
       id = id - 1; 
       String postion = String.valueOf(id); 
       List_img = Array_imgadpter.get(id); 
       String str_id =imagess.get(id).get("Post_id"); 
       String status_like=imagess.get(id).get("likestatus"); 
       String caption=imagess.get(id).get("caption"); 
       //Toast.makeText(mAppContext,"click "+id,Toast.LENGTH_LONG).show(); 
       Intent lObjIntent = new Intent(mAppContext, Images_comment_screen.class); 
       lObjIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       lObjIntent.putExtra("images",List_img); 
       lObjIntent.putExtra("Post_id",str_id); 
       lObjIntent.putExtra("status_like",status_like); 
       lObjIntent.putExtra("caption",caption); 
       mAppContext.startActivity(lObjIntent); 
       //finish(); 
      } 
     }); 
     Paint paint = new Paint(); 
     mResources = mAppContext.getResources(); 
     try { 
      final Bitmap srcBitmap = BitmapFactory.decodeResource(mResources, R.drawable.profile_placeholder); 
      // Get source bitmap width and height 

      int srcBitmapWidth = srcBitmap.getWidth(); 
      int srcBitmapHeight = srcBitmap.getHeight(); 

       /* 
        IMPORTANT NOTE : You should experiment with border and shadow width 
        to get better circular ImageView as you expected. 
        I am confused about those size. 
       */ 
      // Define border and shadow width 
      int borderWidth = 20; 
      int shadowWidth = 0; 

      // destination bitmap width 
      int dstBitmapWidth = Math.min(srcBitmapWidth, srcBitmapHeight) + borderWidth * 2; 
      //float radius = Math.min(srcBitmapWidth,srcBitmapHeight)/2; 

      // Initializing a new bitmap to draw source bitmap, border and shadow 

      Bitmap dstBitmap = Bitmap.createBitmap(dstBitmapWidth, dstBitmapWidth, Bitmap.Config.ARGB_8888); 
      Canvas canvas = new Canvas(dstBitmap); 




      // Initialize a new canvas 

      // Draw a solid color to canvas 
      canvas.drawColor(Color.WHITE); 

      // Draw the source bitmap to destination bitmap by keeping border and shadow spaces 
      canvas.drawBitmap(srcBitmap, (dstBitmapWidth - srcBitmapWidth)/2, (dstBitmapWidth - srcBitmapHeight)/2, null); 

      // Use Paint to draw border 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(borderWidth * 2); 
      paint.setColor(Color.WHITE); 

      // Draw the border in destination bitmap 
      canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/2, paint); 

      // Use Paint to draw shadow 
      paint.setColor(Color.LTGRAY); 
      paint.setStrokeWidth(shadowWidth); 

      // Draw the shadow on circular bitmap 
      canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/2, paint); 

       /* 
        RoundedBitmapDrawable 
         A Drawable that wraps a bitmap and can be drawn with rounded corners. You 
         can create a RoundedBitmapDrawable from a file path, an input stream, or 
         from a Bitmap object. 
       */ 
      // Initialize a new RoundedBitmapDrawable object to make ImageView circular 
      RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mResources, dstBitmap); 

       /* 
        setCircular(boolean circular) 
         Sets the image shape to circular. 
       */ 
      // Make the ImageView image to a circular image 
      roundedBitmapDrawable.setCircular(true); 

       /* 
        setAntiAlias(boolean aa) 
         Enables or disables anti-aliasing for this drawable. 
       */ 
      roundedBitmapDrawable.setAntiAlias(true); 

      // Set the ImageView image as drawable object 
      vh.Circular_imagview.setImageDrawable(roundedBitmapDrawable); 
     }catch (OutOfMemoryError error){ 
      error.printStackTrace(); 
     } 
     //convertView.setTag(vh); 

     //int selectedPage = 0; 
     //vh.mPager.setId(position + 1); 
     try { 
      vh.mPager.setPageTransformer(true, TRANSFORM_CLASSES.get(0).clazz.newInstance()); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 




     //mPager.setCurrentItem(selectedPage); 
     List_img = Array_imgadpter.get(position); 
     vh.Name.setText(imagess.get(position).get("username")); 
     vh.Caption.setText(imagess.get(position).get("caption")); 
     vh.mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
       int positionn = vh.mPager.getId(); 
       positionn = positionn - 1; 
       String postion = String.valueOf(positionn); 
       List_img = Array_imgadpter.get(positionn); 
       items = List_img.split(","); 

      } 

      @Override 
      public void onPageSelected(int position) { 
       int positionn = vh.mPager.getId(); 
       positionn = positionn - 1; 
       String postion = String.valueOf(positionn); 
       List_img = Array_imgadpter.get(positionn); 

      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

       int positionn = vh.mPager.getId(); 
       positionn = positionn - 1; 
       String postion = String.valueOf(positionn); 
       List_img = Array_imgadpter.get(positionn); 


      } 


     }); 




     FragmentManager fm = ((Activity) context).getFragmentManager(); 

     mAdapter = new PageAdapter(fm); 

     vh.mPager.setAdapter(mAdapter); 

     return convertView; 
    } 


    static class ViewHolder { 
     ImageView imgView, star1, star2, star3, star4; 
     TextView Name, Caption, fixed_meal, price, rating, distance, TxtVw_Afterdiscount, Txtvw; 
     Button BTN_DETAIL; 
     private viewpager mPager; 
     Button button2; 
     CircularImageView Circular_imagview; 

    } 

    @Override 
    public int getCount() { 

     return imagess.size(); 
    } 


    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
//  int positionn = vh.mPager.getId(); 
//  positionn = positionn - 1; 
//  String postion = String.valueOf(positionn); 
//  List_img = Array_imgadpter.get(positionn); 
//  Intent intent = new Intent(mAppContext, Images_comment_screen.class); 
//  intent.putExtra("images", List_img); 
// 
// 
//  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//  mAppContext.startActivity(intent); 
     Toast.makeText(mAppContext, "click", Toast.LENGTH_LONG).show(); 
    } 

    private static final class TransformerItem { 

     final String title; 
     final Class<? extends ViewPager.PageTransformer> clazz; 

     public TransformerItem(Class<? extends ViewPager.PageTransformer> clazz) { 
      this.clazz = clazz; 
      title = clazz.getSimpleName(); 
     } 

     @Override 
     public String toString() { 
      return title; 
     } 

    } 

    public class PlaceholderFragment extends Fragment { 

     private static final String EXTRA_POSITION = "EXTRA_POSITION"; 
     //private final int[] COLORS = new int[]{R.drawable.imgone, R.drawable.imgtwo, R.drawable.imgthree, R.drawable.imgfour}; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      final int position = getArguments().getInt(EXTRA_POSITION); 
      final ImageView textViewPosition = (ImageView) inflater.inflate(R.layout.fragment_main, container, false); 

      items = List_img.split(","); 
      String url = items[position]; 

      try { 
       Picasso.with(contexts).load(items[position]).placeholder(R.drawable.placeholderdevzillad).into(textViewPosition); 
       // 
       // img_loader.DisplayImage(url,textViewPosition); 
      } catch (OutOfMemoryError error) { 

      } 


      //textViewPosition.setBackgroundResource(COLORS[position]); 
      return textViewPosition; 
     } 


    } 

    private class PageAdapter extends FragmentStatePagerAdapter { 

     public PageAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      final Bundle bundle = new Bundle(); 
      bundle.putInt(PlaceholderFragment.EXTRA_POSITION, position); 

      final PlaceholderFragment fragment = new PlaceholderFragment(); 
      fragment.setArguments(bundle); 

      return fragment; 
     } 

     @Override 
     public int getCount() { 
      return 4; 
     } 

    } 



} 

答えて

0

は、ここではいくつかの提案です:

  1. あなたのSampleAdapterであなたのListView項目を、リサイクルしていない - それがnullでない場合getView、convertViewが再利用されていません。
  2. はちょうどViewPagerのアイテムとしてのViewGroup使用し、あなたのViewPager項目としてフラグメントを使用する必要はありません。より多くの場合
  3. 、私はあなたが別のListView項目の異なるViewPagersのすべての項目を管理し、リサイクルするリサイクルPagerAdapterを定義することができると思います。

私の悪い英語を許し、これはあなたのために有用であると思います。

+0

提案をいただきありがとうございます –