Android LoaderCallbacks

在Fragment里面使用LoaderCallbacks接口 异步HTTP获取图片

public abstract class AbstractLoader<D> extends AsyncTaskLoader<D> {

    public AbstractLoader(Context context) {
        super(context);
    }

    @Override
    protected void onStartLoading() { //会自动调用  step2
        super.onStartLoading();
        forceLoad();//开始执行任务
    }
}


public class BitmapLoader extends AbstractLoader<Bitmap> {

    static final String IMG_URL = "http://h.hiphotos.baidu.com/image/pic/item/dcc451da81cb39db599abb5bd2160924ab183061.jpg";

    public BitmapLoader(Context context) {
        super(context);
    }

    @Override
    public Bitmap loadInBackground() { //后台执行任务 step3
        InputStream ins = HTTPHelper.httpGet(IMG_URL);
        return BitmapFactory.decodeStream(ins);
    }

}

public final class HTTPHelper {

    public HTTPHelper() {
    }

    public static InputStream httpGet(String url) {
        HttpURLConnection connection = (HttpURLConnection) createConnection(url, 5000);
        try {
            if (connection != null && connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return connection.getInputStream();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static URLConnection createConnection(String url, int timeout) {
        try {
            URLConnection urlConnection = (URLConnection) new URL(url).openConnection();
            urlConnection.setConnectTimeout(timeout);
            urlConnection.setReadTimeout(timeout);
            urlConnection.setDoInput(true);
            return urlConnection;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

在Frament中使用


public class MyFragment extends Fragment implements LoaderCallbacks<Bitmap>{

    static final String TAG = "MyFragment";
    private ImageView imageView;

    public MyFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_img_layout, null);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG,"onViewCreated>>>" + getActivity());
        imageView = (ImageView) view.findViewById(R.id.img);
        //初始化任务执行 step1
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override //创建异步加载Loader
    public Loader<Bitmap> onCreateLoader(int arg0, Bundle b) {
        return new BitmapLoader(getActivity());
    }

    @Override //任务执行完成后 直接更新UI  step4
    public void onLoadFinished(Loader<Bitmap> arg0, Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onLoaderReset(Loader<Bitmap> loader) {

    }

}



public class MainActivity extends FragmentActivity {

    static String TAG = MainActivity.class.getCanonicalName();

    public MainActivity() {
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new MyFragment()).addToBackStack(null).commit();



    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。