Skip to content

Commit 245437c

Browse files
authored
Merge pull request #26 from rosuH/dev
✨ 💄
2 parents 39bffc7 + 2123293 commit 245437c

File tree

3 files changed

+152
-9
lines changed

3 files changed

+152
-9
lines changed

filepicker/src/main/java/me/rosuh/filepicker/FilePickerActivity.kt

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.support.v4.content.ContextCompat
1313
import android.support.v4.widget.SwipeRefreshLayout
1414
import android.support.v7.widget.LinearLayoutManager
1515
import android.support.v7.widget.RecyclerView
16+
import android.util.SparseArray
1617
import android.view.LayoutInflater
1718
import android.view.View
1819
import android.view.animation.AnimationUtils
@@ -29,6 +30,7 @@ import me.rosuh.filepicker.bean.FileNavBeanImpl
2930
import me.rosuh.filepicker.config.FilePickerManager
3031
import me.rosuh.filepicker.utils.BaseActivity
3132
import me.rosuh.filepicker.utils.FileUtils
33+
import me.rosuh.filepicker.widget.PosLinearLayoutManager
3234
import me.rosuh.filepicker.widget.RecyclerViewFilePicker
3335
import java.io.File
3436
import java.util.concurrent.atomic.AtomicInteger
@@ -177,10 +179,28 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
177179

178180
private fun initLoadingView() {
179181
swipeRefreshLayout = findViewById(R.id.srl)
180-
swipeRefreshLayout?.setOnRefreshListener {
181-
reloadList()
182+
swipeRefreshLayout?.apply {
183+
setOnRefreshListener {
184+
reloadList()
185+
}
186+
isRefreshing = true
187+
setColorSchemeColors(*resources.getIntArray(
188+
when(pickerConfig.themeId){
189+
R.style.FilePickerThemeCrane ->{
190+
R.array.crane_swl_colors
191+
}
192+
R.style.FilePickerThemeReply ->{
193+
R.array.reply_swl_colors
194+
}
195+
R.style.FilePickerThemeShrine ->{
196+
R.array.shrine_swl_colors
197+
}
198+
else ->{
199+
R.array.rail_swl_colors
200+
}
201+
}
202+
))
182203
}
183-
swipeRefreshLayout?.isRefreshing = true
184204
}
185205

186206

@@ -201,7 +221,7 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
201221
emptyView = LayoutInflater.from(context).inflate(R.layout.empty_file_list_file_picker, null, false)
202222
adapter = listAdapter
203223
layoutAnimation = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_item_anim_file_picker)
204-
layoutManager = LinearLayoutManager(this@FilePickerActivity)
224+
layoutManager = PosLinearLayoutManager(this@FilePickerActivity)
205225
addOnItemTouchListener(fileListListener)
206226
}
207227
}
@@ -246,6 +266,9 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
246266
val file = File(item.filePath)
247267
if (!file.exists()) return
248268
if (file.isDirectory) {
269+
(rvNav?.adapter as? FileNavAdapter)?.let{
270+
saveCurrPos(it.data.last(), position)
271+
}
249272
// 如果是文件夹,则进入
250273
enterDirAndUpdateUI(item)
251274
} else {
@@ -255,6 +278,25 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
255278
}
256279
}
257280

281+
private val currPosMap: HashMap<String, Int> by lazy {
282+
HashMap<String, Int>(4)
283+
}
284+
private val currOffestMap: HashMap<String, Int> by lazy {
285+
HashMap<String, Int>(4)
286+
}
287+
288+
/**
289+
* 保存当前文件夹被点击项,下次进入时将滑动到此
290+
*/
291+
private fun saveCurrPos(item: FileNavBeanImpl?, position: Int){
292+
item?.run {
293+
currPosMap[filePath] = position
294+
(rvContentList?.layoutManager as? LinearLayoutManager)?.let {
295+
currOffestMap.put(filePath, it.findViewByPosition(position)?.top?:0)
296+
}
297+
}
298+
}
299+
258300
/**
259301
* 条目被长按
260302
*/
@@ -374,7 +416,7 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
374416
navAdapter?.data = navDataSource
375417

376418
navAdapter!!.notifyDataSetChanged()
377-
notifyDataChangedForList()
419+
notifyDataChangedForList(fileBean)
378420

379421
rvNav?.adapter?.itemCount?.let {
380422
rvNav?.smoothScrollToPosition(
@@ -388,8 +430,12 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
388430
}
389431
}
390432

