Android碎片相互重叠

时间:2017-05-06 17:02:15

标签: android multithreading android-fragments android-asynctask swiperefreshlayout

在我的应用中,我有两个片段:FragmentAFragmentB,这些片段是在MainActivity的帮助下从SlidingMenu加载的。

显示FragmentB时,它会立即在后台启动AsyncTask以从服务器加载数据。加载过程由活动SwipeRefreshLayout反映给用户。

加载完成后,FragmentB的UI会在传递给MyAsyncClass的代理人的帮助下刷新加载的数据。

我现在面临以下问题: 一旦调用了我的委托中的processFinish,我就会在SwipeRefreshLayout的帮助下停止刷新swipeRefreshLayout.setRefreshing(false)。 如果用户在后台任务仍处于进行状态时返回FragmentA,则大约20%的情况会显示,FragmentB仍在用户的FragmentA后台界面(见下面的截图)。

正如您在图片中看到的那样,会显示FragmentA(文字"尚未播放的曲目"来自FragmentA),但在后台您仍可看到FragmentB

如前所述,这不是100%的情况,因此它不应该是背景/透明度问题。如果我注释掉swipeRefreshLayout.setRefreshing(false),那么问题就无法重现,但如果用户留在SwipeRefreshLayout,我需要停止FragmentB

知道是什么导致了这种行为吗?

Screenshot of the problem

AsyncResponse接口:

public interface AsyncResponse {

    void processFinish(String result);
}

FragmentB级:

    public class FragmentB extends MyFragment implements SwipeRefreshLayout.OnRefreshListener, AsyncResponse {

        private SwipeRefreshLayout swipeRefreshLayout;
        private View view;
        private RecyclerView recyclerView;

        public FragmentB () {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            view = inflater.inflate(R.layout.songs_list, container, false);

            swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
            swipeRefreshLayout.setOnRefreshListener(this);
            swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));

            // Set the adapter
            Context context = view.getContext();
            recyclerView = (RecyclerView) view.findViewById(R.id.listinclude);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));

            recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), R.drawable.divider));

            List<Song> songsList = databaseHandler.getAllSongs();
            setVisibilities(songsList);

            this.recyclerViewAdapter = new RecyclerViewAdapter(songsList, mListener, false, false);

            recyclerView.setAdapter(this.recyclerViewAdapter);

  swipeRefreshLayout.setRefreshing(true);
            receive();

            return view;
        }

        private void setVisibilities(List<Song> songsList) {
            ViewFlipper viewFlipper = (ViewFlipper) view.findViewById(R.id.viewFlipper);
            if (songsList.isEmpty() && viewFlipper.getDisplayedChild() == 0) {
                viewFlipper.setDisplayedChild(1);
            } else if (!songsList.isEmpty() && viewFlipper.getDisplayedChild() == 1) {
                viewFlipper.setDisplayedChild(0);
            }
        }


        @Override
        public void onRefresh() {
            if (MyOnlineHelper.isOnline(getContext())) {
                receive();
            } else {
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        private void receive() {
            new MyAsyncClass(this, getContext()).execute("receiving");
        }

        @Override
        public void processFinish(String output) {
                // refreshUI
                swipeRefreshLayout.setRefreshing(false);
        }
    }

FragmentA级:

public class FragmentA extends MyFragment implements AsyncResponse {

    private View view;
    private RecyclerView recyclerView;

    public FragmentA() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        List<Song> songsList = myDatabaseHandler.getAllSongs();

        view = inflater.inflate(R.layout.home_list, container, false);

        // Set the adapter
        Context context = view.getContext();
        recyclerView = (RecyclerView) view.findViewById(R.id.listinclude);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), R.drawable.divider));

        setVisibilities(songsList);

        this.recyclerViewAdapter = new RecyclerViewAdapter(songsList, mListener, false, true);

        recyclerView.setAdapter(this.recyclerViewAdapter);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    private void setVisibilities(List<Song> songsList) {
        ViewFlipper viewFlipper = (ViewFlipper) view.findViewById(R.id.viewFlipper);
        if (songsList.isEmpty() && viewFlipper.getDisplayedChild() == 0) {
            viewFlipper.setDisplayedChild(1);
        } else if (!songsList.isEmpty() && viewFlipper.getDisplayedChild() == 1) {
            viewFlipper.setDisplayedChild(0);
        }
    }


    @Override
    public void processFinish(String output) {
        // does something
    }
}

MyAsyncClass:

public class MyAsyncClass extends AsyncTask<String, Integer, String> {

        private AsyncResponse delegate;
        private Context mContext;

        private MyDatabaseHandler myDatabaseHandler;

        public MyAsyncClass(AsyncResponse delegate, Context context) {
            this.delegate = delegate;
            this.mContext = context;
        }


        @Override
        protected String doInBackground(String... params) {

            // calling webservice and writing it to the database
        }

