2015-10-27 32 views
1

在我的應用程序中,我想在主片段中使用StaggeredGridLKayout。然而,當我添加一個新的項目,並通知新項目的適配器:當有太多的項目,以適應屏幕上出現在StaggeredGridLayout中滾動和重新調整項目

@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

Tirns了我雙啓動加載程序。