繁体   English   中英

Android片段彼此重叠

[英]Android Fragments Overlapping each other

在我的应用程序中,我有两个片段: FragmentAFragmentB ,它们是通过SlidingMenuMainActivity加载的。

显示FragmentB ,它将立即在后台启动AsyncTask来从服务器加载数据。 激活过程SwipeRefreshLayout向用户反映了加载过程。

加载完成后,借助于传递给MyAsyncClass的委托,使用加载的数据刷新FragmentB的UI。

我现在面临以下问题: processFinish我的委托中的processFinish ,我将借助swipeRefreshLayout.setRefreshing(false)停止SwipeRefreshLayout的刷新。 如果用户要回FragmentA而后台任务还在进行中,它出现在的情况下,约20%,即FragmentB仍处于后台FragmentA在用户界面(见下图)。

如您在图像中所看到的,显示了FragmentA (文本“尚未播放曲目”来自FragmentA ),但是在背景中您仍然可以看到FragmentB

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

任何想法导致这种行为的原因吗?

问题的屏幕截图

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

感谢Prsnth Gettin High和这篇文章( 在刷新过程中使用SwipeRefreshLayout切换片段时,片段会冻结,但实际上仍然可以正常工作 ),将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>

尝试重用视图。 它可能会解决此问题。

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

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

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

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

请问您为什么要使用可运行的交换片段?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM