parent
53e21bc22e
commit
3ddcaaff76
@ -0,0 +1,111 @@ |
||||
package xyz.fycz.myreader.ui.activity |
||||
|
||||
import android.app.Activity |
||||
import android.os.Bundle |
||||
import android.view.View |
||||
import androidx.appcompat.widget.Toolbar |
||||
import androidx.core.content.ContextCompat |
||||
import androidx.core.graphics.drawable.DrawableCompat |
||||
import androidx.recyclerview.widget.ItemTouchHelper |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.reactivex.Single |
||||
import xyz.fycz.myreader.R |
||||
import xyz.fycz.myreader.application.App |
||||
import xyz.fycz.myreader.application.SysManager |
||||
import xyz.fycz.myreader.base.BaseActivity |
||||
import xyz.fycz.myreader.databinding.ActivityGroupManagerBinding |
||||
import xyz.fycz.myreader.databinding.ItemGroupBinding |
||||
import xyz.fycz.myreader.greendao.entity.BookGroup |
||||
import xyz.fycz.myreader.ui.adapter.BookGroupAdapter |
||||
import xyz.fycz.myreader.ui.adapter.helper.ItemTouchCallback |
||||
import xyz.fycz.myreader.ui.dialog.BookGroupDialog |
||||
import xyz.fycz.myreader.util.SharedPreUtils |
||||
|
||||
/** |
||||
* @author fengyue |
||||
* @date 2021/8/30 12:48 |
||||
*/ |
||||
class GroupManagerActivity : BaseActivity() { |
||||
private lateinit var binding: ActivityGroupManagerBinding |
||||
|
||||
private lateinit var adapter: BookGroupAdapter |
||||
|
||||
private lateinit var groupDialog: BookGroupDialog |
||||
|
||||
private lateinit var itemTouchHelper: ItemTouchHelper |
||||
|
||||
private var openGroup: Boolean = false |
||||
|
||||
override fun bindView() { |
||||
binding = ActivityGroupManagerBinding.inflate(layoutInflater) |
||||
setContentView(binding.root) |
||||
} |
||||
|
||||
override fun setUpToolbar(toolbar: Toolbar?) { |
||||
super.setUpToolbar(toolbar) |
||||
setStatusBarColor(R.color.colorPrimary, true) |
||||
supportActionBar?.title = getString(R.string.manage_book_group) |
||||
} |
||||
|
||||
override fun initData(savedInstanceState: Bundle?) { |
||||
super.initData(savedInstanceState) |
||||
groupDialog = BookGroupDialog(this) |
||||
groupDialog.initBookGroups(false) |
||||
openGroup = SharedPreUtils.getInstance().getBoolean("openGroup") |
||||
} |
||||
|
||||
override fun initWidget() { |
||||
super.initWidget() |
||||
binding.scBookGroup.isChecked = openGroup |
||||
binding.recyclerView.visibility = if (openGroup) View.VISIBLE else View.GONE |
||||
adapter = BookGroupAdapter(this, { itemTouchHelper.startDrag(it) }, groupDialog).apply { |
||||
setItems(groupDialog.getmBookGroups()) |
||||
addFooterView { |
||||
ItemGroupBinding.inflate(inflater, it, false).apply { |
||||
ivIcon.setImageDrawable( |
||||
ContextCompat.getDrawable( |
||||
this@GroupManagerActivity, |
||||
R.drawable.ic_plus |
||||
) |
||||
) |
||||
ivIcon.setColorFilter(resources.getColor(R.color.colorAccent)) |
||||
tvGroupName.text = "添加分组" |
||||
ivMove.visibility = View.GONE |
||||
root.setOnClickListener { |
||||
groupDialog.showAddOrRenameGroupDia(false, true, 0, |
||||
object : BookGroupDialog.OnGroup() { |
||||
override fun change() { |
||||
|
||||
} |
||||
|
||||
override fun addGroup(group: BookGroup) { |
||||
App.getHandler().postDelayed( |
||||
{ adapter.addItem(group) }, 300 |
||||
) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
binding.recyclerView.layoutManager = LinearLayoutManager(this) |
||||
binding.recyclerView.adapter = adapter |
||||
val itemTouchCallback = ItemTouchCallback() |
||||
itemTouchCallback.setOnItemTouchListener(adapter) |
||||
itemTouchCallback.setLongPressDragEnable(true) |
||||
itemTouchHelper = ItemTouchHelper(itemTouchCallback) |
||||
itemTouchHelper.attachToRecyclerView(binding.recyclerView) |
||||
} |
||||
|
||||
override fun initClick() { |
||||
super.initClick() |
||||
binding.rlBookGroup.setOnClickListener { |
||||
openGroup = !openGroup |
||||
binding.scBookGroup.isChecked = openGroup |
||||
SharedPreUtils.getInstance().putBoolean("openGroup", openGroup) |
||||
binding.recyclerView.visibility = if (openGroup) View.VISIBLE else View.GONE |
||||
setResult(RESULT_OK) |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,126 @@ |
||||
package xyz.fycz.myreader.ui.adapter |
||||
|
||||
import android.annotation.SuppressLint |
||||
import android.content.Context |
||||
import android.content.res.TypedArray |
||||
import android.os.AsyncTask |
||||
import android.util.TypedValue |
||||
import android.view.MotionEvent |
||||
import android.view.ViewGroup |
||||
import androidx.core.content.ContextCompat |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import org.jetbrains.anko.backgroundColor |
||||
import xyz.fycz.myreader.R |
||||
import xyz.fycz.myreader.application.App |
||||
import xyz.fycz.myreader.base.adapter2.ItemViewHolder |
||||
import xyz.fycz.myreader.base.adapter2.RecyclerAdapter |
||||
import xyz.fycz.myreader.databinding.ItemGroupBinding |
||||
import xyz.fycz.myreader.greendao.entity.BookGroup |
||||
import xyz.fycz.myreader.greendao.service.BookGroupService |
||||
import xyz.fycz.myreader.ui.adapter.helper.IItemTouchHelperViewHolder |
||||
import xyz.fycz.myreader.ui.adapter.helper.ItemTouchCallback |
||||
import xyz.fycz.myreader.ui.adapter.helper.OnStartDragListener |
||||
import xyz.fycz.myreader.ui.dialog.BookGroupDialog |
||||
import xyz.fycz.myreader.ui.dialog.DialogCreator |
||||
import xyz.fycz.myreader.util.ToastUtils |
||||
|
||||
/** |
||||
* @author fengyue |
||||
* @date 2021/8/30 9:25 |
||||
*/ |
||||
class BookGroupAdapter( |
||||
context: Context, |
||||
private val onStartDragListener: OnStartDragListener, |
||||
private val groupDialog: BookGroupDialog, |
||||
) : RecyclerAdapter<BookGroup, ItemGroupBinding>(context), |
||||
ItemTouchCallback.OnItemTouchListener { |
||||
|
||||
private var isMoved: Boolean = false |
||||
|
||||
override fun getViewBinding(parent: ViewGroup): ItemGroupBinding { |
||||
return ItemGroupBinding.inflate(inflater, parent, false) |
||||
} |
||||
|
||||
override fun convert( |
||||
holder: ItemViewHolder, |
||||
binding: ItemGroupBinding, |
||||
item: BookGroup, |
||||
payloads: MutableList<Any> |
||||
) { |
||||
binding.run { |
||||
tvGroupName.text = item.name |
||||
} |
||||
} |
||||
|
||||
@SuppressLint("ClickableViewAccessibility") |
||||
override fun registerListener(holder: ItemViewHolder, binding: ItemGroupBinding) { |
||||
binding.run { |
||||
ivIcon.setOnClickListener { |
||||
val item = getItem(holder.layoutPosition) |
||||
DialogCreator.createCommonDialog( |
||||
context, "确定删除分组[" + item?.name + "]?", |
||||
"删除后,该书籍分组将永久不再显示,是否继续删除?", true, |
||||
"确定", "取消", { _, _ -> |
||||
BookGroupService.getInstance() |
||||
.deleteBookGroup(item) |
||||
removeItem(holder.layoutPosition) |
||||
groupDialog.initBookGroups(false) |
||||
ToastUtils.showSuccess("分组[" + item?.name + "]删除成功!") |
||||
}, null |
||||
) |
||||
} |
||||
ivMove.setOnTouchListener { _, motionEvent -> |
||||
if (motionEvent.action == MotionEvent.ACTION_DOWN) { |
||||
//通知ItemTouchHelper开始拖拽 |
||||
onStartDragListener.onStartDrag(holder) |
||||
} |
||||
false |
||||
} |
||||
} |
||||
binding.root.setOnClickListener { |
||||
if (groupDialog.groupSize >= 50) { |
||||
ToastUtils.showWarring("分组数量不能超过50") |
||||
return@setOnClickListener |
||||
} |
||||
groupDialog.showAddOrRenameGroupDia(true, false, |
||||
holder.layoutPosition, object : BookGroupDialog.OnGroup() { |
||||
override fun change() { |
||||
App.getHandler().postDelayed( |
||||
{ notifyItemChanged(holder.layoutPosition) }, 300 |
||||
) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { |
||||
swapItem(srcPosition, targetPosition) |
||||
isMoved = true |
||||
return true |
||||
} |
||||
|
||||
override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { |
||||
if (isMoved) { |
||||
AsyncTask.execute { |
||||
for ((index, item) in getItems().withIndex()) { |
||||
item.num = index |
||||
} |
||||
BookGroupService.getInstance().updateGroups(getItems()) |
||||
groupDialog.initBookGroups(false) |
||||
} |
||||
} |
||||
isMoved = false |
||||
} |
||||
|
||||
/*override fun onItemSelected(viewHolder: RecyclerView.ViewHolder) { |
||||
viewHolder.itemView.backgroundColor = context.resources.getColor(R.color.colorBackground) |
||||
} |
||||
|
||||
override fun onItemClear(viewHolder: RecyclerView.ViewHolder) { |
||||
val typedValue = TypedValue() |
||||
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true) |
||||
val attribute = intArrayOf(android.R.attr.selectableItemBackground) |
||||
val typedArray = context.theme.obtainStyledAttributes(typedValue.resourceId, attribute) |
||||
viewHolder.itemView.background = typedArray.getDrawable(0) |
||||
}*/ |
||||
} |
@ -0,0 +1,31 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<set xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:interpolator="@android:anim/decelerate_interpolator" |
||||
> |
||||
|
||||
<translate |
||||
android:fromYDelta="200%" |
||||
android:toYDelta="0%" |
||||
android:duration="750" |
||||
/> |
||||
|
||||
<alpha |
||||
android:fromAlpha="0.0" |
||||
android:toAlpha="1.0" |
||||
android:duration="750"/> |
||||
|
||||
<!-- <scale--> |
||||
<!-- android:pivotX="50%"--> |
||||
<!-- android:pivotY="50%"--> |
||||
<!-- android:fromXScale="0.5"--> |
||||
<!-- android:fromYScale="0.5"--> |
||||
<!-- android:toXScale="1"--> |
||||
<!-- android:toYScale="1"--> |
||||
<!-- android:duration="750"--> |
||||
|
||||
<!-- />--> |
||||
|
||||
|
||||
|
||||
|
||||
</set> |
@ -0,0 +1,12 @@ |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:viewportWidth="1024" |
||||
android:viewportHeight="1024"> |
||||
<path |
||||
android:pathData="M767.98,815.98c-12.31,0 -24.56,-4.69 -33.94,-14.06l-512,-512c-18.75,-18.75 -18.75,-49.12 0,-67.88s49.13,-18.75 67.88,0l512,512c18.75,18.75 18.75,49.12 0,67.88 -9.38,9.38 -21.63,14.06 -33.94,14.06z" |
||||
android:fillColor="#ffffff"/> |
||||
<path |
||||
android:pathData="M255.98,815.98c-12.31,0 -24.56,-4.69 -33.94,-14.06 -18.75,-18.75 -18.75,-49.12 0,-67.88l512,-512c18.75,-18.75 49.12,-18.75 67.88,0s18.75,49.12 0,67.88l-512,512c-9.38,9.38 -21.63,14.06 -33.94,14.06z" |
||||
android:fillColor="#ffffff"/> |
||||
</vector> |
@ -0,0 +1,12 @@ |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:viewportWidth="1024" |
||||
android:viewportHeight="1024"> |
||||
<path |
||||
android:pathData="M512,912.03c-26.5,0 -48,-21.5 -48,-48v-704c0,-26.5 21.5,-48 48,-48s48,21.5 48,48v704c0,26.5 -21.5,48 -48,48z" |
||||
android:fillColor="#ffffff"/> |
||||
<path |
||||
android:pathData="M864,560.03H160c-26.5,0 -48,-21.5 -48,-48s21.5,-48 48,-48h704c26.5,0 48,21.5 48,48s-21.5,48 -48,48z" |
||||
android:fillColor="#ffffff"/> |
||||
</vector> |
@ -0,0 +1,60 @@ |
||||
<?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="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<include layout="@layout/toolbar" /> |
||||
|
||||
<RelativeLayout |
||||
android:id="@+id/rl_book_group" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="60dp" |
||||
android:background="@drawable/selector_common_bg" |
||||
android:gravity="center" |
||||
android:paddingLeft="20dp" |
||||
android:paddingRight="20dp"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_centerVertical="true" |
||||
android:background="@drawable/selector_common_bg" |
||||
android:orientation="vertical"> |
||||
|
||||
<TextView |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:text="@string/book_group" |
||||
android:textColor="@color/textSecondary" |
||||
android:textSize="@dimen/text_normal_size" /> |
||||
|
||||
<TextView |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:paddingTop="5dp" |
||||
android:text="@string/book_group_tip" |
||||
android:textColor="@color/textAssist" /> |
||||
</LinearLayout> |
||||
|
||||
<androidx.appcompat.widget.SwitchCompat |
||||
android:id="@+id/sc_book_group" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignParentEnd="true" |
||||
android:layout_centerVertical="true" |
||||
android:clickable="false" |
||||
android:longClickable="false" /> |
||||
</RelativeLayout> |
||||
|
||||
<View |
||||
android:layout_width="match_parent" |
||||
android:layout_height="15dp" /> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/recycler_view" |
||||
android:background="@color/colorForeground" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"/> |
||||
|
||||
</LinearLayout> |
@ -0,0 +1,42 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="50dp" |
||||
android:background="?android:attr/selectableItemBackground" |
||||
android:gravity="center_vertical" |
||||
android:paddingHorizontal="10dp" |
||||
android:paddingVertical="5dp"> |
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView |
||||
android:id="@+id/iv_icon" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="match_parent" |
||||
android:layout_alignParentStart="true" |
||||
android:layout_centerVertical="true" |
||||
android:paddingHorizontal="5dp" |
||||
app:srcCompat="@drawable/ic_cross" |
||||
app:tint="@color/md_red_600" /> |
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView |
||||
android:id="@+id/tv_group_name" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_centerInParent="true" |
||||
android:layout_toStartOf="@id/iv_move" |
||||
android:layout_toEndOf="@+id/iv_icon" |
||||
android:paddingHorizontal="10dp" |
||||
android:text="@string/app_name" |
||||
android:textColor="@color/textPrimary" |
||||
android:textSize="@dimen/text_normal_size" /> |
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView |
||||
android:id="@+id/iv_move" |
||||
android:layout_width="30dp" |
||||
android:layout_height="match_parent" |
||||
android:layout_alignParentEnd="true" |
||||
android:layout_centerVertical="true" |
||||
android:padding="8dp" |
||||
app:srcCompat="@drawable/ic_line_spacing3" |
||||
app:tint="@color/textAssist" /> |
||||
</RelativeLayout> |
Loading…
Reference in new issue