我正在处理显示餐厅订单的列表视图。
您可能会看到,每行有4个项目:
单位
文章+组
单价
总价
我需要创建节标题以按组对项目进行分组。
在这种情况下,结果列表应如下所示:
PRIMEROS
1个AMPARI-RICARD。 3.50 3.50
2OS
1菜单初始15.50 15.50
BEBIDAS
4 LIMONADA CASERA。 3.30 3.30
1个AMPARI-RICARD。 3.50 3.50
这是我的适配器代码:
public class MyAppAdapter extends BaseAdapter //has a class viewholder which holds
{
public class ViewHolder {
TextView txtUnidades,txtArticulo,txtPrecio, txtBaseImponible;
ImageView imageView;
}
public List<ListaLineasTicket> parkingList;
public Context context;
ArrayList<ListaLineasTicket> arraylist;
private MyAppAdapter(List<ListaLineasTicket> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ListaLineasTicket>();
arraylist.clear();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
MyAppAdapter.ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content_lineas_ticket, parent, false);
viewHolder = new MyAppAdapter.ViewHolder();
viewHolder.txtUnidades = (TextView) rowView.findViewById(R.id.txtUnidades);
viewHolder.txtArticulo = (TextView) rowView.findViewById(R.id.txtArticulo);
viewHolder.txtPrecio = (TextView) rowView.findViewById(R.id.txtPrecio);
viewHolder.txtBaseImponible = (TextView) rowView.findViewById(R.id.txtBaseImponible);
rowView.setTag(viewHolder);
} else {
viewHolder = (MyAppAdapter.ViewHolder) convertView.getTag();
}
// here setting up names and images
String unidades = parkingList.get(position).getUnidades();
String filename = unidades; // full file name
String[] parts = filename.split("\\."); // String array, each element is text between dots
String beforeFirstDot = parts[0];
System.out.println( "hola dos lineas unidades "+ beforeFirstDot );
viewHolder.txtUnidades.setText(beforeFirstDot );
viewHolder.txtArticulo.setText(parkingList.get(position).getDescripcionarticulo()+" "+ parkingList.get(position).getGrupo());
viewHolder.txtPrecio.setText(parkingList.get(position).getPrecioventa());
viewHolder.txtBaseImponible.setText(parkingList.get(position).getBaseimponible() );
return rowView;
}
}
我需要创建此节标题吗?
答案 0 :(得分:2)
做到这一点的一种方法是通过使用具有标题和项目位置的布局,然后在标题相同的情况下更改可见性。
您可以参考此示例。
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/section_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/primary"
android:textStyle="bold"
tools:text="This is section header" />
<TextView
android:id="@+id/list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/section_header"
android:background="?android:attr/selectableItemBackground"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="example text" />
<!-- more views can go here -->
</RelativeLayout>
适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.DataViewHolder> {
private List<Contact> mData;
@Inject
public RecyclerAdapter() {
mData = new ArrayList<>();
}
public void setData(List<Contact> data) {
mData = data;
}
public Contact getItem(int position){
return mData.get(position);
}
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new DataViewHolder(itemView);
}
@Override
public void onBindViewHolder(final DataViewHolder holder, int position) {
Contact contact = mData.get(position);
holder.headerTextView.setText(contact.getName().substring(0, 1));
holder.nameTextView.setText(contact.getName());
// if not first item check if item above has the same header
if (position > 0 && mData.get(position - 1).getName().substring(0, 1).equals(contact.getName().substring(0, 1))) {
holder.headerTextView.setVisibility(View.GONE);
} else {
holder.headerTextView.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
return mData.size();
}
public class DataViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.section_header)
TextView headerTextView;
@BindView(R.id.list_item_name)
TextView nameTextView;
public DataViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}