如何使用ActionBar选项卡在TabReselected上重新加载片段或刷新片段

时间:2014-02-21 14:54:29

标签: java android android-fragments

描述 ::

  1. 有一个ActionBarTab(3-tab)设置,我们正在链接三个片段 对于每个Action-Tab,每个片段包含listview
  2. 每当我们点击Tab时,我们都会对数据进行排序。
  3. onTabReselect我使用接口方法调用sort函数, 没有错误,它会执行但不刷新listview

  4. MainActivity.java

    public class MainActivity extends Activity {
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Toast.makeText(this, DosUtils.ExecuteDOSCommand(), Toast.LENGTH_SHORT);
    
            // Initialize
            FindMyBuffetApplicationCls.initFindMyBuffetApplicationCls(MainActivity.this);
    
            setContentView(R.layout.main_activity);
            appContext = getApplicationContext();
    
            //ActionBar
            ActionBar actionbar = getActionBar();
            actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            ActionBar.Tab RatingTab = actionbar.newTab().setText(FindMyBuffetConstants.TAB_NAME_RATING);
            ActionBar.Tab PriceTab = actionbar.newTab().setText(FindMyBuffetConstants.SORT_BY_PRICE);
            ActionBar.Tab DistanceTab = actionbar.newTab().setText(FindMyBuffetConstants.SORT_BY_DISTANCE);
    
            Fragment SortRestFragmentByRating = new SortRestaurantRating();
            Fragment SortRestFragmentByPrice = new SortRestaurantByPrice();
            Fragment SortRestFragmentByDistance = new SortRestaurantByDistance();
    
            RatingTab.setTabListener(new MyTabsListener(SortRestFragmentByRating));
            PriceTab.setTabListener(new MyTabsListener(SortRestFragmentByPrice));
            DistanceTab.setTabListener(new MyTabsListener(SortRestFragmentByDistance));
    
            actionbar.addTab(RatingTab);
            actionbar.addTab(PriceTab);
            actionbar.addTab(DistanceTab);
    
            FindMyBuffetApplicationCls.comm = (Communicator) new SortRestaurantByPrice();
        }
    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;
    
        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            if(tab.getText().toString()==FindMyBuffetConstants.TAB_NAME_RATING){
                if(FindMyBuffetApplicationCls.isRatingOrderByDesc==false){
                    FindMyBuffetApplicationCls.isRatingOrderByDesc=true;
                }
                else {
                    FindMyBuffetApplicationCls.isRatingOrderByDesc=false;
                }
    
            }
    
            if(tab.getText().toString()==FindMyBuffetConstants.SORT_BY_PRICE){
                if(FindMyBuffetApplicationCls.isPriceOrderByDesc==false){
                    FindMyBuffetApplicationCls.isPriceOrderByDesc=true;
                }
                else {
                    FindMyBuffetApplicationCls.isPriceOrderByDesc=false;
                }
    
                FindMyBuffetApplicationCls.comm.RefreshFragment();
            }
    
            if(tab.getText().toString()==FindMyBuffetConstants.SORT_BY_DISTANCE){
                if(FindMyBuffetApplicationCls.isDistanceOrderByDesc==false){
                    FindMyBuffetApplicationCls.isDistanceOrderByDesc=true;
                }
                else {
                    FindMyBuffetApplicationCls.isDistanceOrderByDesc=false;
                }
            }
    
        }
    
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
    
            ft.replace(R.id.main_container, fragment);
            if(tab.getText().toString()==FindMyBuffetConstants.TAB_NAME_RATING){
                if(FindMyBuffetApplicationCls.isRatingOrderByDesc==false)
                    FindMyBuffetApplicationCls.isRatingOrderByDesc=true;
                else 
                    FindMyBuffetApplicationCls.isRatingOrderByDesc=false;
            }
    
            if(tab.getText().toString()==FindMyBuffetConstants.SORT_BY_PRICE){
                if(FindMyBuffetApplicationCls.isPriceOrderByDesc==false)
                    FindMyBuffetApplicationCls.isPriceOrderByDesc=true;
                else 
                    FindMyBuffetApplicationCls.isPriceOrderByDesc=false;
            }
    
            if(tab.getText().toString()==FindMyBuffetConstants.SORT_BY_DISTANCE){
                if(FindMyBuffetApplicationCls.isDistanceOrderByDesc==false)
                    FindMyBuffetApplicationCls.isDistanceOrderByDesc=true;
                else 
                    FindMyBuffetApplicationCls.isDistanceOrderByDesc=false;
            }
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);
            Log.i("TEST TAB",tab.getText().toString());
    
        }
    
    }
    

    SortRestaurantByPrice.java

    public class SortRestaurantByPrice extends Fragment implements Communicator{
        // Declaration
        ListView xmlFragmentListView;
        SortRestaurantListViewAdapter listViewAdapter;
        View layout;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            layout = inflater.inflate(R.layout.sort_restaurant_by_price, container,false);
            xmlFragmentListView = ((ListView) layout.findViewById(R.id.sortrestaurantbypricelistview));
            return layout;
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            //layout = null; // now cleaning up!
        }
    
        private void SortRestaurantList(String strSortBy)
                throws Exception, TransformerConfigurationException,
                TransformerException, FindMyBuffetException {
    
            // Process the Main Screen Data
            List<RestaurantDetails> lstRest = null;     
                        lstRest = SAXXMLParser.parse(xmlInputStream);
                        SortRestaurantListViewAdapter listViewAdapter = new SortRestaurantListViewAdapter(lstRest);
                        xmlFragmentListView.setAdapter(listViewAdapter);
    
        }
    
        @Override
        public void RefreshFragment() {
            // TODO Auto-generated method stub
            Log.i("hELLO", "refresh requested. Try to reload data for this fragment...");
                SortRestaurantList("Price");
    
        }
    }
    

    Communicator.java

    public interface Communicator {
        public void RefreshFragment();
    }
    

1 个答案:

答案 0 :(得分:0)

请使用sortRestaurantList方法在列表中调用notifyDataChanged方法。

 private void SortRestaurantList(String strSortBy)
        throws Exception, TransformerConfigurationException,
        TransformerException, FindMyBuffetException {

    // Process the Main Screen Data
    List<RestaurantDetails> lstRest = null;     
                lstRest = SAXXMLParser.parse(xmlInputStream);
                SortRestaurantListViewAdapter listViewAdapter = new SortRestaurantListViewAdapter(lstRest);
                xmlFragmentListView.setAdapter(listViewAdapter);
       listViewAdapter.notifyDataSetChanged(); // call this on Adapter

}

事实上,在onCreateView方法中移动适配器代码。只修改(排序)SortRestaurant方法中的List lstRest,然后调用notifyDataSetChanged方法。