Ticker-Using-Recycler-View-Android
所属分类:android开发
开发工具:Java
文件大小:0KB
下载次数:0
上传日期:2020-02-04 10:12:17
上 传 者:
sh-1993
说明: 有时在我们的android应用程序中需要一个字幕文本效果,如水平或垂直平滑自动滚动。示例:对于突发新闻ap...,
(Sometimes in our android apps need a marquee text effect like smooth autoscroll horizontally or vertically. Example: For breaking news app there is a most necessary functionality which is show the breaking news like a horizontal scroll. Again, In share market app in top app should give a auto scroll effect for show the text..But in android)
文件列表:
AndroidTicker/ (0, 2020-02-04)
AndroidTicker/.idea/ (0, 2020-02-04)
AndroidTicker/.idea/caches/ (0, 2020-02-04)
AndroidTicker/.idea/caches/build_file_checksums.ser (540, 2020-02-04)
AndroidTicker/.idea/caches/gradle_models.ser (158599, 2020-02-04)
AndroidTicker/.idea/codeStyles/ (0, 2020-02-04)
AndroidTicker/.idea/codeStyles/Project.xml (3309, 2020-02-04)
AndroidTicker/.idea/compiler.xml (686, 2020-02-04)
AndroidTicker/.idea/copyright/ (0, 2020-02-04)
AndroidTicker/.idea/copyright/profiles_settings.xml (74, 2020-02-04)
AndroidTicker/.idea/encodings.xml (159, 2020-02-04)
AndroidTicker/.idea/gradle.xml (864, 2020-02-04)
AndroidTicker/.idea/misc.xml (5209, 2020-02-04)
AndroidTicker/.idea/modules.xml (363, 2020-02-04)
AndroidTicker/.idea/runConfigurations.xml (564, 2020-02-04)
AndroidTicker/.idea/vcs.xml (183, 2020-02-04)
AndroidTicker/MyApplication.zip (101710, 2020-02-04)
AndroidTicker/app/ (0, 2020-02-04)
AndroidTicker/app/build.gradle (1427, 2020-02-04)
AndroidTicker/app/proguard-rules.pro (675, 2020-02-04)
AndroidTicker/app/src/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/com/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/com/example/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/com/example/aliahmed/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/com/example/aliahmed/myapplication/ (0, 2020-02-04)
AndroidTicker/app/src/androidTest/java/com/example/aliahmed/myapplication/ExampleInstrumentedTest.java (769, 2020-02-04)
AndroidTicker/app/src/main/ (0, 2020-02-04)
AndroidTicker/app/src/main/AndroidManifest.xml (887, 2020-02-04)
AndroidTicker/app/src/main/java/ (0, 2020-02-04)
AndroidTicker/app/src/main/java/com/ (0, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/ (0, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/aliahmed/ (0, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/aliahmed/myapplication/ (0, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/aliahmed/myapplication/AutoScrollActivity.java (4749, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/aliahmed/myapplication/AutoScrollFragment.java (5246, 2020-02-04)
AndroidTicker/app/src/main/java/com/example/aliahmed/myapplication/FragmentContainerActivity.java (1191, 2020-02-04)
... ...
# Creating a Autoscrolled RecyclerView like Marquee text in Android also known as "Ticker in android"
### Why this article?
Sometimes in our android apps need a marquee text effect like smooth autoscroll horizontally or vertically.
##### Example:
For breaking news app there is a most necessary functionality which is show the breaking news like a horizontal scroll.
Again, In share market app in top app should give a auto scroll effect for show the text.
Stock exchange scroll:
![alt text](https://github.com/aliahmedbd/Ticker-Using-Recycler-View-Android/blob/master/ezgif.com-crop.gif "Example Activity")
But in android there is no perfect library for marquee text but in this article I will give a simplest solution for marquee text.
### Procedure:
#### Add RecyclerView:
Add RecyclerView as your desire place and also add dependency in gradle.
#### XML:
```xml
```
#### Gradle :
```java
compile 'com.android.support:recyclerview-v7:24.2.0'
```
#### Create adapter for RecyclerView
Create adapter for RecyclerView. If you don't know how to create adapter please follow this link: [Click Here](http://antonioleiva.com/recyclerview/ )
#### Make horizontal scroll
For RecyclerView we have to use LayoutManager there is a method which is :
```java
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
```
Then use this in RecyclerView :
```java
yourRecyclerView.setLayoutManager(linearLayoutManager );
```
Then your RecyclerView will srolled in horizontally.
#### Creating auto scroll for RecyclerView
Here I use a thread which will perform autoscroll the items of RecyclerView. There is a method in RecyclerView which is smoothScrollToPosition() which take position of recyclerview to scroll that position to current position. So I use a thread to increment the position automatically so an autoscroll is happening because position is incremented automatically. When all incremented position will equal to the total item size then start from beginning. In this way an autoscroll effect is happened.
Here is the code :
```java
public void autoScroll(){
final int speedScroll = 0;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
int count = 0;
@Override
public void run() {
if(count == scrollStockAdapter.getItemCount())
count =0;
if(count < scrollStockAdapter.getItemCount()){
rec_scroll_stock.smoothScrollToPosition(++count);
handler.postDelayed(this,speedScroll);
}
}
};
handler.postDelayed(runnable,speedScroll);
}
```
#### Create Smooth scroll automatically
Now main problem is auto scroll speed which is too fast. For RecyclerView we have to declare a LayoutManager. In LayoutManager there is a method which is smoothScrollToPosition() which is responsible for smooth scrolling. But for scrolling there is a velocity which default speed is 25f. To change this velocity you have to override this method. If you want a smooth scroll then increase this value as you want. In my case I use 4000f.
```java
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,RecyclerView.State state, int position) {
LinearSmoothScroller smoothScroller = newLinearSmoothScroller(MainActivity.this) {
private static final float SPEED = 4000f;// Change this value (default=25f)
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return SPEED / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
};
```
Then set this LayoutManager to your RecyclerView like this :
yourRecyclerView.setLayoutManager(layoutManager);
#### Conclusion:
In this way you can get a look and feel like Marquee text or like news feed.
### Using this repository in your project?
If you are using this repository in your project, and happy about it, please send me a [email](mailto:aliahmedaiub@gmail.com) and let me know, I'll add a references of your app. Thanks :)
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
近期下载者:
相关文件:
收藏者: