在 StaggeredGridLayout 中滚动和重新调整项目

Scrolling and readjusting items in StaggeredGridLayout

在我的应用程序中,我想在主要片段中使用 StaggeredGridLKayout。但是,当我添加新项目并通知适配器新项目时:

@Override
  public void onSaveSuccess() {
    getLoaderManager().restartLoader(0, null, this);
    mFragmentCardAdapter.notifyDataSetChanged();
  }

当项目太多无法显示在屏幕上时,就会出现问题。屏幕变空,项目放在它下面,我必须向下滚动到它们。此外,他们会不断重新调整并四处移动,直到达到屏幕顶部良好的程度。但是我希望它停止将 space 留在顶部,而只在顶部添加最近添加的内容。

以下是我使用的一些方法:

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTitle(getResources().getString(R.string.title));
    getLoaderManager().initLoader(0, null, this);
    mList = new ArrayList<Record>();
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment, container, false);
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);
    mRecyclerView.setHasFixedSize(false);
    mStaggered = new StaggeredGridLayoutManager(getResources()
        .getInteger(R.integer.staggered_columns), StaggeredGridLayoutManager.VERTICAL);
  }

@Override
  public void onLoadFinished(Loader<List<Record>> loader,
      List<Record> data) {
    mList = data;
    mFragmentCardAdapter =
        new FragmentCardAdapter(getContext(), mList);
    mFragmentCardAdapter.setOnRecordClickedListener(
        new FragmentCardAdapter.OnRecordClickedListener() {
          @Override
          public void onRecordClicked(long id) {
            getFragmentManager().beginTransaction()
                .replace(R.id.main_activity_content_frame, RecordFragment.newInstance(id))
                .addToBackStack("").commit();
          }
        });
    mStaggered.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
    mRecyclerView.setLayoutManager(mStaggered);
    mRecyclerView.setAdapter(mFragmentCardAdapter);
  }

和适配器:

public class FragmentCardAdapter extends RecyclerView.Adapter<FragmentCardHolder>{



private List<Record> mList;
  private Context mContext;
  private OnRecordClickedListener mRecordClickedListener;

  public FragmentCardAdapter(Context context, List<Record> list){
    mList = list;
    mContext = context;
  }

  public FragmentCardAdapter(Context context){
    mList = new ArrayList<>();
    mContext = context;
  }

  @Override
  public FragmentCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_record, null);
    return new FragmentCardHolder(layout);
  }

  @Override
  public void onBindViewHolder(FragmentCardHolder holder, final int position) {
    String text = mList.get(position).getText().replace(' ', '\n');
    holder.mText.setText(text);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(mRecordClickedListener != null){
          mRecordClickedListener.onRecordClicked(mList.get(position).getId());
        }
      }
    });
  }

  @Override
  public int getItemCount() {
    return mList.size();
  }

  public void setOnRecordClickedListener(OnRecordClickedListener onRecordClickedListener){
    mRecordClickedListener = onRecordClickedListener;
  }

  public interface OnRecordClickedListener{
    void onRecordClicked(long id);
  }
}

原来我双启动了loader