RecyclerView-点击切换多种布局

布局main

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

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

            android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

代码   
MainActivity
 
  
private RecyclerView mRecyclerView;
private List mList;
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    adapter = new MyAdapter();
    mRecyclerView.setAdapter(adapter);
    mList = new ArrayList<>();
    for (int i = 0; i < 130; i++) {
        mList.add("数据" + i);
    }
}
public void Click(View view){
    switch (view.getId()){
        case R.id.button_tianjia:
            mList.add("添加一个");
            adapter.notifyDataSetChanged();
            break;
        case R.id.button_shanchu:
            mList.remove(1);
            adapter.notifyDataSetChanged();
            break;
        case R.id.button_xianxing:
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            break;
        case R.id.button_wangge:
            mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
            break;
        case R.id.button_pubu:
            mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

            break;
    }
}
 
适配器   
MyAdapter 
private class MyAdapter extends RecyclerView.Adapter {

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(MainActivity.this, R.layout.item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textview.setText(mList.get(position));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.textView);
        }
    }
}
线性布局上拉加载更多
 
  
//上拉加载
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

     //设置加载的状态
       // adapter.changeMoreStatus(RecyclerViewAdapter.LOADING_MORE);
        //判断到底部的条件
        if (newState == RecyclerView.SCROLL_STATE_IDLE && linearLayoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
            Log.e("--------->", "到底");
            //可以直接addAll
            for (int i = 0; i < 30; i++) {
                mList.add("测试数据hhhhhhh" + i);
            }

          //刷新数据
            adapter.notifyDataSetChanged();
           
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    }
});



你可能感兴趣的:(RecyclerView-点击切换多种布局)