按下后退按钮后,片段列表视图不会刷新

时间:2016-11-18 10:35:52

标签: android listview android-fragments

使用OnScroll侦听器实现CustomListview,以便从服务器加载内容。在backpress上,片段返回容器中的home片段。再次加载相同的列表视图,在按钮单击时,即使创建了子片段的新实例,也只有最后几个项被加载到列表视图中。

对数据集中的任何更改都会调用

notifydatasetchanged()

代码onclicklistener用于显示fragment

 card.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){                      
                        FragmentManager fragmentManager;
                        FragmentTransaction fragmentTransaction;
                        fragmentManager = getActivity().getSupportFragmentManager();
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentManager.popBackStack();
                        fragmentTransaction.replace(R.id.Container, new frag()).addToBackStack("card").commit();
}

Asynctask用于在onScroll方法中加载适配器。

lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int currentVisibleItemCount;
            private int currentScrollState;
            private int currentFirstVisibleItem;
            private int totalItem;
            // private LinearLayout lBelow;


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                this.currentScrollState = scrollState;
                this.isScrollCompleted();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                this.currentFirstVisibleItem = firstVisibleItem;
                this.currentVisibleItemCount = visibleItemCount;
                this.totalItem = totalItemCount;
            }

            private void isScrollCompleted() {
                if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                        && this.currentScrollState == SCROLL_STATE_IDLE) {
                    // if ( this.currentScrollState == SCROLL_STATE_IDLE){
                    /** To do code here*/

                    scrollpos = this.totalItem;
                    int lastPos = this.currentFirstVisibleItem + this.currentVisibleItemCount;
                    if (lastPos == scrollpos && !loadingMore) {
                        if (this.totalItem < jsonobjectcount-1) {
                            int index = lv.getFirstVisiblePosition();
                            new Card.Getmembers().execute();
                        } else {
                            lv.removeFooterView(pb);

                        }

                    }
                    }
                    }

0 个答案:

没有答案