391-
private fun notifyDataChangedForList() {
433+
private fun notifyDataChangedForList(fileBean: FileBean) {
392434
rvContentList?.apply {
435+
(layoutManager as? PosLinearLayoutManager)?.setTargetPos(
436+
currPosMap[fileBean.filePath] ?:0,
437+
currOffestMap[fileBean.filePath] ?:0
438+
)
393439
layoutAnimation = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_item_anim_file_picker)
394440
adapter?.notifyDataSetChanged()
395441
scheduleLayoutAnimation()
@@ -420,12 +466,13 @@ class FilePickerActivity : BaseActivity(), View.OnClickListener, RecyclerViewLis
420466
}
421467

422468
override fun onBackPressed() {
423-
if (navDataSource.size <= 1) {
469+
if ((rvNav?.adapter as? FileNavAdapter)?.itemCount?:0 <= 1) {
424470
super.onBackPressed()
425471
} else {
426472
// 即将进入的 item 的索引
427-
val willEnterItemPos = navDataSource.size - 2
428-
enterDirAndUpdateUI(navDataSource[willEnterItemPos])
473+
(rvNav?.adapter as? FileNavAdapter)?.run {
474+
enterDirAndUpdateUI(getItem(this.itemCount - 2)!!)
475+
}
429476
}
430477
}
431478

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.rosuh.filepicker.widget
2+
3+
import android.content.Context
4+
import android.os.Parcelable
5+
import android.support.v7.widget.LinearLayoutManager
6+
import android.support.v7.widget.RecyclerView
7+
import android.util.AttributeSet
8+
9+
class PosLinearLayoutManager : LinearLayoutManager {
10+
constructor(context: Context?) : super(context)
11+
constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(
12+
context,
13+
orientation,
14+
reverseLayout
15+
)
16+
17+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
18+
context,
19+
attrs,
20+
defStyleAttr,
21+
defStyleRes
22+
)
23+
24+
private var pendingTargetPos = -1
25+
26+
private var pendingPosOffset = -1
27+
28+
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
29+
if (pendingTargetPos != -1 && state?.itemCount?:0 > 0){
30+
scrollToPositionWithOffset(pendingTargetPos, pendingPosOffset)
31+
pendingPosOffset = -1
32+
pendingTargetPos = -1
33+
}
34+
super.onLayoutChildren(recycler, state)
35+
}
36+
37+
override fun onRestoreInstanceState(state: Parcelable?) {
38+
pendingTargetPos = -1
39+
pendingPosOffset = -1
40+
super.onRestoreInstanceState(state)
41+
}
42+
43+
fun setTargetPos(pos:Int, offset:Int){
44+
pendingTargetPos = pos
45+
pendingPosOffset = offset
46+
}
47+
}

filepicker/src/main/res/values/colors.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,74 @@
66
<color name="rail_button_focus">#01714b</color>
77
<color name="rail_text_color">#fff</color>
88
<color name="rail_textColor_Primary">#fff</color>
9+
<color name="rail_swl_color_1">#9900C381</color>
10+
<color name="rail_swl_color_2">#BF00C381</color>
11+
<color name="rail_swl_color_3">#CC00C381</color>
12+
<color name="rail_swl_color_4">#E600C381</color>
13+
<color name="rail_swl_color_5">#00c381</color>
14+
<integer-array name="rail_swl_colors">
15+
<item>@color/rail_swl_color_1</item>
16+
<item>@color/rail_swl_color_2</item>
17+
<item>@color/rail_swl_color_3</item>
18+
<item>@color/rail_swl_color_4</item>
19+
<item>@color/rail_swl_color_5</item>
20+
</integer-array>
21+
922

1023
<color name="reply_color_primary">#304956</color>
1124
<color name="reply_color_primary_dark">#446576</color>
1225
<color name="reply_color_accent">#ffa900</color>
1326
<color name="reply_button_focus">#865900</color>
1427
<color name="reply_text_color">#fff</color>
1528
<color name="reply_textColor_Primary">#fff</color>
29+
<color name="reply_swl_color_1">#99ffa900</color>
30+
<color name="reply_swl_color_2">#BFffa900</color>
31+
<color name="reply_swl_color_3">#CCffa900</color>
32+
<color name="reply_swl_color_4">#E6ffa900</color>
33+
<color name="reply_swl_color_5">#ffa900</color>
34+
<integer-array name="reply_swl_colors">
35+
<item>@color/reply_swl_color_1</item>
36+
<item>@color/reply_swl_color_2</item>
37+
<item>@color/reply_swl_color_3</item>
38+
<item>@color/reply_swl_color_4</item>
39+
<item>@color/reply_swl_color_5</item>
40+
</integer-array>
1641

1742
<color name="crane_color_primary">#7d005c</color>
1843
<color name="crane_color_primary_dark">#57003a</color>
1944
<color name="crane_color_accent">#f60000</color>
2045
<color name="crane_button_focus">#8f0000</color>
2146
<color name="crane_text_color">#fff</color>
2247
<color name="crane_textColor_Primary">#fff</color>
48+
<color name="crane_swl_color_1">#99f60000</color>
49+
<color name="crane_swl_color_2">#BFf60000</color>
50+
<color name="crane_swl_color_3">#CCf60000</color>
51+
<color name="crane_swl_color_4">#E6f60000</color>
52+
<color name="crane_swl_color_5">#f60000</color>
53+
<integer-array name="crane_swl_colors">
54+
<item>@color/crane_swl_color_1</item>
55+
<item>@color/crane_swl_color_2</item>
56+
<item>@color/crane_swl_color_3</item>
57+
<item>@color/crane_swl_color_4</item>
58+
<item>@color/crane_swl_color_5</item>
59+
</integer-array>
2360

2461
<color name="shrine_color_primary">#ffd9cd</color>
2562
<color name="shrine_color_primary_dark">#dfc3c2</color>
2663
<color name="shrine_color_accent">#48282c</color>
2764
<color name="shrine_button_focus">#301b1e</color>
2865
<color name="shrine_text_color">#48282c</color>
2966
<color name="shrine_textColor_Primary">#fff</color>
67+
<color name="shrine_swl_color_1">#99ffd9cd</color>
68+
<color name="shrine_swl_color_2">#BFffd9cd</color>
69+
<color name="shrine_swl_color_3">#CC48282c</color>
70+
<color name="shrine_swl_color_4">#E648282c</color>
71+
<color name="shrine_swl_color_5">#48282c</color>
72+
<integer-array name="shrine_swl_colors">
73+
<item>@color/shrine_swl_color_1</item>
74+
<item>@color/shrine_swl_color_2</item>
75+
<item>@color/shrine_swl_color_3</item>
76+
<item>@color/shrine_swl_color_4</item>
77+
<item>@color/shrine_swl_color_5</item>
78+
</integer-array>
3079
</resources>

0 commit comments

Comments
 (0)