recyclerview实现gridview效果,加载更多

package com.bruce.recyclerview03038;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;//用来加载的recyclerview
    private ArrayList<String> mArrayList = new ArrayList<>();
    private MyAdapter mAdapter;
    private GridLayoutManager mGridViewLayoutManger;
    private boolean isLoadingMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //设置数据源  100个item
        setData();
        //设置布局管理器 --GridLayout效果--2行
        mGridViewLayoutManger = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(mGridViewLayoutManger);
        //实例化适配器--
        mAdapter = new MyAdapter(this);
        //将适配器和RecyclerView绑定
        mRecyclerView.setAdapter(mAdapter);
        //给适配器设置数据源
        mAdapter.setData(mArrayList);
        mAdapter.setOnItemClickLitener(new MyAdapter.OnItemClickLitener() {

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, mArrayList.get(position) + " click",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();
                mAdapter.removeData(position);
            }
        });
    }

    /**
     * 设置数据源-添加100个 item
     */
    public void setData() {
        for (int i = 0; i < 100; i++) {
            mArrayList.add("item" + i);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((GridLayoutManager) mGridViewLayoutManger).findLastVisibleItemPosition();
                int totalItemCount = mGridViewLayoutManger.getItemCount();
//                lastVisibleItem >= totalItemCount - 4 // 表示剩下4个item自动加载,各位自由选择
                // dy>0 表示向下滑动
                if (lastVisibleItem >= totalItemCount - 4 && dy > 0) {
                    if (isLoadingMore) {
                        Log.d("", "ignore manually update!");
                    } else {
//                        loadPage();//这里多线程也要手动控制isLoadingMore
                        Toast.makeText(MainActivity.this, "MainActivity>>loadPage", Toast.LENGTH_SHORT).show();
                        isLoadingMore = false;
                    }
                }
            }
        });
    }
}
<pre name="code" class="java">package com.bruce.recyclerview03038;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/3/8.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    //
    private Context mContext;
    private ArrayList<String> mArrayList = new ArrayList<>();

    public MyAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(ArrayList<String> mArrayList) {
        this.mArrayList = mArrayList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_home, parent,
                false));
        return holder;
    }
    public void addData(int position) {
        mArrayList.add(position, "Insert One");
        notifyItemInserted(position);
    }

    public void removeData(int position) {
        mArrayList.remove(position);
        notifyItemRemoved(position);
    }
    //...
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(mArrayList.get(position));

        // 如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.id_num);
        }
    }
}


 
 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


你可能感兴趣的:(recyclerview实现gridview效果,加载更多)