如何在recyclerView项目中添加框架布局?

时间:2017-03-10 21:01:47

标签: android android-recyclerview fragment

我想为每个元素创建一个片段。我把它转回到RecyclerView中列表的大小,我在RecyclerView布局中定义了一个框架布局。如何将片段绑定到每个元素的framelayout? 样本适配器=

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

    private Context context;
    private HashMap<Integer, Order> tester;
    private  TotalFragment fragment;
    private FragmentManager fm;

    public Adapter(Context context , HashMap<Integer, Order> tester, FragmentManager fm) {
        this.context = context;
        this.tester = tester;
        this.fm = fm;
    }

    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(context).inflate(R.layout.layout_list_view_row_items, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Adapter.ViewHolder holder, int position) {
        fragment = new TotalFragment();
        int id =holder.totalFragment.generateViewId();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(id,fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public int getItemCount() {
        return tester.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        FrameLayout totalFragment;
        ViewHolder(View itemView) {
            super(itemView);
            totalFragment = (FrameLayout) itemView.findViewById(R.id.deneme);
        }
    }
}

Adapter Layout = 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/deneme"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为您可以在片段中使用onCreateView()并为您的framelayout充气。