package com.wangbb.naruto.app.fragment.childFragment; import android.graphics.Color; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.wangbb.naruto.R; import com.wangbb.naruto.app.activity.MainActivity; import com.wangbb.naruto.app.adapter.ChildOneAdapter; import com.wangbb.naruto.app.fragment.BaseFragment; import com.wangbb.naruto.app.view.LoadingViewHelper; import com.wangbb.naruto.utils.Utility; import java.util.ArrayList; /** * Created by wangbinbin on 15/5/9. */ public class ChildOneFragment extends BaseFragment { private RecyclerView mRecyclerView; private ArrayList<String> list; private SwipeRefreshLayout mSwipeRefreshLayout; private LinearLayoutManager mLayoutManager; private boolean canLoadMore = true; private LoadingViewHelper loadingView; private View view; private MainActivity mainActivity; private ChildOneAdapter adapter; private boolean isLoadingData = false; private boolean isInitData = false; private int page = 1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_child_one, container, false); initView(); setListerner(); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); this.mainActivity = (MainActivity) getActivity(); mSwipeRefreshLayout.setRefreshing(true); if (adapter == null) { adapter = new ChildOneAdapter(mainActivity, list); mRecyclerView.setAdapter(adapter); } mSwipeRefreshLayout.setRefreshing(false); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (!isInitData && isVisibleToUser) { initData(); isInitData = true; } } private void initView() { mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout); mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW); // mSwipeRefreshLayout.setProgressBackgroundColor(R.color.primary_dark_material_dark); mSwipeRefreshLayout.setProgressViewOffset(false, Utility.dip2px(0), Utility.dip2px(80)); mSwipeRefreshLayout.setHorizontalFadingEdgeEnabled(true); mSwipeRefreshLayout.setHapticFeedbackEnabled(true); mSwipeRefreshLayout.setHasTransientState(true); mSwipeRefreshLayout.setHovered(true); mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); //�������ȷ��ÿ��item�ĸ߶��ǹ̶��ģ��������ѡ������������ mRecyclerView.setHasFixedSize(false); mLayoutManager = new LinearLayoutManager(mainActivity); mRecyclerView.setLayoutManager(mLayoutManager); loadingView = new LoadingViewHelper(view.findViewById(R.id.loading_view)); } private void setListerner() { mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if (mRecyclerView != null && mRecyclerView.getAdapter() != null) mRecyclerView.getAdapter().notifyDataSetChanged(); mSwipeRefreshLayout.postDelayed(new Runnable() { @Override public void run() { page = 1; list.clear(); initData(); mSwipeRefreshLayout.setRefreshing(false); } }, 2000); } }); mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { int lastVisibleItem = mLayoutManager.findLastVisibleItemPosition(); int totalItemCount = mLayoutManager.getItemCount(); //lastVisibleItem >= totalItemCount - 4 ��ʾʣ��4��item�Զ����أ���λ����ѡ�� dy>0 ��ʾ���»��� if (!isLoadingData && lastVisibleItem >= totalItemCount - 2 && dy > 0) { isLoadingData = true; mRecyclerView.postDelayed(new Runnable() { @Override public void run() { page++; initData(); mRecyclerView.getAdapter().notifyDataSetChanged(); } }, 2000l); } } }); } protected void initData() { isLoadingData = false; if (list == null) list = new ArrayList<String>(); for (int i = 'A'; i < 'J'; i++) { list.add((char) i + " page = " + page); } if (mRecyclerView != null && mRecyclerView.getAdapter() != null) mRecyclerView.getAdapter().notifyDataSetChanged(); } }