RollTextView
說明:一個可以滾動的佈局,可以在首頁顯示新聞消息等,可自定義每次顯示的數目,動畫時間等...
原理:RollTextView繼承RecyclerView,因此實際上並非是TextView,而是ViewGroup,在控件內容重寫的定時器,保證按照某種約束進行滾動。
源碼可參考github地址:rolltextview
1、準備步驟
在項目build.gradle中添加依賴:
compile 'com.knowledge.mnlin:rolltextview:0.0.1'
若項目依賴有衝突,則可以屏蔽該庫中依賴的資源
compile ('com.knowledge.mnlin:rolltextview:0.0.1'){
exclude group:'com.android.support', module:'recyclerview-v7'
...
}
如果提示找不到依賴文件,可能時jcenter未及時通過,可以依賴私人倉庫
//Project的build.gradle文件
allprojects {
repositories {
...
maven { url "https://dl.bintray.com/lovingning/maven"}
}
}
2、簡單使用
1、在需要顯示滾動信息的layout文件中添加控件;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:orientation="vertical">
<View
android:layout_marginTop="100dp"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="1px"/>
<com.knowledge.mnlin.RollTextView
android:id="@+id/rtv_temp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="1px"/>
</LinearLayout>
2、在Activity或Fragment中初始化數據
RollTextView rollTextView = findViewById(R.id.rtv_temp);
rollTextView.refreshData(Arrays.asList("0000000"
, "1111111111111111111111111111111111111111111111111111"
, "22222"
, "3333333"
, "444444"
, "55555"
, "6666666"
, "777777"
, "888888"
, "99999999"));
3、進階屬性
可設屬性總覽:
rollTextView.setAppearCount(3)//設置每次顯示的數量
.setInterval(2000)//設置滾動的間隔時間,以毫秒爲單位
.setOrderVisible(true)//設置顯示序號的view是否可見
.setEndText("查看", true)//設置尾部文字是否可見
.setLayoutResource(R.layout.item_roll_text_view)//爲item自定義layout,但必須遵循規定
.setRollDirection(1)//設置滾動的方向,0爲向上滾動,1爲向下滾動,2爲向右滾動,3爲向左滾動
//設置itemClick監聽器
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Logger.e("點擊位置:" + position);
}
})
//設置數據源,接收一個list,顯示內容時調用其toString方法,因此數據內容不可爲null
.refreshData(Arrays.asList("0000000"
, "1111111111111111111111111111111111111111111111111111"
, "22222"
, "3333333"
, "444444"
, "55555"
, "6666666"
, "777777"
, "888888"
, "99999999"));
1、使用setAppearCount
該方法可以設置每次顯示的條目,默認情況下,只顯示一條信息。
在滾動方向爲水平時,只能顯示一條信息。
2、setInterval
設置滾動時間間隔,默認情況下爲2000ms
3、setOrderVisible與setEndText
默認情況下,會爲每條信息添加序號,並在尾部添加lable:“更多”,可通過方法關閉顯示效果
4、setLayoutResource自定義item佈局
如果不滿足預設的item-layout文件,可以自定義xml佈局;
- 定義佈局時儘量不要做過多修改,避免滑動失敗;
- 自定義佈局中必須有三個AppCompatTextView子view,並且id分別爲:tv_left、tv_center、tv_right。
- 定義佈局時,root佈局上不要使用margin*屬性**,避免滾動時錯位。
系統默認的xml文件爲:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/prefer_view_height"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_left"
android:layout_width="@dimen/prefer_view_height"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="@dimen/prefer_view_padding_vertical"
android:text="1."/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_center"
style="@style/TextViewStandard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="很多內容很多內容很多內容很多內容很多內容很多內容很多內容"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_right"
style="@style/TextViewStandard"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="詳情"/>
</LinearLayout>
5、使用setRollDirection設置滾動方向
默認情況下,控件從下向上滾動;當然可以通過該方法來設置滾動的方向:
- 0 表示向上滾動
- 1 表示向下滾動
- 2 表示向右滾動
- 3 表示向左滾動
6、refreshData
refreshData方法需要在初始化的最後執行,該方法會將之前所有的設置進行apply,然後再次刷新動畫。
因此務必保證:refreshData必須調用並且最後調用
7、主動開啓或關閉動畫
庫中已經對動畫的運行與停止自定義了處理方法,在控件不可見時,動畫會自動關閉,且控件可以手動滑動。若有特殊需求需要控件停止或啓動動畫,可以主動調用方法:
* startAnimation() 開啓動畫
* stopAnimation() 關閉動畫