volley+viewpagaer+fragment实现左右滑动加载图片



首先、主布局是一个viewpager




    


在activity中
package com.bruce.brucedemo.fragement.httpviewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.bruce.brucedemo.R;

public class ImageLoadActivity extends FragmentActivity {
    private ViewPager mViewPager;
    private ImageLoadFragment mFragment;
    private ImageLoadAdapter mAdapter;

    private String[] imageThumbUrls = new String[]{             //数据源
            "https://img-my.csdn.net/uploads/201407/26/1406383299_1976.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383291_6518.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383291_8239.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383290_9329.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383290_1042.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383275_3977.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383264_3954.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383264_4787.jpg",
    };

    /**
     * 创建view
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        View view = LayoutInflater.from(this).inflate(R.layout.activity_image_load_layout,null);
//        setContentView(view);
        setContentView(R.layout.activity_image_load_layout);
        /**
         *得到控件
         */
        mViewPager = (ViewPager) findViewById(R.id.image_load_viewpager);
        /**
         * 实例化适配器
         */
        mAdapter = new ImageLoadAdapter(getSupportFragmentManager());/**得到了适配器**/
        mViewPager.setAdapter(mAdapter);/**将控件和适配器关联**/
        mAdapter.setDate(imageThumbUrls);/**为适配器设置数据源**/
    }

    /**
     * 切换fragment的pageradapter 适配器
     */
    class ImageLoadAdapter extends FragmentStatePagerAdapter {
        private String[] imageurls = new String[]{};

        public ImageLoadAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * 设置数据源
         *
         * @param urls
         */
        public void setDate(String[] urls) {
            this.imageurls = urls;
            notifyDataSetChanged();
        }

        /**
         * 每次加载两个
         *
         * @param position
         * @return
         */
        @Override
        public Fragment getItem(int position) {
            mFragment = ImageLoadFragment.newInstance(imageurls[position]);
            return mFragment;
        }

        @Override
        public int getCount() {
            return imageurls.length;
        }
    }

}
然后是写fragment的布局



    

    


fragment的类,在里面操作。

package com.bruce.brucedemo.fragement.httpviewpagerfragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.bruce.brucedemo.R;
import com.bruce.brucedemo.utils.Logs;

public class ImageLoadFragment extends Fragment {
    private ImageView mImageView;// 显示图片控件
    private String mUrl;//图片的url地址
    private static ProgressBar mProgressBar;
    private static String URL = "URL";//取图片的key

    /**
     * 通过此方法得到ImageLoadFragment对象
     *
     * @param url
     * @return
     */
    public static ImageLoadFragment newInstance(String url) {

        Bundle args = new Bundle();
        //把传过来的url通过Bundle传递
        args.putString(URL, url);

        ImageLoadFragment fragment = new ImageLoadFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 再次方法中得到图片的url
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Bundle bundle = getArguments();
        mUrl = bundle.getString(URL);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment_image_load_layout, container, false);
        //得到控件
        mImageView = (ImageView) view.findViewById(R.id.load_image_view);
        mProgressBar = (ProgressBar) view.findViewById(R.id.imageload_progressbar);
        /**
         * 通过url地址得到图片,加到imageview上面
         */
        Logs.w("murl?>>>" + mUrl);
        Logs.w("mImageView?>>>" + mImageView);
        // Picasso.with(getContext()).load(mUrl).into(mImageView);
//        Picasso.with(getContext()).load(mUrl).into(mImageView, new Callback() {
//            @Override
//            public void onSuccess() {
//                mProgressBar.setVisibility(View.GONE);
//            }
//            @Override
//            public void onError() {
//
//            }
//        });
        /**
         * 使用volley加载图片到imageview上
         */
        //得到请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        //构造图片请求imageRequest
        ImageRequest imageRequest = new ImageRequest(mUrl, new Response.Listener() {
            /**
             * 成功了就将图片显示,让进度条消失
             * @param bitmap
             */
            @Override
            public void onResponse(Bitmap bitmap) {
                if (bitmap != null) {
                    mImageView.setImageBitmap(bitmap);
                    mProgressBar.setVisibility(View.INVISIBLE);
                }

            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getActivity(), volleyError.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        imageRequest.setShouldCache(true);//设置缓存
        requestQueue.add(imageRequest);//将请求添加到请求队列里面
        return view;
    }

}



你可能感兴趣的:(android进阶)