切换片段

时间:2016-08-28 15:48:36

标签: android android-fragments android-arrayadapter

我在这个论坛上尝试过与我的问题相关的不同答案,但似乎没有一个对我有用。

ArrayAdapter类

public class UserAdapter extends ArrayAdapter<User> {
public UserAdapter(Context context, ArrayList<User> users) {
    super(context, 0, users);
}

private static class ViewHolder {
    TextView water;
    TextView amount;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    User user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        // If there's no view to re-use, inflate a brand new view for row
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.card, parent, false);
        viewHolder.water = (TextView) convertView.findViewById(R.id.WaterConsumed);
        viewHolder.amount = (TextView) convertView.findViewById(R.id.WaterAmount);
        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data into the template view using the data object
    viewHolder.water.setText(user.waterconsum);
    viewHolder.amount.setText(user.amount);
    // Return the completed view to render on screen
    return convertView;
}

用户类

public class User implements Serializable{
 String waterconsum;
 String amount;

public User(String waterconsum, String amount) {
    this.waterconsum = waterconsum;
    this.amount = amount;
}

显示数据的片段类之一

public class WaterFragment extends Fragment {

ArrayList<User> arrayOfUsers = new ArrayList<User>();
UserAdapter adapter;

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setRetainInstance(true);
    if (savedInstanceState != null) {
        arrayOfUsers = (ArrayList<User>) savedInstanceState.getSerializable("List");
    }
    return view = inflater.inflate(R.layout.water, container, false);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    Button button = (Button) view.findViewById(R.id.additem);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInputDialog();
        }
    });

    adapter = new UserAdapter(getActivity(), arrayOfUsers);
    ListView listView = (ListView) view.findViewById(R.id.listofitems);
    if (listView.getAdapter() != null) {
        listView.onRestoreInstanceState(savedInstanceState);
    } else {
        listView.setAdapter(adapter);
    }


}

protected void showInputDialog() {
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    final View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setView(promptView);
    final EditText editText = (EditText) promptView.findViewById(R.id.edittext);

    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String text = editText.getText().toString();
                    addItems(text);
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

public void addItems(String text) {
    arrayOfUsers.add(new User("Water Consumed Today", text));
}

@Override
public void onSaveInstanceState(Bundle state) {
    state.putSerializable("List", arrayOfUsers);
    super.onSaveInstanceState(state);
}

每当我在不同的片段之间切换时,阵列数据总是被重置。所以我不确定如何解决这个问题,我尝试了各种解决方案,包括保存片段状态和数组数据,但到目前为止似乎都没有。

编辑:片段处理程序类

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

    bottomBar = BottomBar.attach(this, savedInstanceState);
    bottomBar.setItemsFromMenu(R.menu.three_buttons_menu, new OnMenuTabSelectedListener() {
        @Override
        public void onMenuItemSelected(@IdRes int menuItemId) {
            if (menuItemId == R.id.Calendar) {
                CalendarFragment f = new CalendarFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
            } else if (menuItemId == R.id.Water) {
                WaterFragment wf = new WaterFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,wf).commit();
            } else if (menuItemId == R.id.Advice) {
                AdviceFragment af = new AdviceFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.frame, af).commit();
            } else if (menuItemId == R.id.Diary) {
                AdviceFragment af = new AdviceFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.frame, af).commit();
            } else if (menuItemId == R.id.Help) {
                AdviceFragment af = new AdviceFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.frame, af).commit();
            }
            getFragmentManager().executePendingTransactions();

        }
    });

1 个答案:

答案 0 :(得分:0)

在片段的onCreate中只创建一次适配器。

并在你的onViewCreated中将此适配器设置为listview。

private UserAdapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(adapter==null)
            adapter = new UserAdapter(getActivity(), arrayOfUsers);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle    savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView listView = (ListView) view.findViewById(R.id.listofitems);
        listView.setAdapter(adapter);
    }

每当数据发生变化时,在适配器中添加新项并调用notifyDataSetChanged。

UPDATE :从Fragment Handler类开始,每次更换片段时都要创建新实例

CalendarFragment f = new CalendarFragment();

我建议你这样做..

private CalendarFragment calenderFragment;
    private WaterFragment waterFragment;
    private AdviceFragment adviceFragment;


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

        calenderFragment = new CalendarFragment();
        waterFragment = new WaterFragment();
        adviceFragment = new AdviceFragment();

        bottomBar = BottomBar.attach(this, savedInstanceState);
        bottomBar.setItemsFromMenu(R.menu.three_buttons_menu, new OnMenuTabSelectedListener() {
            @Override
            public void onMenuItemSelected(@IdRes int menuItemId) {
                if (menuItemId == R.id.Calendar) {
                    if(calenderFragment==null)
                        calenderFragment = new CalendarFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, calenderFragment)
                            .commit();
                } else if (menuItemId == R.id.Water) {
                    if(waterFragment==null)
                        waterFragment = new WaterFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, waterFragment)
                            .commit();
                } else if (menuItemId == R.id.Advice) {
                    if(adviceFragment==null)
                        adviceFragment = new AdviceFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.frame, adviceFragment )
                            .commit();
                } else if (menuItemId == R.id.Diary) {
                    if(adviceFragment==null)
                        adviceFragment = new AdviceFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.frame, adviceFragment )
                            .commit();
                } else if (menuItemId == R.id.Help) {
                    if(adviceFragment==null)
                        adviceFragment = new AdviceFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.frame, adviceFragment )
                            .commit();
                }
                getFragmentManager().executePendingTransactions();

            }
        });