首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在在RecyclerView中,CardView内的FrameLayout中扩充自定义视图?

如何在在RecyclerView中,CardView内的FrameLayout中扩充自定义视图?
EN

Stack Overflow用户
提问于 2018-07-31 04:48:33
回答 1查看 0关注 0票数 0

我试图在每个FrameLayout中,为CardView内的 RecyclerViewCustomDrawingA.javaCustomDrawingB.java)分别扩充2个自定义绘图。有谁知道如何做到这一点?itemA.setFrameLayoutA();itemA.setFrameLayoutB();返回错误。

recyclerview_listitem.xml

代码语言:txt
复制
<?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>

ReccyclerViewAdapter类

代码语言:txt
复制
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.?;
//            VHitem.flB.?;
        }
    }

    @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);
        }
    }
}

ReccyclerViewListItem类

代码语言:txt
复制
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;
    }
}

Fragment类

代码语言:txt
复制
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();
            itemA.setFrameLayoutB();
            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);
}

当前结果

EN

回答 1

Stack Overflow用户

发布于 2018-07-31 14:42:56

在你发布的代码中,错误是setFrameLayoutA()setFrameLayoutB()调用Expect参数:

代码语言:javascript
复制
public class RecyclerViewListItem {
    ...
    public void setFrameLayoutA(FrameLayout frameLayoutA) {
        this.frameLayoutA = frameLayoutA;
    }

    public void setFrameLayoutB(FrameLayout frameLayoutB) {
        this.frameLayoutB = frameLayoutB;
    }
}

有很多方法可以“解决”这个问题,但是我在这里提出的任何建议都可能是“错误的”,因为你的项目到底想要做什么还不清楚。

尽管如此,以下仍需改变:

代码语言:javascript
复制
public List<RecyclerViewListItem> getListItems()
{
    ...
    itemA.setFrameLayoutA();
    itemA.setFrameLayoutB();
}

你必须传递FrameLayout给他们中的每一个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100001786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档