Android 页面转html,HtmlRecycler将一个简单的html页面转换为原生android小部件的RecyclerView...

HtmlRecycler

Converts a simple html page into A RecyclerView of native android widgets powered by Jsoup library and inspired by Medium Textview.

This is under development

Note

This library was design and developed by ME and we use this in our application which depends on a Content Management system and was never intended to replace browsers or act as one. this library simply gave us more control over html page than WebView

Add it to your project

dependencies {

implementation 'com.github.m7mdra:HtmlRecycler:0.1.11'

}

This project is distributed using jitpack. Make sure you add it as a maven repository to your build.gradle

allprojects {

repositories {

maven {

url 'https://jitpack.io'

}

}

}

Demo

625332134c6f4d4600884b99daebf603.png

625332134c6f4d4600884b99daebf603.png

Or simply git clone the repository and build the app module.

Currently supported html elements

Paragraph

H1...H6

Image

Video

Audio

Ordered List

Unordered List

Description List

Anchor Link

IFrame

Table

DIV

Implementation

val networkSource = NetworkSource("https://gist.githubusercontent.com/m7mdra/f22c62bc6941e08064b4fbceb4832a90/raw/ea8574d986635cf214541f1f5702ef37cc731aaf/article.html")

HtmlRecycler.Builder(this@MainActivity)

.setSource(networkSource)

.setAdapter(DefaultElementsAdapter(this@MainActivity) { element, i, view ->

}})

.setRecyclerView(recyclerView)

.setLoadingCallback(object : HtmlRecycler.LoadCallback {

override fun onLoadingStart() {

progressBar.visibility = View.VISIBLE

}

override fun onLoaded(document: Document?) {

progressBar.visibility = View.GONE

}

})

.build()

The above code uses the existing implementation of DefaultElementsAdapter which extends ElementsAdapter class which inherently is a RecylcerView Adpater the DefaultElementsAdapter uses a layout resources files defined by me but they not styled probably and are very buggy (especially the video, audio and iframe ones).

Want to create your own adapter? just simply extend ElementsAdapter and override methods:

class BetterImplementationThanTheAuthorsAdapter : ElementsAdapter() {

override fun onCreateElement(parent: ViewGroup, elementType: ElementType): RecyclerView.ViewHolder {

when (elementType) {

ElementType.Paragraph -> {

return ParagraphViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_paragarph, parent, false))

}

// Define other elements here

}

}

override fun onBindElement(holder: RecyclerView.ViewHolder, position: Int) {

val element = elements[position] //current element

if (holder is ParagraphViewHolder){

val paragraphElement = element as ParagraphElement

holder.paragraphText.text= paragraphElement.text

}

}

}

}

Then replace the default adapter with your adapter:

HtmlRecycler.Builder(this)

.setSource(StringSource(Data.data))

.setAdapter(BetterImplementationThanTheAuthorsAdapter()) // this is a custom adapter

.setRecyclerView(recyclerView)

.build()

How to add Data

Data can come from different sources, the library support the following:

Assets

File

String

Network (runs on UI thread by default so you have to run it on different thread or write your own Source Implementation )

Write your own source

Simply implement the Source interface which will return a Document of the parsed Source:

class FileSource(val file: File) : Source {

override fun get(): Document {

return Jsoup.parse(file, "UTF-8")

}

}

Attach Click listeners on elements

In DefaultElemetsAdapter class at line #27 l i defined a higher-order-function in the constructor method (which dose the same as defining an interface) and on line #75 we envoke the method passing our element and the position of the clicked view.

TODO list:

Define a standard Layout styling.

allow NetworkSource to run on UI thread without crashing.

Support the following elements:

Table

Div

Section

Superscript and Subscrpit

Test Element Extractors for different data sets.

add more control over paragraph element. paragraph element will be rendered using the android.text.Html class rather than handled by the library

other thing that i come up with...

Dependencies:

PR are welcome just use crtl+alt+L or (command + alt+L for mac ... idk if right) after every time your finish write code to format it.

projects using this library:

if you are using this library in your project let me by sending an email at xm7mdrax@gmail.com know and i will post it here.

be the first on the list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值