android - SwipeRefreshLayout 和 TabLayout 问题

标签 android android-tablayout swiperefreshlayout

我有一个简单的项目,在 MainActivity 中有一个 TabLayout,有 3 个选项卡, 每个选项卡都有一个 SwipeRefreshLayout。

如果我拉动以刷新第一个选项卡,然后在第一个选项卡仍在刷新时移动到第三个选项卡,当我返回第一个选项卡时,看起来第一个选项卡上有一个 View ,其中包含在我移动到第三个选项卡之前选项卡的状态。我可以滚动项目并正常使用选项卡。

请看下面的图片和下面我的代码。

我可以关闭调用 swipeLayout.setRefreshing(false) 的“刷新指示器”;在 ContentFragment 的 onPause 中,但覆盖 View 停留在那里。

初始画面

Initial screen

出现问题的屏幕

Screen with the issue

主 Activity

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    TabLayout tabLayout;

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

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);

        PagerAdapter pageAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);

        tabLayout.setupWithViewPager(viewPager);
    }

    static class PagerAdapter extends FragmentPagerAdapter {

        private final int TAB_COUNT = 3;

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new ContentFragment();
        }

        @Override
        public int getCount() {
            return TAB_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Tab 1";
                case 1:
                    return "Tab 2";
                case 2:
                    return "Tab 3";
                default:
                    throw new IndexOutOfBoundsException("Invalid tab position");
            }
        }
    }
}

内容 fragment

public class ContentFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    RecyclerView recyclerView;
    private RecyclerAdapter adapter;

    SwipeRefreshLayout swipeLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);

        adapter = new RecyclerAdapter();

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);

        return view;
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                swipeLayout.setRefreshing(false);
            }
        }, 5000);
    }

    private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);

            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tvTitle.setText("Item #" + position);
        }

        @Override
        public int getItemCount() {
            return 50;
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            private final TextView tvTitle;

            public ViewHolder(View itemView) {
                super(itemView);

                tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            }

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        app:tabGravity="fill" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_below="@id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragment 内容.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

recyclerview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/tvTitle" />
</RelativeLayout>

最佳答案

我找到的解决方案是在 fragment View 被销毁时删除所有 SwipeRefreshLayout View :

@Override 
public void onDestroyView() {
    swipeLayout.removeAllViews();
    super.onDestroyView();
}

关于android - SwipeRefreshLayout 和 TabLayout 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133047/

相关文章:

java - 使用 Rest 服务找不到 RequestParams

android - 如何更改android studio底部导航栏上选择的图标颜色

android - TabLayout Indicator 在选择 Tab 时滑动非常慢

java - 访问 CordovaWebView 对象

java - 从github导入android项目到Eclipse后出现问题

android - 如何创建动态选项卡并将不同的产品加载到每个 fragment ?

安卓 : How to create TabLayout with drawable for count textview?

android - 如何将颜色资源 ID 的 int 数组从 array.xml 传递到 SwipeRefreshLayout.setColorSchemeResources

android - 在 AppBarLayout 的 Toolbar 下制作 SwipeRefreshLayout

android - SwipeRefreshLayout 隐藏在带有透明状态栏的 ActionBar 后面