回收者视图未调用onBindviewHolder

时间:2019-12-31 00:13:47

标签: java android android-studio

当我尝试在回收器视图中选择一个元素时,未调用OnBindViewHolder。 我使用的是水平布局,最初没有显示所有元素(显示7个元素中的4个,并且当用户向右移动3个元素时交替显示)。 通常,当用户单击某个元素时,应触发OnBindViewHolder,但对我而言这不是真的。它唯一触发的时间是初始化。由于不会触发,因此无法单击“回收者”视图内的任何元素。也许与我的布局有关?我不确定

MyAdapter

public MyAdapter(){
    this.setHasStableIds(true);
}


//Set Keys
public void setSelectionTracker(SelectionTracker<Long> selectionTracker) {
    this.mSelectionTracker = selectionTracker;
}

public static class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView dayView, numberView;
    public View view;
    ScheduleDetails scheduleDetails = new ScheduleDetails();

    public MyViewHolder(View itemView){
        super(itemView);
        view = itemView;
        dayView = itemView.findViewById(R.id.day);
        numberView = itemView.findViewById(R.id.day_number);
    }

    void bind(int position, String dayInit, String numberInit, Boolean isSelected){
        scheduleDetails.position = position;
        System.out.println("Hit2: " + scheduleDetails.position);

        dayView.setText(dayInit);
        numberView.setText(numberInit);
        view.setSelected(isSelected);

    }

    public ItemDetailsLookup.ItemDetails<Long> getItemDetails(@NonNull MotionEvent motionEvent){
        return scheduleDetails;
    }
}

static class ScheduleDetails extends ItemDetailsLookup.ItemDetails<Long>{
    int position;
    Long identifier;

    @Override
    public int getPosition() {
        return position;
    }

    @Nullable
    @Override
    public Long getSelectionKey() {
        return identifier;
    }

    @Override
    public boolean inSelectionHotspot(@NonNull MotionEvent e){
        return true;
    }
}

public MyAdapter(String[] day, String[] number){
    days = day;
    numbers = number;
}


@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recycler_layout, parent, false);

    MyViewHolder vh = new MyViewHolder(view);
    return vh;
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    String dayInit = days[position];
    String numberInit = numbers[position];
    Long positions = (long) position;

    System.out.println("Position: " + position);

    boolean isSelected = false;
    if(mSelectionTracker != null){
        if(mSelectionTracker.isSelected(positions)){
            isSelected = true;
        }
        holder.bind(position, dayInit, numberInit, isSelected);
    }
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return days.length;
}

@Override
public long getItemId(int position) {
    return position;
}

1 个答案:

答案 0 :(得分:0)

已解决

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    String dayInit = days[position];
    String numberInit = numbers[position];
    Long id = getItemId(position);

    boolean isSelected = false;
    if(mSelectionTracker != null) {
        if (mSelectionTracker.isSelected(id)) {
            isSelected = true;
        }
    }

    System.out.println("Here: " + isSelected);
    holder.bind(position, id, dayInit, numberInit, isSelected);
}

@Override
public long getItemId(int position) {
    return position;
}

我从getItemId检索位置并将其放入我的id变量(我使用了setHasStableIds)。此实施不需要Click Listener。这是用于androidx recyclerview-selection库

最后我在MyViewHolder中声明了这些内容:

  

scheduleDetails.position =位置;   scheduleDetails.identifier =键;