加载片段android最佳实践

时间:2016-05-07 07:22:59

标签: android fragment

我有一个从API加载数据的片段。我想在Google Play应用中显示加载进度,并在完成后隐藏或删除它。 首先,我不想使用其他库(http://johnkil.github.io/Android-ProgressFragment/)。我有很多代码。我创建了一个显示加载的基本片段。但我在自己的班级中实例化它。以这种方式工作是好事。但是加载实现效果很好。我的加载片段是:

public class SpinnerFragment extends Fragment {

private SpinnerFragment spinnerFragment;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    spinnerFragment = new SpinnerFragment();
}

/**
 * Detach from view.
 */
@Override
public void onDestroyView() {
    super.onDestroyView();
}

protected void addSpinner() {
    if (!spinnerFragment.isAdded()) {
        getFragmentManager().beginTransaction().add(R.id.content_frame, spinnerFragment)
                .commit();
    }
}

protected void removeSpinner() {
    if (spinnerFragment.isAdded()) {
        getFragmentManager().beginTransaction().remove(spinnerFragment).commit();
    }
}
}

我正在使用这个课程如下:

public class List extends SpinnerFragment implements View.OnClickListener {

    private ListAdapter listAdapter;
    private ArrayList<ListRowItem> arrayList;
    private ListView listView;



    public List() {

    }

    public static List newInstance() {
        List fragment = new List();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(getActivity()));
        setRetainInstance(true);
addSpinner();
       //loadDataFromWeb
String         URL = new SqLite(getActivity()).returnDefaultURI() + "notice/"
        new AsyncRequest(onAsyncRequestComplete, getActivity(), "GET", null,
                null, 1).execute(URL);
    }

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

        return init(inflater.inflate(R.layout.notice_activity, container, false));
    }

    private View init(View view) {

        listView = (ListView) view.findViewById(R.id.content_container);
        listView.setOnScrollListener(onScrollListener());
        footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.listview_loading_footer, listView, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (listAdapter == null && arrayList == null) {
            arrayList = new ArrayList<>();
            listAdapter = new ListAdapter(getActivity(), arrayList);

            listView.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();

        }

    }

    AsyncRequest.OnAsyncRequestComplete onAsyncRequestComplete = new AsyncRequest
            .OnAsyncRequestComplete() {
        @Override
        public void asyncResponse(final String response, int apiKey) {
            switch (apiKey) {
                case 1:

                            //loading complete 
    listViewBinding(reasponse);
    removeSpinner();


                    break;

            }
        }

    };

}

0 个答案:

没有答案