我试图在SUM
(COUNT
和CardView
)内的RecyclerView
的每个FrameLayout中分别填充2个自定义工程图。如何从Fragment本身而不是CustomDrawingA.java
方法添加这些自定义图形。我尝试过
CustomDrawingB.java
onBindViewHolder
recyclerview_listitem.xml
setFrameLayoutA(android.widget.FrameLayout) in RecyclerViewListItem cannot be applied to (com.package.name.CustomDrawingA)
RecyclerViewAdapter类
setFrameLayoutB(android.widget.FrameLayout) in RecyclerViewListItem cannot be applied to (com.package.name.CustomDrawingB)
RecyclerViewListItem类
<?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>
片段类
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) */
}
}
}
当前结果
答案 0 :(得分:1)
只能通过适配器访问ViewHolder。您不能直接从活动中添加视图。不可能