RecycleView仿Grifview 左右滑动并实现右滑至最右边加载更多

时间:2022-12-22 22:24:12
package com.zaa.recycleview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
   ArrayList<String> datas=new ArrayList<>();	
   private RecyclerView recyclerView;
   private GridAdapter adapter;
   int start = 0 ;
   int limit =20; //每次加载数据量
      @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      recyclerView = (RecyclerView) findViewById(R.id.recycleview);
      GridLayoutManager manager = new GridLayoutManager(this,2);
      manager.setOrientation(LinearLayoutManager.HORIZONTAL);
      recyclerView.setLayoutManager(manager);
      recyclerView.addItemDecoration(new SpacesItemDecoration(20));
      adapter = new GridAdapter();
      recyclerView.setAdapter(adapter);
      loadData();
      recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
         @Override
         public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.e("infoo","newState:"+newState+"---"+recyclerView.computeHorizontalScrollExtent()+"----"+recyclerView.computeHorizontalScrollOffset()+"--"+recyclerView.computeHorizontalScrollRange());
            int se=recyclerView.computeHorizontalScrollExtent();
            int so=recyclerView.computeHorizontalScrollOffset();
            int sr=recyclerView.computeHorizontalScrollRange();
            if (newState == 0 && se + so == sr) {
               if (datas.size() == 100) {    //设置加载上限
                    Toast.makeText(getApplicationContext(),"数据已经加载完毕",Toast.LENGTH_SHORT).show();
                  return;
               }
               start+=limit;
               loadData();
            }
         }
         @Override
         public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
         }
      });
   }
   private void loadData(){
      for (int i = start; i < limit+start; i++) {
         datas.add("测试数据"+i);
      }
      adapter.notifyDataSetChanged();
   }
   class GridAdapter extends RecyclerView.Adapter<ViewHolder> {
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View view = View.inflate(parent.getContext(), R.layout.item_grid, null);
         return new ViewHolder(view);
      }
      @Override
      public void onBindViewHolder(final ViewHolder holder, final int position) {

         holder.textView.setText(datas.get(position));
      }
      @Override
      public int getItemCount() {
         return datas.size();
      }
   }
   class ViewHolder extends RecyclerView.ViewHolder {
      TextView textView;
      public ViewHolder(View itemView) {
         super(itemView);
         textView= (TextView) itemView.findViewById(R.id.tvContent);
      }
   }
}