Commit 3556eebf authored by Mygod's avatar Mygod

Tasker: scroll to selected profile

parent 10e3b211
...@@ -76,12 +76,11 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -76,12 +76,11 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
handler.post(() => text.setText(builder)) handler.post(() => text.setText(builder))
} }
def bind(item: Profile, i: Int) { def bind(item: Profile) {
this.item = item this.item = item
updateText() updateText()
if (item.id == ShadowsocksApplication.profileId) { if (item.id == ShadowsocksApplication.profileId) {
text.setChecked(true) text.setChecked(true)
selectedIndex = i
selectedItem = this selectedItem = this
} else { } else {
text.setChecked(false) text.setChecked(false)
...@@ -102,7 +101,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -102,7 +101,7 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
def getItemCount = profiles.length def getItemCount = profiles.length
def onBindViewHolder(vh: ProfileViewHolder, i: Int) = vh.bind(profiles(i), i) def onBindViewHolder(vh: ProfileViewHolder, i: Int) = vh.bind(profiles(i))
def onCreateViewHolder(vg: ViewGroup, i: Int) = def onCreateViewHolder(vg: ViewGroup, i: Int) =
new ProfileViewHolder(LayoutInflater.from(vg.getContext).inflate(R.layout.layout_profiles_item, vg, false)) new ProfileViewHolder(LayoutInflater.from(vg.getContext).inflate(R.layout.layout_profiles_item, vg, false))
...@@ -154,7 +153,6 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -154,7 +153,6 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
} }
} }
private var selectedIndex = -1
private var selectedItem: ProfileViewHolder = _ private var selectedItem: ProfileViewHolder = _
private val handler = new Handler private val handler = new Handler
...@@ -181,10 +179,9 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe ...@@ -181,10 +179,9 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
profilesList.setLayoutManager(layoutManager) profilesList.setLayoutManager(layoutManager)
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
if (selectedIndex < 0) selectedIndex = profilesAdapter.profiles.zipWithIndex.collectFirst { layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
case (profile, i) if profile.id == ShadowsocksApplication.profileId => i case (profile, i) if profile.id == ShadowsocksApplication.profileId => i
}.getOrElse(-1) }.getOrElse(-1))
layoutManager.scrollToPosition(selectedIndex)
removedSnackbar = Snackbar.make(profilesList, R.string.removed, Snackbar.LENGTH_LONG) removedSnackbar = Snackbar.make(profilesList, R.string.removed, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, ((v: View) => profilesAdapter.undoRemoves): OnClickListener) .setAction(R.string.undo, ((v: View) => profilesAdapter.undoRemoves): OnClickListener)
removedSnackbar.getView.addOnAttachStateChangeListener(new OnAttachStateChangeListener { removedSnackbar.getView.addOnAttachStateChangeListener(new OnAttachStateChangeListener {
......
...@@ -82,7 +82,7 @@ class TaskerActivity extends AppCompatActivity { ...@@ -82,7 +82,7 @@ class TaskerActivity extends AppCompatActivity {
} }
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] { private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
private val profiles = ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) val profiles = ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
def getItemCount = 1 + profiles.length def getItemCount = 1 + profiles.length
def onBindViewHolder(vh: ProfileViewHolder, i: Int) = i match { def onBindViewHolder(vh: ProfileViewHolder, i: Int) = i match {
case 0 => vh.bindDefault case 0 => vh.bindDefault
...@@ -110,9 +110,13 @@ class TaskerActivity extends AppCompatActivity { ...@@ -110,9 +110,13 @@ class TaskerActivity extends AppCompatActivity {
switch = findViewById(R.id.serviceSwitch).asInstanceOf[Switch] switch = findViewById(R.id.serviceSwitch).asInstanceOf[Switch]
switch.setChecked(taskerOption.switchOn) switch.setChecked(taskerOption.switchOn)
val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView] val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
profilesList.setLayoutManager(new LinearLayoutManager(this)) val lm = new LinearLayoutManager(this)
profilesList.setLayoutManager(lm)
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
if (taskerOption.profileId >= 0) lm.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
case (profile, i) if profile.id == taskerOption.profileId => i + 1
}.getOrElse(0))
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment