gpt4 book ai didi

android - RecyclerView、GridLayoutManager 和动态 Spans 计数

转载 作者:行者123 更新时间:2023-11-29 01:35:15 27 4
gpt4 key购买 nike

我在 recyclerview 中遇到了一个奇怪的错误。我动态地设置和调整跨度以适应从 AsyncTask 下载的信息。

有时显示不正确。它似乎显示在屏幕上。!

enter image description here

enter image description here

然后在一次或多次重新加载后它会正确显示。它似乎从屏幕上消失了,但我不明白为什么。

这是我的适配器:

   public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ViewHolder> {

private List<Schedule> mDataset;
/**
* First parameter is the position in the dataset
* The value returned is the number of schedules to display
*/
private TreeMap<Integer, Integer> schedulesToDisplay;
private int maxSpans;

private static final String TAG="LINE_ADATER";

// Provide a suitable constructor (depends on the kind of dataset)
public ScheduleAdapter() {
mDataset = new ArrayList<Schedule>();
schedulesToDisplay = new TreeMap<Integer, Integer>();
}

// Create new views (invoked by the layout manager)
@Override
public ScheduleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_schedule, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder((LinearLayout) v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Log.d(""+position, "BINDVIEW");
holder.setItem(mDataset.get(position));
Schedule s = mDataset.get(position);
holder.time.setText(s.toString());
}

public void add(Schedule item) {
Integer nbHour = schedulesToDisplay.get(item.hour);
int position = mDataset.size();
if(nbHour == null){
schedulesToDisplay.put(item.hour, 1);
Log.d(item.toString()+" : 1 span ("+position+")", TAG);
}
else{
nbHour++;
schedulesToDisplay.put(item.hour, nbHour);
Log.d(item.toString()+ " : " + nbHour +" spans ("+position+")", TAG);
if(nbHour > maxSpans){
maxSpans = nbHour;
}
}
mDataset.add(position, item);
notifyItemInserted(position);
}

public List<Schedule> getDataSet(){
return mDataset;
}

public int getSchedulesToDisplay(int position, GridLayoutManager layoutManager){
Integer nbHour = schedulesToDisplay.get(mDataset.get(position).hour);
if(nbHour==null){
return 0;
}
int nbHourInt = nbHour.intValue();
if( maxSpans%nbHourInt != 0){ // Si la nouvelle ligne n'est pas un multiple de la précédente
maxSpans = (maxSpans*nbHourInt)/ MathsUtils.pgcd(maxSpans, nbHourInt);
Log.d("New Max Span "+maxSpans, TAG);
}
layoutManager.setSpanCount(maxSpans);
layoutManager.requestLayout();
layoutManager.getSpanSizeLookup().invalidateSpanIndexCache();

Log.d("Span count "+layoutManager.getSpanCount(), TAG);
Log.d("Heure "+mDataset.get(position).hour+" ("+nbHourInt+") : "+(maxSpans/nbHourInt)+" spans", TAG+" SPAN");
return (maxSpans/nbHourInt);
}

public void removeAll() {
mDataset.clear();
schedulesToDisplay.clear();
maxSpans = 1;
notifyDataSetChanged();
}

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

public class ViewHolder extends RecyclerView.ViewHolder{
// each data item is just a string in this case
TextView time;
Schedule scheduleItem;

public ViewHolder(LinearLayout lyt_main) {
super(lyt_main);
time = (TextView) lyt_main.findViewById(R.id.time);
}

public void setItem(Schedule item) {
scheduleItem = item;
}
}
}

每次下载新数据之前,都会从 Activity 中调用 removeAll() 方法。

最佳答案

其实我解决了这个问题。我注意到 setSpanSizeLookup 方法仅在 notifyItemInserted 之后被调用。我必须在后面加上调整大小的方法。

public void add(Schedule item) {
Integer nbHour = schedulesToDisplay.get(item.hour);
int position = mDataset.size();
if(nbHour == null){
schedulesToDisplay.put(item.hour, 1);
Log.d(item.toString()+" : 1 span ("+position+")", TAG);
}
else{
nbHour++;
schedulesToDisplay.put(item.hour, nbHour);
Log.d(item.toString()+ " : " + nbHour +" spans ("+position+")", TAG);
if(nbHour > maxSpans){
maxSpans = nbHour;
}
}
mDataset.add(position, item);
notifyItemInserted(position);

layoutManager.setSpanCount(maxSpans);
layoutManager.requestLayout();
}

关于android - RecyclerView、GridLayoutManager 和动态 Spans 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640233/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com