更新片段不起作用

Leo*_*uza 15 android android-layout android-fragments

我正在学习Android,在尝试开发应用程序时遇到以下情况:

我有一个活动有两个Fragments:ReminderListFragmentFilterListFragment.第一个片段有一个提醒列表,第二个片段有一个过滤器列表,其中包含每个过滤器中注册的项目名称和数量.但是,当我排除某些提醒时,FilterListFragment值不会更新.当我删除其中一个过滤器时会发生同样的事情(在这种情况下,它会删除所选过滤器的所有记录),它不会更新"提醒"列表.

在此输入图像描述

FilterListFragment 码:

    @Override
        public boolean onContextItemSelected(MenuItem item) {
            if (item.getGroupId() == R.id.context_menu_category) {
                // Used to verify it it is the right context_menu //Gets the item
                // position and gets the category in that position:
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                Category category = ((CategoryFilter) lvFilters.getAdapter().getItem(info.position)).getCategory();

                // Switch between the options in the context menu(Edit and Delete)
                switch (item.getItemId()) {
                case R.id.edit:
                    // Passes the current reminder to be edited via Intent and
                    // Invokes edit method
                    DialogFragment newFragment = EditCategoryDialogFragment.newInstance(category);
                    newFragment.show(getFragmentManager(), "" + R.string.dialog_editcategory_title);
                    updateListView();
                    return true;
                case R.id.delete:
                    // Invokes delete method
                    try {
                        // Deletes from the bank;
                        Controller.instance(getActivity().getApplicationContext()).deleteReminderByCategory(category);
                        Controller.instance(getActivity().getApplicationContext()).deleteCategory(category);
                        updateListView();
                        return true;
                    } catch (DBException e) {
                        Log.e(TAG, e.getMessage());
                    }
                    updateListView();
                    return true;
                default:
                    return super.onContextItemSelected(item);
                }

            }
            return super.onContextItemSelected(item);
        }
Run Code Online (Sandbox Code Playgroud)

ReminderListFragment 码:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getGroupId() == R.id.context_menu_reminder) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            Reminder reminder = (Reminder) contextMenuAdapter.getItem(info.position);
            switch (item.getItemId()) {
            case R.id.edit:
                Intent editIntent = editIntent(reminder);
                editIntent.putExtra("id", reminder.getId());
                editIntent.putExtra("text", reminder.getText());
                editIntent.putExtra("details", reminder.getDetails());
                startActivity(editIntent);
                updateListView(null);
                return true;
            case R.id.delete:
                try {
                    Controller.instance(getActivity().getApplicationContext()).deleteReminder(reminder);
                } catch (DBException e) {
                    Log.e(TAG, e.getMessage());
                }
                updateListView(null);
                return true;
            default:
                return super.onContextItemSelected(item);
            }
        }
        return super.onContextItemSelected(item);
    }
Run Code Online (Sandbox Code Playgroud)

mat*_*tip 7

您应该通过活动与第二个片段进行通信.当您在一个片段中执行某些应该影响第二个片段的内容时,您应该在您的活动中调用一些方法,该方法将在第二个片段中调用update方法.例如活动:

public class MainActivity extends Activity(){
   private Fragment fragmentOne, fragmentTwo;

   public void updateFragmentTwo(){
       fragmentTwo.updateListView();
   }
}
Run Code Online (Sandbox Code Playgroud)

分段:

public class FirstFragment extends Fragment{


   public void updateFragmentTwo(){
       ((MyActivity)getActivity()).updateFragmentTwo();
   }
}
Run Code Online (Sandbox Code Playgroud)


Leo*_*uza -2

我能够通过添加以下方法来解决它:

上课时FilterListFragment

public void reloadReminderListFragment() {
        Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.listReminders);
        if (currentFragment instanceof ReminderListFragment) {
            FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
            fragTransaction.detach(currentFragment);
            fragTransaction.attach(currentFragment);
            fragTransaction.commit();
        }
    }
Run Code Online (Sandbox Code Playgroud)

上课时ReminderListFragment

public void reloadFilterListFragment() {
        Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.listCategories);
        if (currentFragment instanceof FilterListFragment) {
            FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
            fragTransaction.detach(currentFragment);
            fragTransaction.attach(currentFragment);
            fragTransaction.commit();
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 分离并附加相同的片段来更新它,对我来说看起来并不是一个严肃的解决方案。我产生了很多开销。这有点像重新启动汽车来关闭收音机。 (3认同)