前言
因为工作期间,一直用的旧的View,还没用过新出的这些ViewO(∩_∩)O~
一、基本用法
这里直接添上了头部以及删除添加
1. build.gradle
compile 'com.android.support:recyclerview-v7:25.2.0'
2. 布局
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></android.support.v7.widget.RecyclerView>
3. 在activity中的使用
List<String> list = new ArrayList<>();
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);//设置样式,也可以用LinearLayoutManager线性布局或者StaggeredGridLayoutManager瀑布流布局
gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//设置方向
recycler.setLayoutManager(gridLayoutManager);
//这是为了添加头部,是第一条的话(即头部header),就跨两列,不是第一条就还是一列
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position == 0) return 2;
else return 1;
}
});
recycler.setHasFixedSize(true); //如果item大小是一致的,则可以设置,这样子可以改善性能
recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));//设置分割线,默认是android自带的listDivider
recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//设置分割线,默认是android自带的listDivider
recycler.setItemAnimator(new DefaultItemAnimator()); //设置ITEM增加删除的动画
for (int i = 0; i < 9; i++) {
list.add(i + "--");
}
View inflate = LayoutInflater.from(this).inflate(R.layout.section, null); //header
myAdapter = new MyAdapter(inflate, list);
recycler.setAdapter(myAdapter);
}
public void addItem(View view) {
list.add(0, "添加");
myAdapter.notifyItemInserted(1);
}
public void removeItem(View view) {
list.remove(0);
myAdapter.notifyItemRemoved(1);
}
4. adapter
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private View header;
private List<String> datas;
public static final int ITEM_VIEW_TYPE_HEADER = 0;
public static final int ITEM_VIEW_TYPE_ITEM = 1;
public MyAdapter(View header, List<String> datas) {
if (header == null) {
throw new IllegalArgumentException("header is null");
}
this.header = header;
this.datas = datas;
}
boolean isHeader(int position) {
return position == 0;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_VIEW_TYPE_HEADER) {
return new ViewHolder(header);
}
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public int getItemViewType(int position) {
return isHeader(position) ? ITEM_VIEW_TYPE_HEADER : ITEM_VIEW_TYPE_ITEM;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (isHeader(position)) return;
holder.mtxt.setText(datas.get(position - 1));
}
@Override
public int getItemCount() {
if (datas != null) {
return datas.size() + 1;
}
else
return 1;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView mtxt;
public ViewHolder(View itemView) {
super(itemView);
mtxt = (TextView) itemView.findViewById(android.R.id.text1);
}
}
}
效果图
噫噫噫?删除的时候分割线咋么没变?原因是 recycler.setHasFixedSize(true);这个啦,把这行代码删除就OK了哦
二、一步改变分割线样式
这里我们就把setHasFixedSize删除哈,其他代码不变
1. drawable下添加xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="180dp"/>
<solid android:color="#006683"/>
<size android:height="2dp" android:width="2dp"/>
</shape>
2. 在styles.xml中添加item,去修改listDivider的样式
<item name="android:listDivider">@drawable/roundrect</item>
效果
三、关闭动画
public void closeDefaultAnimator() {
RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
itemAnimator.setAddDuration(0);
itemAnimator.setChangeDuration(0);
itemAnimator.setMoveDuration(0);
itemAnimator.setRemoveDuration(0);
((SimpleItemAnimator) itemAnimator).setSupportsChangeAnimations(false);
}
四、图片列表刷新闪烁解决
- 首先关闭动画
- 使用notifyItemRangeChanged进行刷新
3.item的image设置tag,比较tag,相同时则不必在加载一次图片。
网友评论