自定义对话框中的视图,如facebook

时间:2014-04-09 04:17:18

标签: android android-viewpager android-alertdialog android-custom-view

我正在学习android编程 我想这样创造。?你能帮我提一下吗? 韩国社交协会。 P / s:我认为使用Viewpaper和自定义对话框

enter image description here

package com.example.price.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.price.R;
import com.example.price.model.Photos_Json;
import com.example.price.model.Photos_List;
import com.example.price.model.Place;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class MyAdapter_Place  extends ArrayAdapter<Place> {

      Dialog dialog;
    Activity context=null;
     ArrayList<Place> myArray=null;
     int layoutId;
     Gallery gallery ;
     ArrayList<String> ImageUrl=new ArrayList<String>();
     String url="http://social-nice-price.appspot.com/webservices/Places/";
    public MyAdapter_Place(Activity context, int resource, ArrayList<Place> arr) {
        super(context, resource, arr);
        // TODO Auto-generated constructor stub
         this.context=context;
         this.layoutId=resource;
         this.myArray= arr;
    }
    public View getView(int position, View convertView,
             ViewGroup parent) {
        // lay Layout 
        LayoutInflater inflater=context.getLayoutInflater();
        convertView=inflater.inflate(layoutId, null);
        // khai bao cac Phan tu  de SETTing  sau nay 
        TextView Txt_sellingPrice=(TextView)convertView.findViewById(R.id.Txt_sellingPrice);
        TextView Txt_priceAfterDiscount=(TextView)convertView.findViewById(R.id.Txt_priceAfterDiscount);
        TextView Txt_address=(TextView)convertView.findViewById(R.id.Txt_address);
        TextView Txt_updateDate=(TextView)convertView.findViewById(R.id.Txt_updateDate);
        TextView Txt_hotline=(TextView)convertView.findViewById(R.id.Txt_hotline);
        TextView Txt_name=(TextView)convertView.findViewById(R.id.Txt_name);
        TextView Txt_website=(TextView)convertView.findViewById(R.id.Txt_website);
         gallery = (Gallery)convertView.findViewById(R.id.gallery1);
        ImageView ChiDuong=(ImageView)convertView.findViewById(R.id.Imageview_chiduong);
         // Lya phan tu tai vi tri position
        Place place=myArray.get(position);

        // Setting data
        Txt_sellingPrice.setText("Giá Bán : "+place.getSellingPrice());
        Txt_priceAfterDiscount.setText("Giá Đã Giảm : "+place.getPriceAfterDiscount());
        Txt_address.setText("Địa Chỉ : "+place.getAddress());
        //Txt_productCode.setText("Mã Sản Phẩm : "+place.getProductCode());
        Txt_updateDate.setText("Ngày Cập Nhật : "+place.UpdateDate);
        Txt_hotline.setText("Hotline : " +place.getHotLine());
        Txt_name.setText("Tên Quán : " +place.getName());
        Txt_website.setText("Website : "+place.getWebsite());

        //hien thi anh viewpager

        //  lay du lieu json ,=> hinh anh 
        Gson(url+place.ProductCode+"/");

          gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position,
                        long id) {

                  String temp=ImageUrl.get(position);
                  //Toast.makeText(context, temp, Toast.LENGTH_LONG).show();

                  //  dialog hien khi click vao anh trong gallery
                    dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_dialog);
                ViewPager pager=(ViewPager)dialog.findViewById(R.id.pager);
                pager.setAdapter(new ImagePagerAdapter(ImageUrl));
                pager.setCurrentItem(0);
                // ImageView imgselect=(ImageView)dialog.findViewById(R.id.Image_dialog);
                 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                dialog.show();
                //imageLoader.displayImage(temp, imgselect,options);


                }
            });

        ChiDuong.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        return convertView;

    }


    public void Gson(String url){

        String json ="";
          try {
             json = readUrl(url);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          if(json!="" && json!=null){
            Gson gson = new Gson();   
            Photos_List page = gson.fromJson(json, Photos_List.class); 
            List<Photos_Json> temp=page.place.Photos;
            int i=0;
            for(Photos_Json a:temp){
                ImageUrl.add(a.Link);
            }


          }
          gallery.setAdapter(new ImageGalleryAdapter1());

            gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position,
                        long id) {



                }
            });
    }
      public static String readUrl(String urlString) throws Exception {
            BufferedReader reader = null;
            try {
                URL url = new URL(urlString);
                reader = new BufferedReader(new InputStreamReader(url.openStream()));
                StringBuffer buffer = new StringBuffer();
                int read;
                char[] chars = new char[1024];
                while ((read = reader.read(chars)) != -1)
                    buffer.append(chars, 0, read); 

                return buffer.toString();
            } finally {
                if (reader != null)
                    reader.close();
            }
        }

      ImageLoader imageLoader;
      DisplayImageOptions options;
      private class ImageGalleryAdapter1 extends BaseAdapter {

          private int[] imageUrls1={R.drawable.ic_home,R.drawable.bestbuy};

          @Override
            public int getCount() {
                return ImageUrl.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {



                ImageView imageView = (ImageView) convertView;
                if (imageView == null) {
                    imageView = (ImageView)context.getLayoutInflater().inflate(R.layout.item_gallery_image, parent, false);
                }
                  options = new DisplayImageOptions.Builder()
                    .cacheInMemory(true)
                    .cacheOnDisc(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();
                 imageLoader=ImageLoader.getInstance();
                File cacheDir = StorageUtils.getCacheDirectory(context);
                ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
                        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
                        .taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
                        .taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)
                        .threadPoolSize(3) // default
                        .threadPriority(Thread.NORM_PRIORITY - 1) // default
                        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                        .denyCacheImageMultipleSizesInMemory()
                        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // default
                        .memoryCacheSize(2 * 1024 * 1024)
                        .discCache(new UnlimitedDiscCache(cacheDir)) // default
                        .discCacheSize(50 * 1024 * 1024)
                        .discCacheFileCount(100)
                        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
                        .imageDownloader(new BaseImageDownloader(context)) // default
                        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                        .build();
                imageLoader.init(config);
                imageLoader.displayImage(ImageUrl.get(position), imageView, options);
                //imageView.setImageResource(imageUrls[position]);
                return imageView;
            }
        }
        private class ImagePagerAdapter extends PagerAdapter {

            private ArrayList<String> images=new ArrayList<String>();
            private LayoutInflater inflater;

            ImagePagerAdapter(ArrayList<String> images) {
                this.images = images;
                // inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return images.size();
            }

            @Override
            public Object instantiateItem(ViewGroup view, int position) {
                 LayoutInflater inflater = (LayoutInflater) view.getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View imageLayout = inflater.inflate(R.layout.item_paper_image, view, false);
                assert imageLayout != null;
                ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
                final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

                imageLoader.displayImage("R.drawable.bestbuy", imageView, options);

                view.addView(imageLayout, 0);
                return imageLayout;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view.equals(object);
            }

            @Override
            public void restoreState(Parcelable state, ClassLoader loader) {
            }

            @Override
            public Parcelable saveState() {
                return null;
            }
}
}   

item_paper_image.xml

  

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
     />

<ProgressBar
    android:id="@+id/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="gone" />
     

custom_dialog.xml

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" > <android.support.v4.view.ViewPager
 xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/pager"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" /> 
</RelativeLayout>

0 个答案:

没有答案
相关问题