From 57acb76df651ff832bc03598f25ef9bc125c7821 Mon Sep 17 00:00:00 2001 From: Awesome <1760316362@qq.com> Date: Thu, 23 May 2019 22:21:47 +0800 Subject: [PATCH] TitleBar --- .../base/adapter/InfiniteScrollListener.kt | 32 +++++++++++++++++ .../legado/app/base/adapter/ItemAnimation.kt | 2 +- .../adapter/animations/ScaleInAnimation.kt | 3 +- .../io/legado/app/data/entities/SearchBook.kt | 1 + .../io/legado/app/ui/main/MainActivity.kt | 4 +-- .../io/legado/app/ui/search/SearchActivity.kt | 1 - .../io/legado/app/ui/search/SearchAdapter.kt | 34 +++++++++++++++++++ .../java/io/legado/app/ui/widget/TitleBar.kt | 18 +++++++--- app/src/main/res/layout/app_bar_main.xml | 15 ++------ app/src/main/res/layout/item_search.xml | 19 +++++++++++ app/src/main/res/layout/view_titlebar.xml | 1 + app/src/main/res/values/attrs.xml | 2 ++ 12 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/io/legado/app/base/adapter/InfiniteScrollListener.kt create mode 100644 app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt create mode 100644 app/src/main/res/layout/item_search.xml diff --git a/app/src/main/java/io/legado/app/base/adapter/InfiniteScrollListener.kt b/app/src/main/java/io/legado/app/base/adapter/InfiniteScrollListener.kt new file mode 100644 index 000000000..fc7c48bab --- /dev/null +++ b/app/src/main/java/io/legado/app/base/adapter/InfiniteScrollListener.kt @@ -0,0 +1,32 @@ +package io.legado.app.base.adapter + +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView + +/** + * Created by Invincible on 2017/12/15. + * + * 上拉加载更多 + */ +abstract class InfiniteScrollListener() : RecyclerView.OnScrollListener() { + private val loadMoreRunnable = Runnable { onLoadMore() } + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { +// if (dy < 0 || dataLoading.isDataLoading()) return + + val layoutManager:LinearLayoutManager = recyclerView.layoutManager as LinearLayoutManager + val visibleItemCount = recyclerView.childCount + val totalItemCount = layoutManager.itemCount + val firstVisibleItem = layoutManager.findFirstVisibleItemPosition() + + if (totalItemCount - visibleItemCount <= firstVisibleItem + VISIBLE_THRESHOLD) { + recyclerView.post(loadMoreRunnable) + } + } + + abstract fun onLoadMore() + + companion object { + private const val VISIBLE_THRESHOLD = 5 + } +} diff --git a/app/src/main/java/io/legado/app/base/adapter/ItemAnimation.kt b/app/src/main/java/io/legado/app/base/adapter/ItemAnimation.kt index 9e6f20c61..6db4a7415 100644 --- a/app/src/main/java/io/legado/app/base/adapter/ItemAnimation.kt +++ b/app/src/main/java/io/legado/app/base/adapter/ItemAnimation.kt @@ -4,7 +4,7 @@ import android.view.animation.Interpolator import android.view.animation.LinearInterpolator import io.legado.app.base.adapter.animations.AlphaInAnimation import io.legado.app.base.adapter.animations.BaseAnimation -import io.legado.app.base.animations.ScaleInAnimation +import io.legado.app.base.adapter.animations.ScaleInAnimation import io.legado.app.base.adapter.animations.SlideInBottomAnimation import io.legado.app.base.adapter.animations.SlideInLeftAnimation import io.legado.app.base.adapter.animations.SlideInRightAnimation diff --git a/app/src/main/java/io/legado/app/base/adapter/animations/ScaleInAnimation.kt b/app/src/main/java/io/legado/app/base/adapter/animations/ScaleInAnimation.kt index 3f73e7a3c..3464f4079 100644 --- a/app/src/main/java/io/legado/app/base/adapter/animations/ScaleInAnimation.kt +++ b/app/src/main/java/io/legado/app/base/adapter/animations/ScaleInAnimation.kt @@ -1,9 +1,8 @@ -package io.legado.app.base.animations +package io.legado.app.base.adapter.animations import android.animation.Animator import android.animation.ObjectAnimator import android.view.View -import io.legado.app.base.adapter.animations.BaseAnimation class ScaleInAnimation @JvmOverloads constructor(private val mFrom: Float = DEFAULT_SCALE_FROM) : BaseAnimation { diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt index feecd7295..8994f14bd 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt @@ -1,2 +1,3 @@ package io.legado.app.data.entities +class SearchBook \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index d04315dd6..cf4a2329d 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -14,6 +14,7 @@ import io.legado.app.base.BaseActivity import io.legado.app.ui.search.SearchActivity import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.app_bar_main.* +import kotlinx.android.synthetic.main.view_titlebar.* class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener { override val viewModel: MainViewModel @@ -22,11 +23,10 @@ class MainActivity : BaseActivity(), NavigationV get() = R.layout.activity_main override fun onViewModelCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) { - setSupportActionBar(toolbar) fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) } val toggle = ActionBarDrawerToggle( - this, drawer_layout, toolbar, + this, drawer_layout, titleBar.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 33be6236f..14994c535 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -15,7 +15,6 @@ class SearchActivity : BaseActivity() { get() = R.layout.activity_search override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { - } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt new file mode 100644 index 000000000..a23ee7605 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt @@ -0,0 +1,34 @@ +package io.legado.app.ui.search + +import android.content.Context +import io.legado.app.R +import io.legado.app.base.adapter.ItemViewDelegate +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.SearchBook +import kotlinx.android.synthetic.main.item_search.view.* + +class SearchAdapter(context: Context) : SimpleRecyclerAdapter(context) { + + init { + addItemViewDelegate(TestItemDelegate(context)) + } + + override val layoutID: Int + get() = R.layout.item_search + + override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) { + holder.itemView.bookName.text = "我欲封天" + } + + internal class TestItemDelegate(context: Context) : ItemViewDelegate(context){ + override val layoutID: Int + get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates. + + override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt index e7df5b914..b354266eb 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -5,7 +5,10 @@ import android.content.res.ColorStateList import android.graphics.PorterDuff import android.graphics.drawable.Drawable import android.util.AttributeSet +import android.view.Menu +import android.view.View import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar import androidx.core.graphics.drawable.DrawableCompat import com.google.android.material.appbar.AppBarLayout import io.legado.app.R @@ -13,11 +16,16 @@ import kotlinx.android.synthetic.main.view_titlebar.view.* class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, attrs) { + val toolbar: Toolbar + val menu: Menu + get() = toolbar.menu + init { inflate(context, R.layout.view_titlebar, this) + toolbar = findViewById(R.id.toolbar) val a = context.obtainStyledAttributes( attrs, R.styleable.TitleBar, - 0, 0 + R.attr.titleBarStyle, 0 ) val navigationIcon = a.getDrawable(R.styleable.TitleBar_navigationIcon) val navigationContentDescription = a.getText(R.styleable.TitleBar_navigationContentDescription) @@ -40,13 +48,15 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a } } + fun setNavigationOnClickListener(clickListener: ((View) -> Unit)){ + toolbar.setNavigationOnClickListener(clickListener) + } + private fun attachToActivity(context: Context) { val activity = getCompatActivity(context) activity?.let { activity.setSupportActionBar(toolbar) - activity.supportActionBar?.let { - it.setDisplayHomeAsUpEnabled(true) - } + activity.supportActionBar?.setDisplayHomeAsUpEnabled(true) } } diff --git a/app/src/main/res/layout/app_bar_main.xml b/app/src/main/res/layout/app_bar_main.xml index ef693f0b6..ae86cf117 100644 --- a/app/src/main/res/layout/app_bar_main.xml +++ b/app/src/main/res/layout/app_bar_main.xml @@ -7,19 +7,10 @@ android:layout_height="match_parent" tools:context=".ui.main.MainActivity"> - - - - - + android:layout_height="wrap_content"/> diff --git a/app/src/main/res/layout/item_search.xml b/app/src/main/res/layout/item_search.xml new file mode 100644 index 000000000..4fe3215d2 --- /dev/null +++ b/app/src/main/res/layout/item_search.xml @@ -0,0 +1,19 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/view_titlebar.xml b/app/src/main/res/layout/view_titlebar.xml index f1befb3b3..82f286582 100644 --- a/app/src/main/res/layout/view_titlebar.xml +++ b/app/src/main/res/layout/view_titlebar.xml @@ -5,4 +5,5 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" + android:theme="@style/AppTheme.AppBarOverlay" app:popupTheme="@style/AppTheme.PopupOverlay"/> diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 7456589c4..942cea81c 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -29,6 +29,8 @@ + +