SwipeRefreshLayout 和 TabLayout 问题
SwipeRefreshLayout and TabLayout issue
我有一个简单的项目,在 MainActivity 中有一个 TabLayout,有 3 个选项卡,
每个选项卡都有一个 SwipeRefreshLayout。
如果我拉动以刷新第一个选项卡,然后在第一个选项卡仍在刷新时移动到第三个选项卡,当我返回第一个选项卡时,看起来第一个选项卡上有一个带有快照的视图在我移动到第三个选项卡之前选项卡的状态。我可以滚动项目并正常使用选项卡。
请看下面的图片和下面我的代码。
我可以关闭 "refresh indicator" 调用 swipeLayout.setRefreshing(false);在 ContentFragment 的 onPause 中,但覆盖视图停留在那里。
初始屏幕
屏幕有问题
主要活动
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");
}
}
}
}
内容片段
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_content.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>
我找到的解决方案是在片段视图被销毁时删除所有 SwipeRefreshLayout 视图:
@Override
public void onDestroyView() {
swipeLayout.removeAllViews();
super.onDestroyView();
}
我有一个简单的项目,在 MainActivity 中有一个 TabLayout,有 3 个选项卡, 每个选项卡都有一个 SwipeRefreshLayout。
如果我拉动以刷新第一个选项卡,然后在第一个选项卡仍在刷新时移动到第三个选项卡,当我返回第一个选项卡时,看起来第一个选项卡上有一个带有快照的视图在我移动到第三个选项卡之前选项卡的状态。我可以滚动项目并正常使用选项卡。
请看下面的图片和下面我的代码。
我可以关闭 "refresh indicator" 调用 swipeLayout.setRefreshing(false);在 ContentFragment 的 onPause 中,但覆盖视图停留在那里。
初始屏幕
屏幕有问题
主要活动
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");
}
}
}
}
内容片段
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_content.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>
我找到的解决方案是在片段视图被销毁时删除所有 SwipeRefreshLayout 视图:
@Override
public void onDestroyView() {
swipeLayout.removeAllViews();
super.onDestroyView();
}