导航抽屉打开后如何避免片段列表刷新?

时间:2014-12-18 14:25:16

标签: android android-fragments android-listview navigation-drawer

我的片段中有列表,MainActivity中有NavigationDrawer。从NavigationDrawer我正在调用该片段。当我滚动一点并再次打开NavigationDrawer时,列表刷新并从顶部开始。我猜主要原因是setHasOptionsMenu(true);,因为当我评论这一行时,一切都没问题,但我在ActionBar中搜索不起作用。

当我滚动一点时:enter image description here

它应该是什么样的:enter image description here

它的实际外观: enter image description here

我的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.alllists, container, false);
    View header = inflater.inflate(R.layout.list_header, null);
    ImageView image = (ImageView) header.findViewById(R.id.small_icon);
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    Bundle bundle = getArguments();
    position = bundle.getInt("position");
    location = bundle.getString("location");
    image.setImageDrawable(navMenuIcons.getDrawable(position));
    context = getActivity().getApplicationContext();
    DatabaseHandler db = new DatabaseHandler(context);
    items = db.getAllItems(location);
    tmp_items = db.getAllItems(location);
    listView = (ListView) rootView.findViewById(R.id.list);
    adapter = new CustomListAdapter(context, items);
    listView.addHeaderView(header, "", false);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
    setHasOptionsMenu(true);
    return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            if (TextUtils.isEmpty(s)) {
                adapter = new CustomListAdapter(context, tmp_items);
                listView.setAdapter(adapter);
            }
            if (tmp_s.length() > s.length()) {
                adapter.getFilter(tmp_items, true).filter(s);
            } else {
                adapter.getFilter(tmp_items, false).filter(s);
            }
            tmp_s = s;
            return false;
        }
    });
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    mSearchView.setQuery("", false);
}

我更新了我的newInstance方法:

public static final AllLists newInstance(int position, String location) {
    AllLists mainfrag = MainActivity.getMainFragment();
    if (mainfrag == null || !mainfrag.getArguments().getString("location").equals(location)) {
        AllLists all = new AllLists();
        Bundle bdl = new Bundle(2);
        bdl.putInt("position", position);
        bdl.putString("location", location);
        all.setArguments(bdl);
        MainActivity.setMainFragment(all);
        return all;
    }
    return mainfrag;
}

MainActivity:

public static AllLists mainFragment = null;

 ...

public static void setMainFragment(AllLists fragment) {
    mainFragment = fragment;
}

public static AllLists getMainFragment() {
    return mainFragment;
}

为了清楚起见,我改变了我的片段:

getSupportFragmentManager()
       .beginTransaction()
       .replace(R.id.frame_container, AllLists.newInstance(position, location))
       .commit();

2 个答案:

答案 0 :(得分:0)

问题是每次返回时,您的片段视图都会重置。

为了不松散主片段的状态,你应该做&#34; new Fragment()&#34;在newIntance方法中只有一次。您可以创建一个全局变量,例如&#39; mainFragment&#39;,存储一次创建的新Fragment(),然后应用检查,如果您的全局变量,即&#39; mainFragment&#39;,不是null,然后返回mainFragment。

这应该可以解决您的问题。但是在屏幕旋转时,仍会创建一个新实例,导致再次将您带回列表顶部。有关旋转问题,请参阅:

http://blog.sqisland.com/2014/06/navigationdrawer-creates-fragment-twice.html

答案 1 :(得分:0)

来自docs

  

您的片段可以为活动的选项提供菜单项   菜单(以及因此,操作栏)通过实施   onCreateOptionsMenu()。为了使此方法接收呼叫,   但是,你必须在onCreate()期间调用setHasOptionsMenu()   表示该片段想要将项目添加到选项菜单   (否则,片段将不会接到电话   onCreateOptionsMenu())。

所以,让我们移动setHasOptionsMenu(true);从您的onCreateView(...)来电转移到您的片段中的onCreate(...)覆盖:

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