在listfragment被嵌套片段替换后,视图控件仍然存在

时间:2013-11-22 17:08:22

标签: java android android-layout android-fragments android-listfragment

我有一个标签式视图活动,每个标签中有3个标签和一个片段。这些片段由链接到viewpager的FragmentPageAdapter管理。

一切正常,直到我尝试获得第4个片段( ServiceSpecifNotListFragment ),该片段将是由{{管理的一个片段( ServicesListFragment )的嵌套片段。 1}}。

当我尝试用 ServiceSpecifNotListFragment 替换 ServicesListFragment 时。我设法通过 ServiceSpecifNotListFragment 仅替换 ServicesListFragment 中的列表元素, ServicesListFragment 中的其他视图控制器(按钮)仍然是....

我一直在想,问题在于我用于通过子片段替换片段的方法是FragmentPageAdapter service_list_fragment 是listfragment主线性布局的id其中包含使用id:transaction.replace(R.id.service_list_fragment, newFragment).commit();

定义的列表视图

我发现的最接近的Q& A是this one但据我所知,我正在正确使用片段容器ID。

以下是代码和布局:

ServicesListFragment 的布局(嵌套片段的父级)

@id/android:list

ServiceSpecifNotListFragment 的布局(要嵌套的片段)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/service_list_fragment">

      <Button
      android:id="@+id/addServiceButton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="AddService" />

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="0dip"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No data"/>
</LinearLayout>

主要活动xml布局(它基本上是对寻呼机的引用):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/service_notif_list_fragment" >    
     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="0dip"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No data"/>
</LinearLayout> 

ServiceListFragment的源代码,片段嵌套的调用在<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager>

onListItemClick

主要活动java代码

public class ServicesListFragment extends ListFragment  {

    private final String TAG = "ServicesListFragment";
    Button addButton = null;

    ServiceNotifRowAdapter adapter;
    ArrayList<NotifService> list = new ArrayList<NotifService>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Activity parent = getActivity();

        // some ommited code for populating adapter with list of stored subscriptions
            adapter = new ServiceNotifRowAdapter(getActivity().getApplicationContext(),list);
        setListAdapter(adapter);

    }

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

            View rootView = inflater.inflate(R.layout.servicelist_fragment, container, false);

            addButton = (Button) rootView.findViewById(R.id.addServiceButton);
            MainActivity m = (MainActivity) getActivity();
            addButton.setOnClickListener(m);
            return rootView;
        }

      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {

          NotifService  item = (NotifService) getListAdapter().getItem(position);
          Log.d(TAG, "selected service " + item.getServiceURI());


        Fragment newFragment = new ServiceSpecifNotListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("service", item.getServiceURI());
        newFragment.setArguments(bundle);
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.service_list_fragment, newFragment).commit();

      }

标签页适配器

public class MainActivity extends FragmentActivity implements TabListener, AddServiceDialogFragmentListener
, OnClickListener{

    private final String TAG = "MQTT main activity";

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;




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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, StatusListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ServicesListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ConfigFragment.class.getName()));

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),fragments);

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : MqttApplication.tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        // lock the screen in portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


}

1 个答案:

答案 0 :(得分:0)

只是一个替代解决方案:在代码中实现回调,并在ServiceListFragment中单击项目时要求您的视图寻呼机加载SpecificListNotFragment。看看并尝试一下!

 public class ServicesListFragment extends ListFragment  {

    private final String TAG = "ServicesListFragment";
    Button addButton = null;

    ServiceNotifRowAdapter adapter;
    ArrayList<NotifService> list = new ArrayList<NotifService>();

    private onListItemClickCallbackListener mCallback;

    public interface onListItemClickCallbackListener {
        public void replaceFragmentToServiveListNot();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Activity parent = getActivity();
        mCallback = parent; // remember to imlement onListItemClickCallbackListener in activity else it will give error
        // some ommited code for populating adapter with list of stored subscriptions
            adapter = new ServiceNotifRowAdapter(getActivity().getApplicationContext(),list);
        setListAdapter(adapter);

    }

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

            View rootView = inflater.inflate(R.layout.servicelist_fragment, container, false);

            addButton = (Button) rootView.findViewById(R.id.addServiceButton);
            MainActivity m = (MainActivity) getActivity();
            addButton.setOnClickListener(m);
            return rootView;
        }

      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {

          NotifService  item = (NotifService) getListAdapter().getItem(position);
          Log.d(TAG, "selected service " + item.getServiceURI());

            mCallback.replaceFragmentToServiveListNot();

      }
     }


     public class MainActivity extends FragmentActivity implements TabListener, AddServiceDialogFragmentListener
, OnClickListener,onListItemClickCallbackListener {

    private final String TAG = "MQTT main activity";

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    List<Fragment> fragments;


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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();

        fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, StatusListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ServicesListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ConfigFragment.class.getName()));

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),fragments);

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : MqttApplication.tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        // lock the screen in portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void replaceFragmentToServiveListNot() {
        fragments.removeAll(fragments);
        fragments.add(Fragment.instantiate(this, StatusListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ServiceSpecifNotListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ConfigFragment.class.getName()));

        mAdapter.notifyDataSetChanged();
    }


}