ToDoList
1.0.0
appetize.io 上的現場演示
在 dribbble 上檢查這個項目
在我們的部落格上了解我們是如何做到的
##要求
##用法
添加到您的根 build.gradle:
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}
新增依賴:
dependencies {
compile ' com.github.yalantis:todolist:v1.0.1 '
}
將BatRecyclerView
加入到您的 xml 佈局中
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< LinearLayout xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : id = " @+id/root "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : background = " @drawable/gradient "
android : orientation = " vertical " >
< android .support.v7.widget.Toolbar
android : id = " @+id/toolbar "
android : layout_width = " match_parent "
android : layout_height = " ?android:attr/actionBarSize "
android : background = " @android:color/transparent "
app : logo = " @drawable/ic_menu " >
< TextView
android : id = " @+id/text_title "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_gravity = " center "
android : text = " 14 Feb, 2016 "
android : textColor = " @android:color/white "
android : textSize = " 20sp " />
</ android .support.v7.widget.Toolbar>
< com .yalantis.beamazingtoday.ui.widget.BatRecyclerView
android : id = " @+id/bat_recycler_view "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : layout_gravity = " center_horizontal "
app : add_button_color = " @drawable/selector_button_add "
app : hint = " @string/str_add_goal "
app : radio_button_res = " @drawable/selector_radio_button " />
</ LinearLayout >
創建BatListener
private BatListener mListener = new BatListener () {
@ Override
public void add ( String string ) {
mGoals . add ( 0 , new Goal ( string ));
mAdapter . notify ( AnimationType . ADD , 0 );
}
@ Override
public void delete ( int position ) {
mGoals . remove ( position );
mAdapter . notify ( AnimationType . REMOVE , position );
}
@ Override
public void move ( int from , int to ) {
if ( from >= 0 && to >= 0 ) {
//if you use 'BatItemAnimator'
mAnimator . setPosition ( to );
BatModel model = mGoals . get ( from );
mGoals . remove ( model );
mGoals . add ( to , model );
mAdapter . notify ( AnimationType . MOVE , from , to );
if ( from == 0 || to == 0 ) {
mRecyclerView . getView (). scrollToPosition ( Math . min ( from , to ));
}
}
}
};
建立BatAdapter
。傳遞給其建構函式模型列表和BatListener
。請注意,您的模型應該實作BatModel
接口
mAdapter = new BatAdapter ( mGoals = new ArrayList < BatModel >() {{
add ( new Goal ( "first" ));
add ( new Goal ( "second" ));
add ( new Goal ( "third" ));
add ( new Goal ( "fourth" ));
add ( new Goal ( "fifth" ));
add ( new Goal ( "sixth" ));
add ( new Goal ( "seventh" ));
add ( new Goal ( "eighth" ));
add ( new Goal ( "ninth" ));
add ( new Goal ( "tenth" ));
}}, mListener );
mAdapter . setOnItemClickListener ( new OnItemClickListener () {
@ Override
public void onClick ( BatModel item , int position ) {
Toast . makeText ( ExampleActivity . this , item . getText (), Toast . LENGTH_SHORT ). show ();
}
});
初始化您的回收者視圖
mRecyclerView . getView (). setLayoutManager ( new LinearLayoutManager ( this ));
mRecyclerView . getView (). setAdapter ( mAdapter );
ItemTouchHelper itemTouchHelper = new ItemTouchHelper ( new BatCallback ( this ));
itemTouchHelper . attachToRecyclerView ( mRecyclerView . getView ());
mRecyclerView . setAddItemListener ( mListener );
您可以使用BatItemAnimator
對清單項目進行動畫處理,就像我們的演示中一樣:
mAnimator = new BatItemAnimator ();
mAdapter = new BatAdapter ( mGoals = new ArrayList < BatModel >() {{
add ( new Goal ( "first" ));
add ( new Goal ( "second" ));
add ( new Goal ( "third" ));
add ( new Goal ( "fourth" ));
add ( new Goal ( "fifth" ));
add ( new Goal ( "sixth" ));
add ( new Goal ( "seventh" ));
add ( new Goal ( "eighth" ));
add ( new Goal ( "ninth" ));
add ( new Goal ( "tenth" ));
}}, mListener , mAnimator );
mRecyclerView . getView (). setItemAnimator ( mAnimator );
您需要將動畫師實例傳遞給BatRecyclerView
和BatAdapter
。此外,還需要在move()
回呼中傳遞移動項目的位置:
@ Override
public void move ( int from , int to ) {
if ( from >= 0 && to >= 0 ) {
mAnimator . setPosition ( to );
BatModel model = mGoals . get ( from );
mGoals . remove ( model );
mGoals . add ( to , model );
mAdapter . notify ( AnimationType . MOVE , from , to );
if ( from == 0 || to == 0 ) {
mRecyclerView . getView (). scrollToPosition ( Math . min ( from , to ));
}
}
}
有關更多使用範例,請查看範例應用程式
cursor_drawable
屬性和setCursorDrawable(@DrawableRes int res)
方法。應該分別使用而不是cursor_color
屬性和setCursorColor(@ColorInt int color)
方法如果您可以向我們發送您使用我們組件的項目的鏈接,我們將非常高興。只需發送電子郵件至 [email protected] 如果您對動畫有任何疑問或建議,請告訴我們。
PS 我們將發布更多包含在程式碼中的精彩內容以及關於如何使 iOS (Android) 的 UI 變得更好的教學。敬請關注!
The MIT License (MIT)
Copyright © 2017 Yalantis, https://yalantis.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.