recyclerView
流程:
实例化view
准备数据
写adapter
实例化adapter
设置布局管理
创建layoutmanager
view.setlayoutmanager
添加数据列
notifyItemInsert(postion);
删除数据列
notifyItemRemove(postion);
重写recycler.adapter
横向gridview 宽度变短,可以设置margin
设置瀑布流中textview的高度时,方法如下:
添加分割线
dividerItemDecoration.java
RecyclerView添加分割线但无法显示: 在构建DividerItemDecoration时不要使用context,直接使用Activity即可解决
设置添加删除动画
流程:
实例化view
准备数据
写adapter
实例化adapter
设置布局管理
创建layoutmanager
view.setlayoutmanager
添加数据列
notifyItemInsert(postion);
删除数据列
notifyItemRemove(postion);
重写recycler.adapter
横向gridview 宽度变短,可以设置margin
设置瀑布流中textview的高度时,方法如下:
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int i) {
ViewGroup.LayoutParams lp = viewHolder.itemView.getLayoutParams();
lp.height = mHeights.get(i);
viewHolder.itemView.setLayoutParams(lp);
viewHolder.tv.setText(mDatas.get(i));
}
添加分割线
dividerItemDecoration.java
RecyclerView添加分割线但无法显示: 在构建DividerItemDecoration时不要使用context,直接使用Activity即可解决
设置添加删除动画
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerAdapter adapter;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add("第" + i + "条数据");
}
recyclerView = (RecyclerView) findViewById(R.id.recycle);
ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
//获取裁剪的尺寸
int fabSize = getResources().getDimensionPixelSize(R.dimen.fab_size);
//设置轮廓的尺寸
outline.setOval(-4, -4, fabSize + 2, fabSize + 2);
}
};
View fabView = findViewById(R.id.fab_add);
View fabdel = findViewById(R.id.fab_del);
fabView.setOutlineProvider(viewOutlineProvider);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
//设置布局管理器,默认垂直方向
recyclerView.setLayoutManager(linearLayoutManager);
//设置间隔线样式
recyclerView.addItemDecoration(new SampleDivider(this,LinearLayoutManager.VERTICAL));
adapter = new RecyclerAdapter(data);
recyclerView.setAdapter(adapter);
fabView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取第一个可视item的位置
int position = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
//在该位置添加新的item
adapter.addItem(position);
}
});
fabdel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取第一个可视item的位置
int position = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
//在该位置删除新的item
adapter.removeData(position);
}
});
}
/*适用于 RecycleView的LineaLayoutManager*/
class LinearDividerItemDecoration extends RecyclerView.ItemDecoration {
private int[] attrs = {android.R.attr.listDivider};
private Drawable mDividerDrawable;
private int mOri;
public LinearDividerItemDecoration(Context context, int ori) {
TypedArray typedArray = context.obtainStyledAttributes(attrs);
mDividerDrawable = typedArray.getDrawable(0);
this.mOri = ori;
typedArray.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (mOri == LinearLayoutManager.VERTICAL) {
//画水平线 上下左右的每条分割线都看成一个小矩形,绘制每个item的矩形分割线
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getLeft() - params.leftMargin;
int top = child.getBottom() + params.bottomMargin;
int right = child.getRight() + params.rightMargin;
int bottom = top + mDividerDrawable.getIntrinsicHeight();
mDividerDrawable.setBounds(left, top, right, bottom);
mDividerDrawable.draw(c);
}
} else if (mOri == LinearLayoutManager.HORIZONTAL) {
//画垂直线
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getRight() + params.rightMargin;
int right = left + mDividerDrawable.getIntrinsicWidth();
int top = child.getTop() - params.topMargin;
int bottom = child.getBottom() + params.bottomMargin;
mDividerDrawable.setBounds(left, top, right, bottom);
mDividerDrawable.draw(c);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//获取每个item上下左右四边向外的偏移量 上下左右的每条分割线都看成一个小矩形
if (mOri == LinearLayoutManager.HORIZONTAL) {
outRect.set(0, 0, mDividerDrawable.getIntrinsicWidth(), 0);
} else if (mOri == LinearLayoutManager.VERTICAL) {
outRect.set(0, 0, 0, mDividerDrawable.getIntrinsicHeight());
}
}
}
}
RecyclerAdapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private List<String> data;
public RecyclerAdapter(List<String> data) {
this.data = data;
}
/**
* 用于创建控件
* @param parent
* @param viewType
* @return
*/
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//获取列表项控件
View v=View.inflate(parent.getContext(),R.layout.item_recyler,null);
return new MyViewHolder(v);
}
/**
* 为控件设置数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(data.get(position));
holder.itemView.setTag(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public void removeData(int position){
data.remove(position);
//通知recylerview控件某个item即将被删除
notifyItemRemoved(position);
}
public void addItem(int positionToAdd){
data.add(positionToAdd,"新的列表项"+new Random().nextInt(10000));
//通知recylerview控件 添加
notifyItemInserted(positionToAdd);
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv);
}
}
}