代码之家  ›  专栏  ›  技术社区  ›  wbk727

自定义视图未添加到用于RecyclerView的FrameLayout

  •  -3
  • wbk727  · 技术社区  · 6 年前

    我试图在每个框架布局中为 CardView 在一个 RecyclerView ( CustomDrawingA.java CustomDrawingB.java )分别是。如何从片段本身而不是 onBindViewHolder 方法。我试过了

    setFrameLayoutA(android.widget.FrameLayout) in RecyclerViewListItem cannot be applied to (com.package.name.CustomDrawingA)

    setFrameLayoutB(android.widget.FrameLayout) in RecyclerViewListItem cannot be applied to (com.package.name.CustomDrawingB)

    recyclerview_listitem.xml回收服务

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview_listitem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:contentPadding="16dp">
    
        <LinearLayout
            android:id="@+id/cardview_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:animateLayoutChanges="true">
    
    
        <LinearLayout
            android:id="@+id/cardview_information_titlerow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="100">
    
            <TextView
                android:id="@+id/tv_A"
                android:layout_weight="90"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                style="@android:style/TextAppearance.Medium" />
    
            <TextView
                android:id="@+id/tv_expandcollapsearrow"
                android:importantForAccessibility="no"
                android:clickable="true"
                android:focusable="true"
                android:layout_weight="10"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                style="@android:style/TextAppearance.Medium" />
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/tv_B"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                style="@android:style/TextAppearance.Medium" />
    
            <FrameLayout
                android:id="@+id/framelayout_A"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_below="@+id/tv_B" />
            <FrameLayout
                android:id="@+id/framelayout_B"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_below="@+id/framelayout_A" />
    
            <TextView
                android:id="@+id/tv_C"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:textColor="@android:color/white"
                android:background="@drawable/bg_rectangle_grey"
                android:padding="5dp"
                style="@android:style/TextAppearance.Large"
                android:layout_below="@+id/framelayout_B" />
    
            <TextView
                android:id="@+id/tv_D"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:padding="5dp"
                android:textColor="@android:color/white"
                style="@android:style/TextAppearance.Large"
                android:layout_below="@+id/framelayout_B"
                android:layout_toEndOf="@+id/tv_C" />
    
            <TextView
                android:id="@+id/tv_E"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                style="@android:style/TextAppearance.Medium"
                android:layout_below="@+id/tv_D" />
        </RelativeLayout>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    RecyclerViewAdapter类

    public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
    
        //this context we will use to inflate the layout
        private Context mContext;
    
        RecyclerViewHeader header;
        List<RecyclerViewListItem> listItems;
    
    
        public MyRecyclerAdapter(Context context, RecyclerViewHeader header, List<RecyclerViewListItem> listItems)
        {
            this.mContext = context;
            this.header = header;
            this.listItems = listItems;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == TYPE_HEADER)
            {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_headeritem, parent, false);
                return  new VHHeader(v);
            }
            else if(viewType == TYPE_ITEM)
            {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_listitem, parent, false);
                return new VHItem(v);
            }
            throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
        }
    
        private RecyclerViewListItem getItem(int position)
        {
            return listItems.get(position);
        }
    
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {       
            if (holder instanceof VHHeader)
            {
                VHHeader VHheader = (VHHeader)holder;
                VHheader.txtTitle.setText(header.getHeading());
                VHheader.txtSubtitle.setText(header.getSubheading());
            }
            else if (holder instanceof VHItem)
            {
                RecyclerViewListItem currentItem = getItem(position-1);
                VHItem VHitem = (VHItem)holder;
    
                VHitem.txtExpandCollapseArrow.setText(R.string.fa_icon_chevron_up);
                VHitem.txtExpandCollapseArrow.setTypeface(iconFont);
    
                VHitem.txtA.setText(currentItem.getName());
                VHitem.txtB.setText(currentItem.getDescription());
                VHitem.txtC.setText(currentItem.getNewstatus());
                VHitem.txtD.setText(currentItem.getGlutenstatus());
                VHitem.txtE.setText(currentItem.getSuitability());
    
                VHitem.flA.addView(new CustomDrawingA(mContext));
                VHitem.flB.addView(new CustomDrawingB(mContext));
    
            }
        }
    
        @Override
        public int getItemViewType(int position) {
            if(isPositionHeader(position))
                return TYPE_HEADER;
            return TYPE_ITEM;
        }
    
        private boolean isPositionHeader(int position)
        {
            return position == 0;
        }
    
        //increasing getItemcount to 1. This will be the row of header.
        @Override
        public int getItemCount() {
            return listItems.size()+1;
        }
    
        class VHHeader extends RecyclerView.ViewHolder{
            TextView txtTitle, txtSubtitle;
            public VHHeader(View itemView) {
                super(itemView);
                this.txtTitle = itemView.findViewById(R.id.recyclerview_header__heading);
                this.txtSubtitle = itemView.findViewById(R.id.recyclerview_header__subheading);
            }
        }
    
        class VHItem extends RecyclerView.ViewHolder{
            TextView txtExpandCollapseArrow, txtA, txtB, txtC, txtD, txtE;
            FrameLayout flA, flB;
    
            public VHItem(View itemView) {
                super(itemView);
    
                this.txtExpandCollapseArrow = itemView.findViewById(R.id.tv_expandcollapsearrow);
    
                this.txtA = itemView.findViewById(R.id.tv_A);
                this.txtB = itemView.findViewById(R.id.tv_B);
                this.txtC = itemView.findViewById(R.id.tv_C);
                this.txtD = itemView.findViewById(R.id.tv_D);
                this.txtE = itemView.findViewById(R.id.tv_E);
    
                this.flA = itemView.findViewById(R.id.framelayout_A);
                this.flB = itemView.findViewById(R.id.framelayout_B);
    
                /** WORKING CODE BUT NEED AN 'itemA.[XYZ]' VERSION OF THIS TO SET IN FRAGMENT (start) */
                if(position == 1){
                    VHitem.flA.addView(new CustomDrawingA(mContext));
                    VHitem.flB.addView(new CustomDrawingB(mContext));
                }else if(position == 2){    
                    VHitem.flA.addView(new CustomDrawingC(mContext));
                    VHitem.flB.addView(new CustomDrawingD(mContext));
                }else if(position == 3){    
                    VHitem.flA.addView(new CustomDrawingE(mContext));
                    VHitem.flB.addView(new CustomDrawingF(mContext));
                }
                /** WORKING CODE BUT NEED AN 'itemA.[XYZ]' VERSION OF THIS TO SET IN FRAGMENT (end) */
    
            }
        }
    } 
    

    RecyclerServiceWListitem类

    public class RecyclerViewListItem {
    
        private String name;
        private String description;
        private FrameLayout frameLayoutA;
        private FrameLayout frameLayoutB;
        private String newstatus;
        private String glutenstatus;
        private String suitability;
    
        public RecyclerViewListItem(){}
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public FrameLayout getFrameLayoutA() {
            return frameLayoutA;
        }
    
        public void setFrameLayoutA(FrameLayout frameLayoutA) {
            this.frameLayoutA = frameLayoutA;
        }
    
        public FrameLayout getFrameLayoutB() {
            return frameLayoutB;
        }
    
        public void setFrameLayoutB(FrameLayout frameLayoutB) {
            this.frameLayoutB = frameLayoutB;
        }
    
        public String getNewstatus() {
            return newstatus;
        }
    
        public void setNewstatus(String newstatus) {
            this.newstatus = newstatus;
        }
    
        public String getGlutenstatus() {
            return glutenstatus;
        }
    
        public void setGlutenstatus(String glutenstatus) {
            this.glutenstatus = glutenstatus;
        }
    
        public String getSuitability() {
            return suitability;
        }
    
        public void setSuitability(String suitability) {
            this.suitability = suitability;
        }
    }
    

    碎片类

    public class MyFragment extends android.support.v4.app.Fragment {
    
        private FrameLayout frameLayoutA;
        private FrameLayout frameLayoutB;
    
        public MyFragment() {}
    
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            return inflater.inflate(R.layout.fragment_rv, container, false);
        }
    
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            View v = getView();
            assert v != null;
    
            recyclerView = v.findViewById(R.id.my_recyclerview);
            linearLayoutManager = new LinearLayoutManager(getActivity());
    
            MyRecyclerAdapter adapter = new MyRecyclerAdapter(getContext(), getHeader(), getListItems());
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
    
    
            super.onActivityCreated(savedInstanceState);
        }
    
        RecyclerView recyclerView;
        LinearLayoutManager linearLayoutManager;
    
        public RecyclerViewHeader getHeader()
        {
            RecyclerViewHeader header = new RecyclerViewHeader();
            header.setHeading("Desserts");
            header.setSubheading("All made fresh daily");
            return header;
        }
    
        public List<RecyclerViewListItem> getListItems()
        {
            List<RecyclerViewListItem> listItems = new ArrayList<>();
    
                RecyclerViewListItem itemA = new RecyclerViewListItem();
                itemA.setName("Crème Brûlée");
                itemA.setDescription("Caramalised vanilla crème with an almond tuile");
    
                itemA.setFrameLayoutA(new CustomDrawingA(getContext()));
                itemA.setFrameLayoutB(new CustomDrawingB(getContext()));
                itemA.setNewstatus(" New ");
                itemA.setGlutenstatus("Gluten free");
                itemA.setSuitability("Suitable for vegetarians" + "\n" + "Suitable for coeliacs" + "\n" + "Halal friendly" + "\n" + "Kosher friendly");
                listItems.add(itemA);
    
                RecyclerViewListItem itemB = new RecyclerViewListItem();
                itemB.setName("Bûche de Noël");
                itemB.setNewstatus(" New ");
                itemB.setGlutenstatus("Gluten free");
                itemB.setSuitability("Suitable for vegetarians" + "\n" + "Suitable for coeliacs");
                listItems.add(itemB);
    
                RecyclerViewListItem itemC = new RecyclerViewListItem();
                itemC.setName("Croquembouche");
                itemC.setNewstatus(" New ");
                itemC.setGlutenstatus("Gluten free");
                itemC.setSuitability("Suitable for vegetarians" + "\n" + "Suitable for coeliacs");
                listItems.add(itemC);
    }
    

    当前结果

    enter image description here

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Viswanath Kumar Sandu    6 年前

    ViewHolder只能通过适配器访问。不能直接从活动添加视图。不可能的