UltimateRecyclerView是一个比RecyclerView还全面的类,具体的使用标识如下:
/**
* Created by root on 2016/3/1.
*/
public class MainActivity extends AppCompatActivity {
UltimateRecyclerView ultimateRecyclerView;
MyAdapter myAdapter;
LinearLayoutManager linearLayoutManager;
ItemTouchHelper itemTouchHelper;
Toolbar tool_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ultimateRecyclerView = (UltimateRecyclerView) this.findViewById(R.id.ultimate_recycler_view);
ultimateRecyclerView.setHasFixedSize(false);
tool_bar = (Toolbar) this.findViewById(R.id.tool_bar);
setSupportActionBar(tool_bar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
final List stringList = new ArrayList<>();
stringList.add("111");
stringList.add("aaa");
stringList.add("222");
stringList.add("33");
stringList.add("44");
stringList.add("55");
stringList.add("66");
stringList.add("11771");
myAdapter = new MyAdapter(stringList);
linearLayoutManager = new LinearLayoutManager(this);
//设置布局的格式
ultimateRecyclerView.setLayoutManager(linearLayoutManager);
ultimateRecyclerView.setAdapter(myAdapter);
//给每一个item设置前置的栏目条,
StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(myAdapter);
ultimateRecyclerView.addItemDecoration(headersDecor);
//设置可以加载更多,但是经常出错
// ultimateRecyclerView.enableLoadmore();
//设置foot加载的view视图
myAdapter.setCustomLoadMoreView(LayoutInflater.from(this)
.inflate(R.layout.custom_bottom_progressbar, null));
//设置item背景的颜色
ultimateRecyclerView.setRecylerViewBackgroundColor(Color.parseColor("#3a0ff4"));
//编写下拉刷新的监听函数
ultimateRecyclerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
stringList.add("llalala");
myAdapter.notifyDataSetChanged();
ultimateRecyclerView.setRefreshing(false);
linearLayoutManager.scrollToPosition(0);
}
}, 1000);
}
});
//设置之后item内部的元素可以被拖动
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(myAdapter);
itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(ultimateRecyclerView.mRecyclerView);
myAdapter.setOnDragStartListener(new MyAdapter.OnStartDragListener() {
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
itemTouchHelper.startDrag(viewHolder);
}
});
//监听loadmore函数
ultimateRecyclerView.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
@Override
public void loadMore(int itemsCount, final int maxLastVisiblePosition) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
stringList.add("--->");
myAdapter.notifyDataSetChanged();
}
}, 1000);
}
});
//设置刷新控件的颜色组成
ultimateRecyclerView.setDefaultSwipeToRefreshColorScheme(getResources().getColor(android.R.color.holo_blue_bright),
getResources().getColor(android.R.color.holo_green_light),
getResources().getColor(android.R.color.holo_orange_light),
getResources().getColor(android.R.color.holo_red_light));
//设置滑动监听
ultimateRecyclerView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
Toast.makeText(MainActivity.this, "sc", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownMotionEvent() {
}
//在这里设置toolbar的隐藏和出现
@Override
public void onUpOrCancelMotionEvent(ObservableScrollState observableScrollState) {
if (observableScrollState == ObservableScrollState.UP) {
ultimateRecyclerView.hideToolbar(tool_bar, ultimateRecyclerView, getScreenHeight());
ultimateRecyclerView.hideFloatingActionMenu();
} else if (observableScrollState == ObservableScrollState.DOWN) {
ultimateRecyclerView.showToolbar(tool_bar, ultimateRecyclerView, getScreenHeight());
ultimateRecyclerView.showFloatingActionMenu();
}
}
});
ultimateRecyclerView.showFloatingButtonView();
//利用一个spinner进行展示各种item进入和删除的动画效果
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter spinnerAdapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
for (Type type : Type.values()) {
spinnerAdapter.add(type.name());
}
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
ultimateRecyclerView.setItemAnimator(Type.values()[position].getAnimator());
ultimateRecyclerView.getItemAnimator().setAddDuration(500);
ultimateRecyclerView.getItemAnimator().setMoveDuration(500);
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAdapter.insert("newly added item", 1);
}
});
findViewById(R.id.del).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAdapter.remove(1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// FastBinding.startactivity(this, item.getItemId());
return super.onOptionsItemSelected(item);
}
enum Type {
FadeIn(new FadeInAnimator()),
FadeInDown(new FadeInDownAnimator()),
FadeInUp(new FadeInUpAnimator()),
FadeInLeft(new FadeInLeftAnimator()),
FadeInRight(new FadeInRightAnimator()),
Landing(new LandingAnimator()),
ScaleIn(new ScaleInAnimator()),
ScaleInTop(new ScaleInTopAnimator()),
ScaleInBottom(new ScaleInBottomAnimator()),
ScaleInLeft(new ScaleInLeftAnimator()),
ScaleInRight(new ScaleInRightAnimator()),
FlipInTopX(new FlipInTopXAnimator()),
FlipInBottomX(new FlipInBottomXAnimator()),
FlipInLeftY(new FlipInLeftYAnimator()),
FlipInRightY(new FlipInRightYAnimator()),
SlideInLeft(new SlideInLeftAnimator()),
SlideInRight(new SlideInRightAnimator()),
SlideInDown(new SlideInDownAnimator()),
SlideInUp(new SlideInUpAnimator()),
OvershootInRight(new OvershootInRightAnimator()),
OvershootInLeft(new OvershootInLeftAnimator());
private BaseItemAnimator mAnimator;
Type(BaseItemAnimator animator) {
mAnimator = animator;
}
public BaseItemAnimator getAnimator() {
return mAnimator;
}
}
public int getScreenHeight() {
return findViewById(android.R.id.content).getHeight();
}
}
适配器Adapter文件如下:
public class MyAdapter extends UltimateViewAdapter {
List strs;
public MyAdapter(List strs){
this.strs=strs;
}
public void setOnDragStartListener(OnStartDragListener dragStartListener) {
mDragStartListener = dragStartListener;
}
@Override
public MyAdapterViewHolder getViewHolder(View view) {
return new MyAdapterViewHolder(view);
}
@Override
public MyAdapterViewHolder onCreateViewHolder(ViewGroup parent) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_adapter, parent, false);
MyAdapterViewHolder vh = new MyAdapterViewHolder(v);
return vh;
}
public void insert(String string, int position) {
insertInternal(strs, string, position);
}
public void remove(int position) {
removeInternal(strs, position);
}
@Override
public int getAdapterItemCount() {
return strs.size();
}
@Override
public void toggleSelection(int pos) {
super.toggleSelection(pos);
}
@Override
public void setSelected(int pos) {
super.setSelected(pos);
}
@Override
public long generateHeaderId(int position) {
if (getItem(position).length() > 0)
return getItem(position).charAt(0);
else return -1;
}
@Override
public void onBindViewHolder(MyAdapterViewHolder holder, int position) {
Log.v("--->postion",position+"");
holder.textview.setText(strs.get(position));
}
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.stick_header_item, parent, false);
return new RecyclerView.ViewHolder(view){};
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
}
public String getItem(int position) {
if (customHeaderView != null)
position--;
if (position < strs.size())
return strs.get(position);
else return "";
}
public class MyAdapterViewHolder extends UltimateRecyclerviewViewHolder {
TextView textview;
public MyAdapterViewHolder(View itemView) {
super(itemView);
textview= (TextView) itemView.findViewById(R.id.textview);
}
}
}
布局文件如下:
xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#30f2e0"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="?attr/selectableItemBackground"
android:padding="10dp"
android:text="Del" />
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/del"
android:layout_centerInParent="true"
android:background="?attr/selectableItemBackground"
android:padding="10dp"
android:text="Add" />
RelativeLayout>
android.support.v7.widget.Toolbar>
<com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
android:id="@+id/ultimate_recycler_view"
app:recyclerviewClipToPadding="true"
app:recyclerviewScrollbars="vertical"
app:recyclerviewDefaultSwipeColor="@array/google_colors"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent">com.marshalchen.ultimaterecyclerview.UltimateRecyclerView>
FrameLayout>
上面对于简单的功能都做了相应的注释,容易看懂。
github地址是https://github.com/cymcsg/UltimateRecyclerView