我想在特定条件下根据可扩展列表项的线性布局来增加布局。以下是我尝试膨胀的代码(用getChildView()编写)。场景是,如果用户选择了带有子项目明细的项目,而该项目尚未在购物车中,则该项目下方应添加一行,以显示其子项目明细。其对addview()的给出错误。
if (menuItem.hasSubItems() && !cart.contains(menuItem))
{
View inflated = null;
if (inflated == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//to get the MainLayout
inflated = inflater.inflate(R.layout.sub_items_detail_row, null);
holder.subitemContainer.addView(inflated);
}
}
在这里,我将视图绑定到视图持有人中
public ItemViewHolder(View view) {
this.rootView = view;
tvTitle = (TextView) view.findViewById(R.id.tvtitle);
tvDesc = (TextView) view.findViewById(R.id.tvdesc);
tvPrice = (TextView) view.findViewById(R.id.tvprice1);
tvCounter = (TextView) view.findViewById(R.id.itemCouter);
moreInfo = (TextView) view.findViewById(R.id.moreInfo);
roomService_menu_img = (ImageView) view.findViewById(R.id.roomService_menu_img);
addView = view.findViewById(R.id.imgadd);
removeView = view.findViewById(R.id.imgDelete);
subitemContainer = view.findViewById(R.id.subitemContainer);
但我在日志中遇到此错误
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.kaimeramedia.mobilexp.modules.locationservices.adapters.RoomServiceCartAdapter.getChildView(RoomServiceCartAdapter.java:210)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
at android.widget.AbsListView.obtainView(AbsListView.java:2396)
at android.widget.ListView.makeAndAddView(ListView.java:1889)
at android.widget.ListView.fillDown(ListView.java:713)
at android.widget.ListView.fillSpecific(ListView.java:1376)
at android.widget.ListView.layoutChildren(ListView.java:1675)
at android.widget.AbsListView.onLayout(AbsListView.java:2200)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2101)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5873)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
这是getchildview()代码
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
View view = convertView;
final ItemViewHolder holder;
final RoomServiceMenuItem menuItem = (RoomServiceMenuItem) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rowroomservice_new, null);
holder = new ItemViewHolder(view);
view.setTag(holder);
} else {
holder = (ItemViewHolder) view.getTag();
}
String strName = menuItem.getName();
if (strName.contains("\\n")) {
String[] parts = Pattern.compile("\\n", Pattern.LITERAL).split(strName);
strName = parts[0] + "\n" + parts[1];
}
holder.tvTitle.setText(strName);
holder.tvDesc.setText(menuItem.getDescription());
holder.tvPrice.setText("$ " + Utils.formatMoney(menuItem.getPrice()));
CartItem cartItem = cart.findByMenuItem(menuItem);
int quantity = cartItem != null ? cartItem.getQuantity() : 0;
holder.tvCounter.setVisibility(quantity <= 0 ? View.GONE : View.VISIBLE);
holder.tvCounter.setText(String.valueOf(quantity));
holder.addView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Check if this item already exist in cart if it has sub-items
boolean cartItemAlreadyAdded = cart.contains(menuItem);
if (menuItem.hasSubItems() && !cartItemAlreadyAdded) {
// use need to select its sub-item first
cartListener.onClickShowSubItems(menuItem);
return;
}
// User has this item in his cart so just increase the quantity with same sub-items
cart.add(new CartItem(menuItem));
int q = cart.findByMenuItem(menuItem).getQuantity();
onCartChange(holder, menuItem, q);
cartListener.onItemAdded(menuItem, q);
}
});
if (menuItem.hasSubItems() && !cart.contains(menuItem))
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//to get the MainLayout
View inflated = inflater.inflate(R.layout.sub_items_detail_row, null);
holder.subitemContainer.addView(inflated);
}
holder.removeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cart.contains(menuItem.getId())) {
cart.remove(new CartItem(menuItem));
CartItem cartItem = cart.findByMenuItem(menuItem);
int q = cartItem != null ? cartItem.getQuantity() : 0;
onCartChange(holder, menuItem, q);
if (q <= 0) menuItem.clearSubItemsSelection();
cartListener.onItemRemoved(menuItem, q);
}
}
});
holder.moreInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartListener.onClickMoreInfo(menuItem);
}
});
if (menu != null) {
Picasso.with(context).load(menu.getImage()).placeholder(R.drawable.loading).into(holder.roomService_menu_img);
}
if (menuItem.hasSubItems()) {
holder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cartListener.onClickShowSubItems(menuItem);
}
});
}
return view;
}
这是我想在布局中使用的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/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal"
card_view:cardCornerRadius="@dimen/_2sdp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/cartCounter"
android:layout_width="90dp"
android:layout_height="90dp">
<com.kaimeramedia.mobilexp.components.RoundedImageView
android:id="@+id/roomService_menu_img"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/itemCouter"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/roomservice_circle"
android:gravity="center"
android:text="0"
android:textColor="@color/white"
android:textStyle="bold"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:id="@+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/cartCounter"
android:layout_toLeftOf="@+id/rel"
android:layout_toRightOf="@+id/cartCounter"
android:layout_toStartOf="@+id/rel"
android:orientation="vertical">
<TextView
android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Offer Title"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:maxLines="2"
android:singleLine="false"
android:text="desc"
android:textSize="12sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/moreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/border_more_info"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="2dp"
android:text="More"
android:textColor="@color/orange"
android:textStyle="bold" />
<TextView
android:id="@+id/tvprice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:maxLines="1"
android:text="$ 0.0"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/rel"
android:layout_width="70dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:background="#e3e0dd"
android:padding="2dp">
<RelativeLayout
android:id="@+id/imgadd"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="1dp"
android:layout_marginTop="0dp"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/plusicon"
android:text="+"
android:textColor="@color/white"
android:textSize="35sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/imgDelete"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@+id/imgadd"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:background="@color/orange">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/plusicon"
android:text="-"
android:textColor="@color/white"
android:textSize="35sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/subitemContainer"
android:layout_below="@+id/rel"></LinearLayout>
</RelativeLayout>