Recycleview包含子片段

时间:2015-08-07 19:19:07

标签: android android-fragments

大家好我想要一个包含子片段的循环视图示例 和布局管理器是GridLayoutManager
我做了一个,但它只是在第一个单元格中添加而忽略了其他enter image description here

public class SimpleFragment扩展了Fragment {

private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);

    mLayoutManager = new GridLayoutManager(getActivity(), 4);
    mRecyclerView.setLayoutManager(mLayoutManager);


    List<Card> cards = new ArrayList<>();
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));

    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));


    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));

    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));
    cards.add(new Card(Size.LARGE, new FragmentHalf()));

    cards.add(new Card(Size.SMALL, new FragmentHalf()));
    cards.add(new Card(Size.SMALL, new FragmentHalf()));

    cards.add(new Card(Size.SMALL, new FragmentHalf()));
    cards.add(new Card(Size.SMALL, new FragmentHalf()));

    mAdapter = new MyAdapter(cards, mRecyclerView);
    mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_sample, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    mAdapter.addItem(5, new Card(Size.LARGE, new FragmentHalf()));

    return super.onOptionsItemSelected(item);
}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Card> mDataset;
    private RecyclerView recyclerView;
    private HashMap<Fragment, Integer> hashMapID = new HashMap<>();

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public FrameLayout view;
        private int id;

        public ViewHolder(FrameLayout v) {
            super(v);
            this.view = v;
            id = v.getId();
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(final List<Card> myDataset, RecyclerView recyclerView) {
        mDataset = myDataset;
        this.recyclerView = recyclerView;
        GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return getItem(position).size == Size.LARGE ? 2 : 1;
            }
        });


    }

    public Card getItem(int pos) {
        return mDataset.get(pos);
    }

    public void addItem(int position, Card card) {
        if (position == -1)
            mDataset.add(card);
        else
            mDataset.add(position, card);
        notifyItemInserted(position);
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        FrameLayout v = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.my_text_view, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element

        Card card = mDataset.get(position);

        getChildFragmentManager().beginTransaction().replace(holder.view.getId(), card.fragment).commit();

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

enum Size {
    LARGE,
    SMALL
}

class Card {
    public Size size;
    public Fragment fragment;
    public int id;

    public Card(Size size, Fragment fragment) {
        this.size = size;
        this.fragment = fragment;
        id = View.generateViewId();
    }
}

}

1 个答案:

答案 0 :(得分:0)

我解决了 mRecyclerView.setItemViewCacheSize(cards.size());

和方法  @覆盖         public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,                                                        int viewType){

        FrameLayout v = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.my_text_view, parent, false);
        v.setId(View.generateViewId());<-----added
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }