Android Open Source - FragmentTutorial Left Fragment






From Project

Back to project page FragmentTutorial.

License

The source code is released under:

Apache License

If you think the Android project FragmentTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yanlu.android.fragment.frg;
// w w w .  j  a v  a  2s  .  c  om
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.yanlu.android.fragment.R;
import com.yanlu.android.fragment.model.DemoDataMo;
import com.yanlu.android.fragment.net.GsonRequest;
import com.yanlu.android.fragment.net.RequestManager;


public class LeftFragment extends Fragment implements Response.Listener<DemoDataMo>,
        Response.ErrorListener {
    private final static String TAG = "LeftFragment";

    private final String LEFT_URL = "http://tomp110.me/webapi/fragment/left";

    private Button mBtn;
    private TextView mTv;

    private OnFragmentInteractionListener mListener;

    private GsonRequest<DemoDataMo> request;
    private DemoDataMo mData;

    public static LeftFragment newInstance() {

        return new LeftFragment();
    }
    public LeftFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView(LayoutInflater, ViewGroup, Bundle) ");
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_left, container, false);

        mBtn = (Button) view.findViewById(R.id.btn_ok);
        mTv = (TextView) view.findViewById(R.id.content);
        updateView();
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onFragmentInteraction();
            }
        });
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume( )");
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "onAttach(activity)");
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "onDetach( )");
        mListener = null;
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        mTv.setText(error.toString());
    }

    @Override
    public void onResponse(DemoDataMo response) {
        mData = response;
        updateView();
    }

    private void updateView(){
        if (mData != null) {
            StringBuilder sb = new StringBuilder();
            for (String s : mData.value) {
                sb.append(s).append("\r\n");
            }
            mTv.setText(sb.toString());
        }
    }
    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction();
    }


    public void loadData(Response.Listener<DemoDataMo> listener, Response.ErrorListener errorListener) {
        request = new GsonRequest<DemoDataMo>(LEFT_URL, DemoDataMo.class, null, listener, errorListener);

        RequestManager.getRequestQueue().add(request);
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated(savedInstanceState)");
        if(mData == null){//???????????????
            loadData(this, this);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart( )");
    }


    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause( )");
    }

    @Override
    public void onStop() {
        super.onStop();
        if(request != null && !request.isCanceled()) {
            request.cancel();
        }
        Log.d(TAG, "onStop( )");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView( )");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy( )");
    }

}




Java Source Code List

com.yanlu.android.fragment.App.java
com.yanlu.android.fragment.MainActivity.java
com.yanlu.android.fragment.SettingsActivity.java
com.yanlu.android.fragment.frg.FragmentByXml.java
com.yanlu.android.fragment.frg.LeftFragment.java
com.yanlu.android.fragment.frg.RightFragment.java
com.yanlu.android.fragment.model.DemoDataMo.java
com.yanlu.android.fragment.model.DemoParcel.java
com.yanlu.android.fragment.net.GsonRequest.java
com.yanlu.android.fragment.net.LruBitmapCache.java
com.yanlu.android.fragment.net.RequestManager.java