1

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

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

当屏幕上放不下太多项目时,就会出现问题。屏幕变空,项目被放置在其下方,我必须向下滚动到它们。此外,它们会不断重新调整并四处移动,直到达到屏幕顶部正常为止。但是我希望它停止在顶部留下空间,而只是在顶部添加最新添加的内容。

这是我使用的一些方法:

@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);
  }
}
4

1 回答 1

0

Tirns 我双启动装载机。

于 2015-11-03T20:41:20.810 回答