从按钮设置为墙纸单击在Android中使用ViewPager适配器?

时间:2014-05-21 09:39:42

标签: android android-viewpager wallpaper

我想在buttonclick上设置壁纸是currenton viewpager imageview但是按钮不在视图寻呼机中,所以如何从viewpager适配器设置壁纸并且按钮不在imageview中

  

onclick我要设置为墙纸

 imagebutton.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {

                            // coding for wall set as wallpaper

                int b=pager.getCurrentItem();

             WallpaperManager myWallpaperManager=WallpaperManager. getInstance(getApplicationContext());
                                            myWallpaperManager.setResource(id); 

            }
        });
CopyOfClickedImagesActivity .java
public class CopyOfClickedImagesActivity extends BaseActivity 
{
        ArrayList<HashMap<String,String>> mylist = new ArrayList<HashMap<String,String>>();

        public static final String TAG_STATUS="status";
        public static final String TAG_DATA="data";
        public static final String TAG_ID="id";
        public static final String TAG_CATEGORYNAME="category_name";
        public static final String TAG_IMAGENAME="image_name";
        public static final String TAG_SETID="set_id";


        DisplayImageOptions options;

        ViewPager pager;
        ImageView imagebutton;
        ImagePagerAdapter pagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clickedimage);
        pager=(ViewPager)findViewById(R.id.myviewpager);
        imagebutton=(ImageView)findViewById(R.id.imagebutton);


        BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));

        options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.ic_launcher)
        .showImageOnFail(R.drawable.ic_launcher)
        .resetViewBeforeLoading(true)
        .cacheOnDisk(true)
        .imageScaleType(ImageScaleType.EXACTLY)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .considerExifParams(true)
        .displayer(new FadeInBitmapDisplayer(300))
        .build();


        imagebutton.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {

                            // coding for wall set as wallpaper

                int b=pager.getCurrentItem();

             WallpaperManager myWallpaperManager=WallpaperManager. getInstance(getApplicationContext());
                                            myWallpaperManager.setResource(id); 

            }
        });


    }

public class GetImagesAsync extends AsyncTask<String, ArrayList<HashMap<String,String>>, ArrayList<HashMap<String,String>>>
    {


        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... params) 
        {
            JsonParser json = new JsonParser();

            String url="myurlhere";
            String data=json.getdata(url);

            try 
            {
                JSONObject jobject= new JSONObject(data);
                String status=jobject.get(TAG_STATUS).toString();
                if(status.equalsIgnoreCase("1"))
                {
                    JSONArray jarray = jobject.getJSONArray(TAG_DATA);

                    for(int i=0;i<jarray.length();i++)
                    {
                        String id =jarray.getJSONObject(i).get(TAG_ID).toString();
                        String category=jarray.getJSONObject(i).get(TAG_IMAGENAME).toString();

                        HashMap<String, String> map = new HashMap<>();
                        map.put(TAG_ID,id);
                        map.put(TAG_IMAGENAME, category);

                        mylist.add(map);

                    }


                }


            } 
            catch (JSONException e) 
            {
                e.printStackTrace();

            }

            Log.e("ClickedImagesActivity","Data-------"+data);

            return mylist;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) 
        {

            pagerAdapter=new ImagePagerAdapter(result);
            //pager.setAdapter(new ImagePagerAdapter(result));
            pager.setAdapter(pagerAdapter);

        }

    }

    private class ImagePagerAdapter extends PagerAdapter 
    {

        //private String[] images;
        ArrayList<HashMap<String,String>> images;
        private LayoutInflater inflater;

        ImagePagerAdapter(ArrayList<HashMap<String,String>> images) 
        {
            this.images = images;
            inflater = getLayoutInflater();
        }


        @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) {
            View imageLayout = inflater.inflate(R.layout.imagelayout, view, false);
            assert imageLayout != null;

            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            imageView.setBackgroundResource(R.drawable.rounded_corner);
            imageView.setScaleType(ScaleType.FIT_XY);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            imageView.setTag(position);


            imageLoader.displayImage(images.get(position).get(TAG_IMAGENAME), imageView, options, new SimpleImageLoadingListener() 
            {
                @Override
                public void onLoadingStarted(String imageUri, View view) 
                {
                    spinner.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                            break;
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }


                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                }
            });

            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;
        }
    }

0 个答案:

没有答案