gpt4 book ai didi

android - Viewpager Recyclerview 滚动滞后

转载 作者:行者123 更新时间:2023-11-30 00:31:42 25 4
gpt4 key购买 nike

我正在尝试为具有 4 个 fragment (已修复)的主页实现 viewpager。 fragment 本身并不滞后(我确信这是因为它们之前是通过抽屉导航实现的)。 fragment 包含滞后的 recyclerview。它包含图像。我使用 Glide 加载图像。我试过将 setEnabledNestedScrollingEnabled 设置为 false,但没有这样的运气。出于测试目的,我只是用一个 fragment 进行测试,即便如此它还是滞后的。

主要 Activity

package com.example.anubhavr.firebase;


public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {R.drawable.fragment_one, R.drawable.fragment_two, R.drawable.fragment_three, R.drawable.fragment_four};
private String[] names = {"Fragment 1", "Fragment 2", "Fragment 3", "Fragment 4"};
private int isOffline;
private FloatingActionButton floatingActionButton;
private static int currentTab = 0;
private final int RC_SIGN_IN = 1;
private GoogleApiClient mGoogleApiClient;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tab_view_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_profile: {
viewProfile();
return true;
}
case R.id.menu_sign_out: {
signout();
return true;
}
case R.id.menu_store:
{
startVisitingStore();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
private void startVisitingStore(){
Intent i = new Intent(this, StoreActivity.class);
startActivity(i);
}

private void viewProfile() {

Intent i = new Intent(this, ProfileActivity.class);
startActivity(i);

}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

isOffline = getIntent().getIntExtra("isOffline", 0); //1 means is offline, 0 means is online
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}


private void setupTabIcons() {

final TabLayout.Tab tab = tabLayout.getTabAt(0);
Glide.with(this)
.load(tabIcons[0])
.override(42,42)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
tab.setIcon(resource);
}
});

}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
Fragment_One one = new Fragment_One();
Bundle bundle = new Bundle();
bundle.putInt("isOffline", isOffline);
mainActivity.setArguments(bundle);
adapter.addFrag(one, names[0]);

viewPager.setAdapter(adapter);

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
toolbar.setTitle(names[position]);
currentTab = position;

}

@Override
public void onPageScrollStateChanged(int state) {

}
});

}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return null;
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent">


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>


<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</RelativeLayout>

fragment 一

public class Fragment_One extends Fragment implements GEventLoadMore {

......
......

public void setView(final ArrayList<GEventAdapter> eventsArr) {


listView = (RecyclerView) view.findViewById(R.id.listView);
listView.setNestedScrollingEnabled(false);

RecyclerView.ItemAnimator animator = listView.getItemAnimator();

if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}

mLinearLayoutManager = new LinearLayoutManager(getContext());

listView.addItemDecoration(new VerticalSpaceItemDecoration(2));

listView.setLayoutManager(mLinearLayoutManager);

adapter = new Fragment_One_CustomAdapter(getContext(), R.layout.example, eventsArr);

listView.setAdapter(adapter);


listView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);

if (eventsArr.size() > 0) {
int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == recyclerView.getChildCount()) {
//progressDialog.show();
if(isOffline==0)
loadMoreFunction();

}
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {


}
});

restoreScrollPosition();
}

.....
.....

}

最佳答案

如果你的 recyclerView 加载图像滞后,那么可能是你的图像尺寸可能很大,然后尝试压缩你的图像尺寸。您可以使用 Android asset studio 压缩您的图像,这将根据所有不同的设备将您的图像转换为不同的尺寸。安卓 Assets 工作室:- https://romannurik.github.io/AndroidAssetStudio/

关于android - Viewpager Recyclerview 滚动滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com