首页 > 解决方案 > 嵌套的 ViewPager 不刷新

问题描述

问题陈述:

我有一个活动,我使用了视图寻呼机并在其中膨胀了两个片段。

视图分页器中的第二个片段还必须包含一个视图分页器,其中包含另一个片段(如果您想知道如果只需要一个片段,为什么这是一个视图分页器,因为这是一个可配置的组件,并且可能有更多片段里面需要)。

第一个片段

第二个片段和里面的子片段

现在,当我们单击一个按钮时,我必须刷新第一个和第二个片段以及第二个片段内的子片段(在 ViewPager 中)。

问题是第一个和第二个片段正在更新,但第二个片段中的视图寻呼机内的子片段没有得到刷新。

单击按钮后会发生这种情况:

第一个片段刷新

第二个片段刷新但子片段没有

尝试过的解决方案:

我们尝试调试代码,尝试清除适配器和列表。没有任何效果。

欢迎任何指点或建议!

代码 :

MainActivity.java

package com.example.admin.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GuestListPagerAdapter adapter;
    private ViewPager viewPager;
    private Button button;

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

        viewPager = findViewById(R.id.view_pager);
        button = findViewById(R.id.button);

        setUpViewPager();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.clearFragmentList();
                adapter.addFragment(FirstFragment.newInstance("55"));
                adapter.addFragment(SecondFragment.newInstance("88"));
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setUpViewPager() {
        if (null == adapter)
            adapter = new GuestListPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(FirstFragment.newInstance("1"));
        adapter.addFragment(SecondFragment.newInstance("2"));
        viewPager.setAdapter(adapter);
    }

    public class GuestListPagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> mFragments = new ArrayList<>();

        GuestListPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        void addFragment(Fragment fragment) {
            mFragments.add(fragment);
        }

        void clearFragmentList(){
            mFragments.clear();
        }

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

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

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10"
        tools:context=".MainActivity">


        <android.support.v4.view.ViewPager

        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"/>

    <Button
        android:text="Refresh View Pager"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"/>

</LinearLayout>

SecondFragment.java

package com.example.admin.myapplication;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class SecondFragment extends Fragment {
    private static final String TAG = SecondFragment.class.getSimpleName();
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;

    public SecondFragment() {
        // Required empty public constructor
    }

    public static SecondFragment newInstance(String param1) {
        SecondFragment fragment = new SecondFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: 2F");
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        Log.d(TAG, "onCreate: 2F");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv2);
        tv.setText(mParam1);

        ViewPager pager = (ViewPager) view.findViewById(R.id.sub_view_pager);
        SubListPagerAdapter adapter = new SubListPagerAdapter(getChildFragmentManager());
        adapter.clearFragmentList();

        adapter.addFragment(SubFragment.newInstance(mParam1));
        pager.setAdapter(adapter);
        Log.d(TAG, "onCreateView: 2F");
        return view;
    }

    public class SubListPagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> mFragments = new ArrayList<>();

        SubListPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        void addFragment(Fragment fragment) {
            mFragments.add(fragment);
        }

        void clearFragmentList(){
            mFragments.clear();
        }

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

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



        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

    }

}

fragment_second.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="4"
        tools:context=".SecondFragment">



    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv2"
        android:textSize="60sp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/hello_blank_fragment" />

    <android.support.v4.view.ViewPager
        android:background="@color/colorAccent"
        android:id="@+id/sub_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>

</LinearLayout>

子片段.java

package com.example.admin.myapplication;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SubFragment extends Fragment {
    private static final String TAG = SubFragment.class.getSimpleName();
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;

    public SubFragment() {
        // Required empty public constructor
    }

    public static SubFragment newInstance(String param1) {
        SubFragment fragment = new SubFragment();
        Bundle args = new Bundle();
        if(fragment.getArguments()!=null)
            fragment.getArguments().clear();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: sub");
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: sub");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_sub, container, false);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        TextView tv = (TextView) view.findViewById(R.id.sub_tv);
        tv.setText(mParam1);
        Log.d(TAG, "onCreateView: sub");
        return view;
    }


}

片段子.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/sub_tv"
        android:textSize="60dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment" />

</LinearLayout>

FirstFragment.java

package com.example.admin.myapplication;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {
    private static final String TAG = FirstFragment.class.getSimpleName();
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;


    public FirstFragment() {
        // Required empty public constructor
    }


    public static FirstFragment newInstance(String param1) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: 1F");
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        Log.d(TAG, "onCreate: 1F");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(mParam1);
        Log.d(TAG, "onCreateView: 1F");
        return view;
    }
}

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">


    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv"
        android:textSize="60sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

标签: javaandroidandroid-fragmentsandroid-viewpager

解决方案


推荐阅读