Skip to content

Commit

Permalink
丰富sample选项
Browse files Browse the repository at this point in the history
  • Loading branch information
lilei committed Feb 20, 2019
1 parent c25fd01 commit fda4baa
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
63 changes: 59 additions & 4 deletions sample/src/main/java/me/hiten/jkcardlayout/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class MainActivity : AppCompatActivity() {
}

private fun updateConfigShow(){
val text = "卡片数:${mConfig.cardCount} | 偏移像素:${mConfig.offset} | 最大旋转角度:${mConfig.maxRotation} | 拖拽触发阈值${mConfig.swipeThreshold} | 下拉菜单阻尼:${pull_down_layout.getDragRatio()} | 下拉视觉差比例${pull_down_layout.getParallaxRatio()}"
val text = "卡片数:${mConfig.cardCount} | 偏移像素:${mConfig.offset} | 最大旋转角度:${mConfig.maxRotation} | 拖拽触发阈值:${mConfig.swipeThreshold} | 下拉菜单阻尼:${pull_down_layout.getDragRatio()} | 下拉视觉差比例:${pull_down_layout.getParallaxRatio()}"
val list = text.split("|")
val spannableString = SpannableString(text)
var start = 0
Expand All @@ -193,7 +193,31 @@ class MainActivity : AppCompatActivity() {
val end = start + item.length
spannableString.setSpan(object :ClickableSpan(){
override fun onClick(widget: View) {
showDialog(item)
when(index){
0->{
showDialog(index,item,1,mConfig.cardCount,3,1)
}
1->{
showDialog(index,item,0,mConfig.offset,20.dp,1)
}
2->{
val divisor = 2
showDialog(index,item,0,mConfig.maxRotation.toInt()*divisor,120,divisor)
}
3->{
val divisor = 10
showDialog(index,item,1, (mConfig.swipeThreshold*divisor).toInt(),8,divisor)
}
4->{
val divisor = 10
showDialog(index,item,2, (pull_down_layout.getDragRatio()*divisor).toInt(),12,divisor)
}
5->{
val divisor = 10
showDialog(index,item,8,(pull_down_layout.getParallaxRatio()*divisor).toInt(),15,divisor)
}

}
}
},start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
start+=item.length + 1
Expand All @@ -202,9 +226,40 @@ class MainActivity : AppCompatActivity() {
tv_show_config.movementMethod = LinkMovementMethod.getInstance()
}

private fun showDialog(title:String){
val settingDialogFragment = SettingDialogFragment.newInstance(title)
private fun showDialog(index:Int,title: String,start:Int,cur:Int,end:Int,divisor:Int){
val settingDialogFragment = SettingDialogFragment.newInstance(title,start,cur,end,divisor)
settingDialogFragment.show(supportFragmentManager,"dialog_setting")
settingDialogFragment.setCallback {
when(index){
0->{
mConfig.cardCount = it.toInt()
recycler_view.adapter?.notifyDataSetChanged()
}
1->{
mConfig.offset = it.toInt()
recycler_view.adapter?.notifyDataSetChanged()
}
2->{
mConfig.maxRotation = it.toFloat()
recycler_view.adapter?.notifyDataSetChanged()
}
3->{
mConfig.swipeThreshold = it.toFloat()
recycler_view.adapter?.notifyDataSetChanged()
}
4->{
pull_down_layout.setDragRatio(it.toFloat())
}
5->{
pull_down_layout.setParallaxRatio(it.toFloat())
}

else -> {

}
}
updateConfigShow()
}
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package me.hiten.jkcardlayout.sample

import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import kotlinx.android.synthetic.main.dialog_setting.*

class SettingDialogFragment : DialogFragment(){

var mStartValue:Int = 0
var mCurValue:Int = 0
var mEndValue:Int = 0
var mDivisor:Int = 1

var mProgress:Int =0

var mInitProgress:Int = 0

var mTitlePrefix:String? = null

companion object {

const val KEY_TITLE = "title"
const val KEY_START = "start"
const val KEY_CUR = "cur"
const val KEY_END = "end"
const val KEY_DIVISOR = "mDivisor"

fun newInstance(title: String):SettingDialogFragment{
fun newInstance(title: String,start:Int,cur:Int,end:Int,divisor:Int):SettingDialogFragment{
val settingDialogFragment = SettingDialogFragment()
val bundle = Bundle()
bundle.putString(KEY_TITLE,title)
bundle.putInt(KEY_START,start)
bundle.putInt(KEY_CUR,cur)
bundle.putInt(KEY_END,end)
bundle.putInt(KEY_DIVISOR,divisor)
settingDialogFragment.arguments = bundle
return settingDialogFragment
}
Expand All @@ -32,8 +51,64 @@ class SettingDialogFragment : DialogFragment(){
super.onViewCreated(view, savedInstanceState)
arguments?.let {
val title = it.getString(KEY_TITLE)
mStartValue = it.getInt(KEY_START)
mCurValue = it.getInt(KEY_CUR)
mEndValue = it.getInt(KEY_END)
mDivisor = it.getInt(KEY_DIVISOR,1)
tv_title.text = title
mTitlePrefix = title?.split(":")?.get(0)
val max = mEndValue - mStartValue
mProgress = mCurValue-mStartValue
mInitProgress = mProgress
seek_bar.max = max
seek_bar.progress = mProgress

seek_bar.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
mProgress = progress
updateTitle()
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {
}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
}

})
}
}

fun updateTitle(){
val num:Number
if (mDivisor==1) {
num = mProgress+mStartValue
}else{
num = (mProgress+mStartValue) / mDivisor.toFloat()
}
mTitlePrefix?.let {
tv_title.text = it.plus(":").plus(num)
}

}

override fun onDismiss(dialog: DialogInterface?) {
super.onDismiss(dialog)
if (mProgress == mInitProgress){
return
}
if (mDivisor==1) {
mCallback?.invoke(mProgress+mStartValue)
}else{
mCallback?.invoke((mProgress+mStartValue) / mDivisor.toFloat())
}
}


private var mCallback : ((Number) -> Unit)? = null

fun setCallback(callback:((Number) -> Unit)){
this.mCallback = callback
}

}
2 changes: 2 additions & 0 deletions sample/src/main/res/layout/dialog_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<android.support.v7.widget.AppCompatSeekBar
android:id="@+id/seek_bar"
android:max="10"
android:progress="1"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
Expand Down

0 comments on commit fda4baa

Please sign in to comment.