Viewpager Recyclerview滚动滞后

时间:2017-05-28 06:22:43

标签: android android-layout android-fragments

我试图为有4个片段(固定)的主页实现viewpager。这些碎片本身并不滞后(我确信这是因为它们之前是通过导航抽屉实现的。)这些碎片包含了一个滞后的recyclerview。它包含图像。为了加载图像我使用Glide。我已经尝试将setEnabledNestedScrollingEnabled设置为false,但没有这样的运气。出于测试目的,我只是测试一个片段,即使这样也是滞后。

MainActivity

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_One

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();
    }

.....
.....

}

1 个答案:

答案 0 :(得分:1)

如果您的recyclerView滞后加载图片,则可能是您的图片尺寸可能较大,请尝试压缩图片尺寸。您可以使用Android资产工作室压缩图像,这会根据所有不同的设备将图像转换为不同的尺寸。 Android资产工作室: - https://romannurik.github.io/AndroidAssetStudio/