由于getAdapterPosition导致的问题

时间:2017-07-07 18:28:30

标签: android android-recyclerview

我的阵列包含{座椅1,座椅1,座椅1,座椅2,座椅2,座椅3,座椅4,座椅4,座椅4}等值。即图像的左侧 我需要根据座位值对它们进行分组。图像的右侧。 enter image description here

对于给定的环境,座位列表数组将为:{"1", "-1", "-1", "2", "-1", "3", "4", "-1", "-1"} 目前我正在使用recyclerview并在以下条件下检查:

if(seat_list.get(holder.getAdapterPosition()).equals("-1"))
    {
        Log.d("aaa", "if :" + seat_list.get(holder.getAdapterPosition()) );
    }
    else
    {
        Log.d("aaa", "else :" + seat_list.get(holder.getAdapterPosition()) );
        holder.ll_header.setVisibility(View.VISIBLE);
        holder.tv_seat_num.setText(seat_list.get(holder.getAdapterPosition()));
    }

上面的代码列表在滚动recyclerview时被破坏了。 我已经排序,但我想给所有同一座位的订单提供共同标题或标题

2 个答案:

答案 0 :(得分:0)

首先,我认为您的数据不准备显示。所以准备它会更好。 最好有一些类型化的数据模型,其中包含有关座位和订单的信息。 OOP,你知道吗? 为了保持简单,我在活动中准备和映射数据,但对于现实世界的项目来说,这不是一个好主意。 因此,根据您的描述,我们有一些数组,其中项目可以重复,它们在数组中的位置 - 它是orderId。所以,换句话说,我们想要一些具有键值对的分组结构,其中key - >座位№和价值 - > orderIds的集合。但是,如果有一个代表这种关系的类,并且有关于上述内容的信息,那将会好得多。

使用这样的数据,在recyclerView中显示它们不是一个大问题。

在开始时,我想在这里放置几个方法,但是在我意识到它可能会在没有完整代码列表的情况下混淆。 所以,在这里你去做所有的事情。 这是您将通过以下代码获得的结果:

enter image description here

= = = = =看一些代码的时间= = = = =

活动 - > (仅使用xml布局中的RecyclerView)

public class SeatsActivity extends AppCompatActivity {
RecyclerView recycler;
SeatsAdapter seatsAdapter;
final String[] orders =
  { "seat 1", "seat 1", "seat 1", "seat 2", "seat 2", "seat 3", "seat 4", "seat 4", "seat 4" };

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_seats);
  setTitle("Seats example");
  recycler = (RecyclerView) findViewById(R.id.recycler);
  recycler.setLayoutManager(new LinearLayoutManager(this));

  final Map<String, List<Integer>> groupedOrders = groupData(orders);
  final List<SeatItem> seats = mapToSeatsItems(groupedOrders);

  bindToRecycler(seats);
}

private void bindToRecycler(List<SeatItem> seatItems) {
  seatsAdapter = new SeatsAdapter(seatItems);
  recycler.setAdapter(seatsAdapter);
}

private Map<String, List<Integer>> groupData(final String[] items) {
final Map<String, List<Integer>> grouped = new LinkedHashMap<>();

for (int i = 0; i < items.length ; i++) {
  // I took it from your description. I image that orderId is the place of the seat in array
  final Integer orderId = i+1;
  final String headerKey = items[i];

  if (!grouped.containsKey(headerKey)){
    List<Integer> list = new ArrayList<>();
    list.add(orderId);
    grouped.put(headerKey, list);
  } else {
    List<Integer> list = grouped.get(headerKey);
    list.add(orderId);
  }
}

  return grouped;
}

private List<SeatItem> mapToSeatsItems(Map<String, List<Integer>> groupOrders) {
  final List<SeatItem> list = new ArrayList<>(groupOrders.size());
  for (String key : groupOrders.keySet()) {
    final SeatItem item = new SeatItem(key, groupOrders.get(key));
    list.add(item);
  }
  return list;
  }
}

然后是SeatItem,代表数据模型(POJO,Bean,等等)

public class SeatItem {
  public SeatItem(String header, List<Integer> orderIds) {
    this.header = header;
    this.orderIds = orderIds;
  }

  final String header;
  final List<Integer> orderIds;

  public String getHeader() {
    return header;
  }

  public List<Integer> getOrderIds() {
    return orderIds;
  }
}

SeatsAdapter - &gt;

public class SeatsAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {
final List<SeatItem> dataList;

  public SeatsAdapter(final List<SeatItem> items){
  dataList = items;
}

@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup 
parent, int viewType) {
  final View view = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seat, parent, 
false);
  return new SeatsViewHolder(view);
}

@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int 
position) {
  final SeatsViewHolder seatsViewHolder = (SeatsViewHolder) holder;
  final SeatItem item = dataList.get(holder.getAdapterPosition());

  final StringBuilder contentBuilder = new StringBuilder();
  for (int i = 0; i < item.getOrderIds().size(); i++) {
    // here is not production ready, but for just example will be ok
    contentBuilder
        .append("order Id: ")
        .append(item.getOrderIds().get(i));
    if (i + 1 != item.getOrderIds().size()){
      contentBuilder.append("\n");
    }
  }
  seatsViewHolder.tvHeader.setText(item.getHeader());
  seatsViewHolder.tvContent.setText(contentBuilder);
}

@Override public int getItemCount() { return dataList.size(); }

private static class SeatsViewHolder extends RecyclerView.ViewHolder {
  final TextView tvHeader;
  final TextView tvContent;

  SeatsViewHolder(View itemView) {
    super(itemView);
    tvHeader = (TextView) itemView.findViewById(R.id.tvHeader);
    tvContent = (TextView) itemView.findViewById(R.id.tvContent);
  }
}
}

最后一个 - 项目xml,将由recycler

显示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  >

<TextView
    android:id="@+id/tvHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="header! "
    android:textAppearance="@style/TextAppearance.AppCompat.Title"
    android:padding="8dp"
    android:background="#cccccc"

    />

<TextView
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="content ! ! ! "
    android:layout_gravity="top"
    android:gravity="top"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:padding="8dp"
    />

</LinearLayout>

答案 1 :(得分:-2)

您可以使用Collections.sort方法进行排序