        @Override
        protected void onPostExecute(String result) {
            delegate.processFinish(result);
            super.onPostExecute(result);
        }
    }

MainActivity级:

public class MainActivity extends AppCompatActivity implements MyFragment.OnListFragmentInteractionListener, AsyncResponse {


    private FragmentA fragmentA = new FragmentA();
    private FragmentB fragmentB;

    private NavigationView navigationView;
    private DrawerLayout drawer;
    private Toolbar toolbar;

    // index to identify current nav menu item
    private static int navItemIndex = 0;

    public static String CURRENT_TAG = MyConstants.TAG_FRAGMENT_A;

    // toolbar titles respected to selected nav menu item
    private String[] activityTitles;

    // flag to load home fragment when user presses back key
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        fragmentA.setDatabaseHandler(this.myDatabaseHandler);

        // Init UI
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mHandler = new Handler();

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);

        fabSendButton = (FloatingActionButton) findViewById(R.id.fab);

        // Navigation view header
        navHeader = navigationView.getHeaderView(0);


        // load toolbar titles from string resources
        activityTitles = getResources().getStringArray(R.array.sliding_menu_item_activity_titles);


        // initializing navigation menu
        setUpNavigationView();

        if (savedInstanceState == null) {
            navItemIndex = 0;
            CURRENT_TAG = MyConstants.TAG_FRAGMENT_A;
            loadHomeFragment();
        }
    }

    /***
     * Returns respected fragment that user
     * selected from navigation menu
     */
    private void loadHomeFragment() {

        // set toolbar title
        setToolbarTitle();

        // if user select the current navigation menu again, don't do anything
        // just close the navigation drawer
        if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
            drawer.closeDrawers();
            return;
        }

        // Sometimes, when fragment has huge data, screen seems hanging
        // when switching between navigation menus
        // So using runnable, the fragment is loaded with cross fade effect
        // This effect can be seen in GMail app
        Runnable mPendingRunnable = new Runnable() {
            @Override
            public void run() {
                // update the activity_main_header_with_item content by replacing fragments
                Fragment fragment = getFragment();
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
                fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
                fragmentTransaction.commit();
            }
        };

        // If mPendingRunnable is not null, then add to the message queue
        if (mPendingRunnable != null) {
            mHandler.post(mPendingRunnable);
        }

        //Closing drawer on item click
        drawer.closeDrawers();

        // refresh toolbar menu
        invalidateOptionsMenu();
    }

    private Fragment getFragment() {
        switch (navItemIndex) {
            case 0:
                return this.fragmentA;
            case 1:
                if (fragmentB == null) {
                    fragmentB = new FragmentB();
                    fragmentB.setDatabaseHandler(this.myDatabaseHandler);
                }
                return fragmentB;
            default:
                return this.fragmentA;
        }
    }

    private void setToolbarTitle() {
        getSupportActionBar().setTitle(activityTitles[navItemIndex]);
    }

    private void setUpNavigationView() {
        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {
                    //Replacing the activity_main_header_with_item content with ContentFragment Which is our Inbox View;
                    case R.id.nav_A:
                        navItemIndex = 0;
                        CURRENT_TAG = MyConstants.TAG_FRAGMENT_A;
                        break;
                    case R.id.nav_B:
                        navItemIndex = 1;
                        CURRENT_TAG = MyConstants.TAG_FRAGMENT_B;
                        break;
                    default:
                        navItemIndex = 0;
                }

                loadHomeFragment();

                return true;
            }
        });

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawer.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessary or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_header_with_item, menu);
        return true;
    }
}

3 个答案:

答案 0 :(得分:2)

感谢Prsnth Gettin High和这篇文章(When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work),将SwipeRefreshLayout包装在FrameLayout中会有所帮助:

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

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                  android:id="@+id/swipe_refresh_layout"
                                                  android:layout_width="match_parent"
                                                  android:layout_height="match_parent">

        <ViewFlipper
            android:id="@+id/viewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/listinclude"
                layout="@layout/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:visibility="visible"/>

            <include
                android:id="@+id/emptyinclude"
                layout="@layout/fragment_empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible"/>
        </ViewFlipper>

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

答案 1 :(得分:0)

尝试重复使用该视图。它可能会解决问题。

`@Override     public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){

if(view == null){view = inflater.inflate(R.layout.songs_list,container,false);}`

答案 2 :(得分:0)

尝试将片段对象向下转换为特定片段。

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = getFragment();
FragmentA fragA;
FragmentB fragB;
if(fragment intanceof  FragmentA)
{
    fragA=(FragmentA) fragment;
    fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    fragmentTransaction.replace(R.id.frame, fragA, FragmentA.TAG);
}
if(fragment instanceof FragmentB)
{
    fragB=(FragmentB) fragment;
     fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
     fragmentTransaction.replace(R.id.frame, fragB, FragmentB.TAG);
}
fragmentTransaction.commit();

我可以问你为什么要使用runnable交换片段?