这里是一个 Kotlin 活动,应该显示一个事件列表(来自 sample.json)。
问题:当我调用
class TalksActivity : AppCompatActivity(), TalkAdapter.Listener {
private val TAG = TalksActivity::class.java.simpleName
private var mCompositeDisposable: CompositeDisposable? = null
private var mAdapter: TalkAdapter? = null
private var disposable: Disposable? = null
private val mapper = createMapper()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_talks)
pbWaiting.visibility = View.VISIBLE
mCompositeDisposable = CompositeDisposable()
initRecyclerView()
loadTalks()
}
private fun initRecyclerView() {
rv_talks_list.setHasFixedSize(true)
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this)
rv_talks_list.layoutManager = layoutManager
rv_talks_list.adapter = TalkAdapter(ArrayList(Collections.emptyList()), this)
}
private fun loadTalks() {
disposable = getTalks()
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.subscribe ({ result -> handleResponse(result) }, { error -> handleError(error) })
}
private fun handleResponse(talkList: List<Talk>) {
mAdapter = TalkAdapter(ArrayList(talkList), this)
rv_talks_list.adapter = mAdapter
pbWaiting.visibility = View.GONE
}
private fun handleError(error: Throwable) {
pbWaiting.visibility = View.GONE
throw error
}
private fun createMapper(): ObjectMapper {
val mapper = jacksonObjectMapper()
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
return mapper
}
override fun onItemClick(talk: Talk) {
startActivity(CountdownActivity.newIntent(this, talk))
}
private fun getTalks() : Observable<List<Talk>> {
val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
val typeFactory = mapper.typeFactory
val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
return Observable.create<List<Talk>> {
mapper.readValue(text, collectionType)
}
}
}
问题:当我调用
loadTalks()
时,handleResponse(result)
或handleError(error)
从未被调用,屏幕仅显示白色并显示进度条。
控制台中没有错误信息。
这是我的非常简单的activity_talks.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TalksActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_talks_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/pbWaiting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
有什么问题吗?
在没有使用 rxKotlin 和 observable 代码的情况下,这个代码是可以工作的。
编辑
这是我的适配器:
class TalkAdapter(private val dataList: ArrayList<Talk>, private val listener: Listener) : RecyclerView.Adapter<TalkAdapter.ViewHolder>() {
interface Listener {
fun onItemClick(talk: Talk)
}
private val colors: Array<String> = arrayOf("#EF5350", "#EC407A", "#AB47BC", "#7E57C2", "#5C6BC0", "#42A5F5")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(dataList[position], listener, colors, position)
}
override fun getItemCount(): Int = dataList.count()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_talk, parent, false)
return ViewHolder(view)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(talk: Talk, listener: Listener, colors: Array<String>, position: Int) {
itemView.title.text = talk.title
itemView.recap.text = talk.summary
itemView.eventId.text = talk.eventId
itemView.setBackgroundColor(Color.parseColor(colors[position % 6]))
itemView.setOnClickListener { listener.onItemClick(talk) }
}
}
}
编辑
感谢Demigod的答案,我找到了解决方案。
当我使用Observable.create
创建一个Observable时,必须手动触发onNext()
和onError()
。
要修复它,请更改
private fun getTalks() : Observable<List<Talk>> {
val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
val typeFactory = mapper.typeFactory
val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
return Observable.create<List<Talk>> {
mapper.readValue(text, collectionType)
}
}
使用
private fun getTalks(): Observable<List<Talk>> {
val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
val typeFactory = mapper.typeFactory
val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
return Observable.fromCallable { mapper.readValue<List<Talk>>(text, collectionType) }
}
为了更好的性能:
private fun getTalks(): Observable<List<Talk>> {
return Observable.fromCallable {
val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
val typeFactory = mapper.typeFactory
val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
mapper.readValue<List<Talk>>(text, collectionType)
}
}
handleResponse(result)
或者handleError(error)
仍然没有被调用。 - psv