Java tutorial
/* * * * Copyright 2015 Van Shu * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * */ package com.mobimvp.cliques.ui.fragment; import android.app.Activity; import android.app.ListFragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import com.android.volley.Response; import com.mobimvp.cliques.R; import com.mobimvp.cliques.model.Shot; import com.mobimvp.cliques.service.DribbbleApi; import com.mobimvp.cliques.service.GsonRequest; import com.mobimvp.cliques.ui.adapter.ShotsAdapter; import com.mobimvp.cliques.ui.widgets.ListViewUtils; import com.mobimvp.cliques.ui.widgets.LoadingFooter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import static com.mobimvp.cliques.util.LogUtils.makeLogTag; public class ShotsFragment extends BaseFragment implements AdapterView.OnItemClickListener { private static final String TAG = makeLogTag(ShotsFragment.class); public static final String POPULAR = "comments"; public static final String RECENT = "recent"; public static final String VIEWS = "views"; private String sort; private ArrayList<Shot> shots; private ShotsAdapter mShotsAdapter; private View mContentView; private ListView mListView; private LoadingFooter mLoadingFooter; protected int mPage = 1; public interface Listener { public void onFragmentViewCreated(ShotsFragment fragment); public void onFragmentAttached(ShotsFragment fragment); public void onFragmentDetached(ShotsFragment fragment); } @Override public void onAttach(Activity activity) { super.onAttach(activity); if (getActivity() instanceof Listener) { ((Listener) getActivity()).onFragmentAttached(this); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sort = getArguments().getString("sort"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.fragment_shots, container, false); mListView = (ListView) mContentView.findViewById(R.id.listview); mListView.setOnItemClickListener(this); View header = new View(getActivity()); mLoadingFooter = new LoadingFooter(getActivity()); mListView.addHeaderView(header); mListView.addFooterView(mLoadingFooter.getView()); shots = new ArrayList<>(); mShotsAdapter = new ShotsAdapter(shots); mListView.setAdapter(mShotsAdapter); mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (mLoadingFooter.getState() == LoadingFooter.State.Loading || mLoadingFooter.getState() == LoadingFooter.State.TheEnd) { return; } if (firstVisibleItem + visibleItemCount >= totalItemCount && totalItemCount != 0 && totalItemCount != mListView.getHeaderViewsCount() + mListView.getFooterViewsCount() && mListView.getCount() > 0) { loadNextPage(); } } }); loadFirstPage(); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (getActivity() instanceof Listener) { ((Listener) getActivity()).onFragmentViewCreated(this); } } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setContentView(mContentView); } @Override public void onDetach() { super.onDetach(); if (getActivity() instanceof Listener) { ((Listener) getActivity()).onFragmentDetached(this); } } private Response.Listener<Shot[]> listener = new Response.Listener<Shot[]>() { @Override public void onResponse(Shot[] response) { if (response == null) { setEmptyView(); } else { showResult(); shots.addAll(Arrays.asList(response)); mShotsAdapter.notifyDataSetChanged(); mLoadingFooter.setState(LoadingFooter.State.Idle); } } }; public void loadFirstPageAndScrollToTop() { ListViewUtils.smoothScrollListViewToTop(mListView); loadFirstPage(); } protected void resetData() { mPage = 1; shots.clear(); } public boolean canCollectionViewScrollUp() { return ViewCompat.canScrollVertically(mListView, -1); } protected void loadNextPage() { mLoadingFooter.setState(LoadingFooter.State.Loading); loadData(mPage + 1); mPage++; } protected void loadFirstPage() { loadData(1); } private void loadData(int page) { addRequest(new GsonRequest<>(DribbbleApi.getShots(page, sort), Shot[].class, listener, errorListener)); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } public ListView getListView() { return mListView; } }