Add ability to scroll to top by tab click at the Account activity (#1146)
* Issue: tuskyapp#1078 Add ability to scroll to top by tab click at the Account activity * Fix issue with scroll tabs other than current * Update scroll on click behavior * Update code formatting * Remove unused code * Move tab click listener from Fragments to Activities
This commit is contained in:
parent
a2fa49aafb
commit
01234bb94b
8 changed files with 146 additions and 108 deletions
|
@ -15,17 +15,25 @@
|
|||
|
||||
package com.keylesspalace.tusky.pager;
|
||||
|
||||
import android.util.SparseArray;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.keylesspalace.tusky.fragment.AccountMediaFragment;
|
||||
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
|
||||
public class AccountPagerAdapter extends FragmentPagerAdapter {
|
||||
private static final int TAB_COUNT = 4;
|
||||
private String accountId;
|
||||
private String[] pageTitles;
|
||||
|
||||
private SparseArray<Fragment> fragments = new SparseArray<>(TAB_COUNT);
|
||||
|
||||
public AccountPagerAdapter(FragmentManager manager, String accountId) {
|
||||
super(manager);
|
||||
this.accountId = accountId;
|
||||
|
@ -58,11 +66,31 @@ public class AccountPagerAdapter extends FragmentPagerAdapter {
|
|||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 4;
|
||||
return TAB_COUNT;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
||||
Object fragment = super.instantiateItem(container, position);
|
||||
if (fragment instanceof Fragment)
|
||||
fragments.put(position, (Fragment) fragment);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
|
||||
super.destroyItem(container, position, object);
|
||||
fragments.remove(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return pageTitles[position];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Fragment getFragment(int position) {
|
||||
return fragments.get(position);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
package com.keylesspalace.tusky.pager
|
||||
|
||||
import android.util.SparseArray
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import androidx.fragment.app.FragmentPagerAdapter
|
||||
|
@ -22,6 +24,7 @@ import androidx.viewpager.widget.PagerAdapter
|
|||
import com.keylesspalace.tusky.TabData
|
||||
|
||||
class MainPagerAdapter(val tabs: List<TabData>, manager: FragmentManager) : FragmentPagerAdapter(manager) {
|
||||
private val fragments = SparseArray<Fragment>(tabs.size)
|
||||
|
||||
override fun getItem(position: Int): Fragment {
|
||||
val tab = tabs[position]
|
||||
|
@ -44,4 +47,17 @@ class MainPagerAdapter(val tabs: List<TabData>, manager: FragmentManager) : Frag
|
|||
return PagerAdapter.POSITION_NONE
|
||||
}
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||
val fragment = super.instantiateItem(container, position)
|
||||
if (fragment is Fragment)
|
||||
fragments.put(position, fragment)
|
||||
return fragment
|
||||
}
|
||||
|
||||
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
|
||||
super.destroyItem(container, position, `object`)
|
||||
fragments.remove(position)
|
||||
}
|
||||
|
||||
fun getFragment(position: Int): Fragment? = fragments[position]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue