在我的应用中,我想在主片段中使用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);
}
}
答案 0 :(得分:0)
Tirns out我启动了装载机。