package com.wangbb.naruto.app.activity; import android.graphics.Color; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.MotionEvent; import android.view.View; import com.wangbb.naruto.R; import com.wangbb.naruto.app.adapter.HomeAdapter; import com.wangbb.naruto.app.view.DividerGridItemDecoration; import com.wangbb.naruto.utils.NarutoToast; import java.util.ArrayList; /** * Created by wangbinbin on 15/5/7. */ public class NewGridViewActivty extends BaseFragmentActivity { private RecyclerView mRecyclerView; private ArrayList<String> list; private SwipeRefreshLayout mSwipeRefreshLayout; private LinearLayoutManager mLayoutManager; private boolean canLoadMore = true; private Toolbar toolbar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gridview); list = new ArrayList<String>(); initView(); setListerner(); initData(); } private void setListerner() { mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Thread() { @Override public void run() { try { Thread.sleep(2000l); } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(false); } }); } }.start(); } }); 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 (lastVisibleItem >= totalItemCount - 4 && dy > 0) { initData();//������߳�ҲҪ�ֶ�����isLoadingMore } } }); } private void initView() { mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout); mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); //�������ȷ��ÿ��item�ĸ߶��ǹ̶��ģ��������ѡ������������ mRecyclerView.setHasFixedSize(false); mLayoutManager = new GridLayoutManager(this, 2); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setAdapter(new HomeAdapter(this, list)); mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this)); mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { return false; } @Override public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { NarutoToast.toastShow("touch " + motionEvent.getAction()); } }); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("like gridview"); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.arrow_left); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } protected void initData() { for (int i = 'A'; i < 'z'; i++) { list.add("" + (char) i); } mRecyclerView.getAdapter().notifyDataSetChanged(); } }