【问题标题】:Replace ListFragment替换 ListFragment
【发布时间】:2013-02-13 19:37:05
【问题描述】:

我正在使用 ListFragment 在我的活动中显示来自数据库的列表。我已经包括了一个搜索功能。不幸的是,“旧” ListFragments 似乎保留在后台,并且包含查询结果的 ListFragments 显示在其顶部。如何避免显示旧的 ListFragments?

我的 FragmentActivity:

private Button buttonSearch; 
    private TextView searchString; 
    public static String search = null; 
    static IShoppinglist shoppinglistManager;
    static IAktionen aktionenManager; 
    private AktionenListListFragment listFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "ListFragmentActivity created"); 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articlelist);

        shoppinglistManager = new Shoppinglist(this);
        aktionenManager = new Aktionen(this); 

        buttonSearch = (Button) findViewById(R.id.search_Button);
        buttonSearch.setOnClickListener(searchListAktionen);

        //show all entries on start
        listFragment = new AktionenListListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();
    }   

    OnClickListener searchListAktionen = new OnClickListener() {
        public void onClick(View v) {


            try{
                searchString = (TextView) findViewById(R.id.input_search_bezeichnung); 
                search = searchString.getText().toString().trim();
                Log.d(TAG, "search Button clicked "+search); 

                if(search.trim().length()==0){
                    search=null;
                }

                //show all entries on start
                listFragment = new AktionenListListFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();

            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    };

提前致谢,

更新:

感谢您的回答。我试图实现它们,但主要问题似乎是现在 ListFragment 中的 onCreate 和 onActivityCreated 方法被调用了两次(正如我在日志消息中看到的那样)。

我的新代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "ListFragmentActivity created"); 
        super.onCreate(savedInstanceState);

        //force commit
        getSupportFragmentManager().executePendingTransactions();


        if(getSupportFragmentManager().findFragmentByTag(tag) == null) {
            setContentView(R.layout.articlelist);

            shoppinglistManager = new Shoppinglist(this);
            aktionenManager = new Aktionen(this); 

            buttonSearch = (Button) findViewById(R.id.search_Button);
            buttonSearch.setOnClickListener(searchListAktionen);
            listFragment = new AktionenListListFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment,tag).commit();
        }else{
            Log.d(TAG, "ListFragment already exists"); 
        }
    }

我尝试为我的 ListFragment 设置一个唯一标签,但此解决方案不起作用。

我猜其中一个 ListFragments 显示在后台,另一个更新。

【问题讨论】:

  • 嘿,我的回答对你有用吗?
  • 你好?不要告诉我你的那些提出问题然后不投票或选择答案的人..
  • 我已经编辑了我的问题 - 请参阅部分:更新。

标签: android fragment


【解决方案1】:

因此,首先,您需要在每次刷新列表时停止创建新的 ListFragments,并且只需在 ListFragment 中有一个公共方法,Activity 可以调用该方法以使用适当的参数重新启动加载程序。那么:

在你的 onLoadFinished() 中,

你应该用你想用列表替换它的新适配器

myAdapter = new AktionenListCustomCursorAdapter(getActivity(), myCursor);

然后调用:

this.getListView().setAdapter(myAdapter);

所以:

    public void onLoadFinished(Loader<Cursor> mAdapter, Cursor myCursor) {
                if(myCursor!=null){
                    //getting the data from the database
                    Log.d(TAG, "search String "+AktionenListFragmentActivity.search);
                    if(AktionenListFragmentActivity.search==null){
                        myCursor = AktionenListFragmentActivity.aktionenManager.fetchAllArticles();
                    }else{
                        myCursor = AktionenListFragmentActivity.aktionenManager.fetchItemsByBezeichnung(AktionenListFragmentActivity.search);
                    }
myAdapter = new AktionenListCustomCursorAdapter(getActivity(), myCursor);
                    this.getListView().setAdapter(myAdapter);
                }
            }

希望这可以解决您的问题,正如我所理解的那样。如果您有任何问题,请在下面的评论中留下,我会扩展我的答案。如果它对您有用,请接受答案。 :D

【讨论】:

  • 我的回答对了一半。但感谢您提供有用的 dymmeh。
  • 你去 :) 你可能想修复他的数据加载,因为他在 onLoadFinished 的主线程上加载所有数据。
  • 我不同意每次支持数据更改时都实例化一个新适配器。您已经有一个适配器。只需清除它,添加新数据,然后调用 notifyDataSetChanged()。频繁更换适配器并不是一个好的解决方案,因为列表每次都会跳回顶部,而不是保持用户的滚动位置。
  • 在这种情况下这可能无关紧要,因为您总是完全替换数据,但这是一个坏习惯。
  • 查看这篇文章,D Tran 几天前引起了我的注意。 vikinghammer.com/2011/06/17/…
猜你喜欢
  • 2014-07-07
  • 2017-07-23
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多