初步使用RecyclerView实现瀑布流

先看效果

初步使用RecyclerView实现瀑布流_第1张图片

 

 

关于RecyclerView,真的是很强大。

个人觉得主要方便的地方是

1.直接可以设置条目布局,通过setLayoutManager

  

  1. LinearLayoutManager:线性布局,横向或者纵向滑动列表
  2. GridLayoutManager:表格布局
  3. StaggeredGridLayoutManager:流式布局,例如瀑布流效果

2.可以直接设置分割线       addItemDecoration方法

3.直接设置添加删除item动画   setItemAnimator方法

4.对View的复用性好  (这个是别人总结的,我还没看源码)

5.对于分割线,和增删动画,github上开源的类很多

 

 

下面看看怎么用法

   一 导入包,v7的recyclerview包

  二 布局

 

activity_main.xml


    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="com.example.myrecyclerview.MainActivity" >

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

item布局



    android:layout_width="match_parent"
    android:background="#8A2BE2"
    android:layout_margin="3dp"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="1" />


 

三,创建Adapter

 

package com.example.myrecycleviewdemo;

import java.util.ArrayList;
import java.util.List;

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

public class MyAdapter extends RecyclerView.Adapter{
    Context mcontext;
    List mlist;
    List mheight;
    public MyAdapter(Context context, List list) {
        mcontext=context;
        mlist=list;
        //随机高度集合
        mheight=new ArrayList();
        for(int i=0;i){
            mheight.add((int)(100+Math.random()*300));
        }
    }
    
    


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


    //找到布局中空间位置
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv;
        public MyViewHolder(View arg0) {
            super(arg0);
            
            tv=(TextView) arg0.findViewById(R.id.id_num);
        }
        
    }


    //绑定,渲染数据到view中
    @Override
    public void onBindViewHolder(MyViewHolder holder, int arg1) {
        
        LayoutParams lp=holder.tv.getLayoutParams();
        lp.height=mheight.get(arg1);
        holder.tv.setLayoutParams(lp);
        holder.tv.setText(mlist.get(arg1));
        
        
        
    }



    //先执行onCreateViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
        
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
               mcontext).inflate(R.layout.item, parent,
                false));
        return holder;
    }




    public void add(int pos) {
    
        mlist.add(pos, "insert");
        mheight.add((int)(100+Math.random()*300));
        notifyItemInserted(pos);
    }




    public void del(int pos) {
        
        mlist.remove(pos);
        notifyItemRemoved(pos);
    }


}

这里的Adapter需要继承RecyclerView.Adapter,需要实现3个方法:

- onCreateViewHolder()

- onBindViewHolder()

- getItemCount()

 

最后,MainActivity

package com.example.myrecycleviewdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
    RecyclerView recyclerView;
    List mlist;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        
        initview();
        
    }
    private void initview() {
        recyclerView=(RecyclerView) findViewById(R.id.recyclerview_vertical);
        adapter=new MyAdapter(this,mlist);
        //设置动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置分割线
        //recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置布局
         recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
    }
    private void initData() {
        mlist=new ArrayList();
        for(int i=0;i<50;i++){
            mlist.add("number"+i);
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        
        switch (item.getItemId())
        { 
        case R.id.add:
            adapter.add(1);
            break;
        case R.id.del:
            adapter.del(1);
            break;
        }
    
        return true;
    }
}

更换布局,分割线,动画的方式很简单,直接通过方法让不同的类当参数。

 

转载于:https://www.cnblogs.com/xurui1995/p/5745069.html

你可能感兴趣的:(初步使用RecyclerView实现瀑布流)