在Recycler视图中更改视图的布局

时间:2016-04-27 14:41:08

标签: android android-recyclerview android-adapter

我有两种不同的布局,我希望在Recycler View的特定情况下用于我的观看。用户决定应该出现哪种布局。如何在运行时更改回收站视图中显示的视图布局?我想我需要强制我的适配器重新启动并重新创建和重新排列视图。如何做到这一点,我不会失去我的insert动画?

更新:

public class InfinityFragmentRecyclerAdapter extends RecyclerView.Adapter implements HasDatabase
{
  protected JsonDatabase jsonDatabase = new JsonDatabase(null);
protected Context mContext;
protected int arrangeMode;

public InfinityFragmentRecyclerAdapter(Context context, int viewMode)
{
    mContext = context;
    arrangeMode = viewMode;
}

// this is the method which enables me to change the view mode
public void setNewArrangeMode(int mode)
{
    arrangeMode = mode;
    notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
    View view;
    if (arrangeMode == SupportsGridAndListArrangement.GRID_VIEW)
    {
        view = LayoutInflater.from(viewGroup.getContext()).inflate
                (R.layout.grid_layout, null);
    }
    else
    {
        view = LayoutInflater.from(viewGroup.getContext()).inflate
                (R.layout.list_layout, null);
    }
    return new ItemHolder(view, mContext, arrangeMode);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i)
{
....
    if (arrangeMode == SupportsGridAndListArrangement.LIST_VIEW)
    {
        ...

        StaggeredGridLayoutManager.LayoutParams layoutParams =
                new StaggeredGridLayoutManager.LayoutParams
                        (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setFullSpan(true);
        viewHolder.itemView.setLayoutParams(layoutParams);
    }
}

@Override
public int getItemCount()
{
    if (jsonDatabase != null)
    {
        return jsonDatabase.getCount();
    }
    else
    {
        return 0;
    }
}

@Override
public Object getDatabase()
{
    return jsonDatabase;
}


public void append(Object newJsonDatabase)
{
    int currentCount = 0;
    if (jsonDatabase != null)
    {
        currentCount = jsonDatabase.getCount();
    }
    int newCount = ((JsonDatabase) newJsonDatabase).getCount();
    jsonDatabase.appendAtEnd((JsonDatabase) newJsonDatabase);
    for (int i = currentCount; i < (currentCount + newCount); i++)
    {
        notifyItemInserted(i);
    }
}
}

0 个答案:

没有答案