【问题标题】:how to add frame layout in recyclerView item?如何在 recyclerView 项目中添加框架布局?
【发布时间】:2017-03-10 21:01:46
【问题描述】:

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

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>

【问题讨论】:

    标签: android android-recyclerview fragment


    【解决方案1】:

    我认为您可以在片段中使用 onCreateView() 并膨胀您的框架布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多