RecyclerView achieved waterfall effect

the extends the MainActivity AppCompatActivity {class public 

    / ** 
     * multiplexed view control 
     * / 
    Private RecyclerView recyclerView; 

    / ** 
     * display data 
     * / 
    Private the ArrayList <String> mDatas; 

    / ** 
     * RecyclerView adapters 
     * / 
    Private MyRecyclerViewAdapter Adapter; 

    @override 
    protected void the onCreate (the Bundle savedInstanceState) { 
        super.onCreate (savedInstanceState); 
        the setContentView (R.layout.activity_main); 
        .. 1 // find the control 
        recyclerView = (RecyclerView) the findViewById (R.id.recyclerview); 
        // 2. layout reputation as a cascade: 3, the vertical direction
        = New new StaggeredGridLayoutManager staggeredGridLayoutManager StaggeredGridLayoutManager (3, StaggeredGridLayoutManager.VERTICAL); 
        // 3 is a layout manager is provided recyclerView. 
        RecyclerView.setLayoutManager (staggeredGridLayoutManager); 
        initData (); // initialize the data 
        . 3 // Create adapter 
        adapter = new MyRecyclerViewAdapter (this , mDatas); 
        // settings to add, remove animated item is, DefaultItemAnimator the default 
        recyclerView.setItemAnimator (new new DefaultItemAnimator ()); 
        // 4 set adapter. 
        recyclerView.setAdapter (adapter); 

        // add a click event 
        adapter.setOnItemClickListener (new new MyRecyclerViewAdapter.OnRecyclerItemClickListener () { 
            @Override
            void in the onItemClick public (View View, int position) { 
                //Toast.makeText(MainActivity.this, "clicked:" + mDatas.get (position), Toast.LENGTH_SHORT) the .Show (); 
                adapter.addItem (position, "Add content"); 
                Log.i ( "Tag", "in the onItemClick:" + position); 
                Log.i ( "Tag", "set:" + mDatas.toString ()); 
            } 
        }); 
        // set long press event 
        adapter.setOnItemLongClickListener (new new MyRecyclerViewAdapter.onRecyclerItemLongClickListener () { 
            @Override 
            public void onItemLongClick (View View, int position) { 
                //Toast.makeText(MainActivity.this, "the press:" + mDatas.get (position) , Toast.LENGTH_SHORT).show();
                adapter.removeItem(position);
                Log.i("tag", "onItemLongClick: "+position);
                Log.i("tag", "集合: "+mDatas.toString());
            }
        });

    }

    //初始化数据
    protected void initData(){
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++){
            mDatas.add("" + (char) i);
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/sunjian43792901/p/11130605.html