a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset.
Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById() lookups.
內建的 layout manager:
LinearLayoutManager
GridLayoutManager
StaggeredGridLayoutManager
custom(extends RecyclerView.LayoutManager)
Animations
extends RecyclerView.ItemAnimator
RecyclerView.setItemAnimator() method.
Example
RecyclerView layout
1 2 3 4 5 6
<!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
// use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true);
// use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example) mAdapter = new MyAdapter(myDataset); mRecyclerView.setAdapter(mAdapter); } ... }
// Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder publicstaticclassViewHolderextendsRecyclerView.ViewHolder{ // each data item is just a string in this case public TextView mTextView; publicViewHolder(TextView v){ super(v); mTextView = v; } }
// Provide a suitable constructor (depends on the kind of dataset) publicMyAdapter(String[] myDataset){ mDataset = myDataset; }
// Create new views (invoked by the layout manager) @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); // set the view's size, margins, paddings and layout parameters ... ViewHolder vh = new ViewHolder(v); return vh; }
// Replace the contents of a view (invoked by the layout manager) @Override publicvoidonBindViewHolder(ViewHolder holder, int position){ // - get element from your dataset at this position // - replace the contents of the view with that element holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager) @Override publicintgetItemCount(){ return mDataset.length; } }
1.Extend the ViewOutlineProvider class. 2.Override the getOutline() method. 3.Assign the new outline provider to your view with the View.setOutlineProvider() method.
Clip Views
Clipping views 能讓你簡單地改變 view 的形狀。
views for consistency with other design elements or to change the shape of a view in response to user input.
You can clip a view to its outline area using the View.setClipToOutline() method or the android:clipToOutline attribute.
Only rectangle, circle, and round rectangle outlines support clipping, as determined by the Outline.canClip() method.
To clip a view to the shape of a drawable, set the drawable as the background of the view (as shown above) and call the View.setClipToOutline() method.
Clipping views is an expensive operation, so don’t animate the shape you use to clip a view. To achieve this effect, use the Reveal Effect animation.