在PagerAdapter中多次创建RecyclerView

时间:2018-06-04 07:40:45

标签: android android-recyclerview android-viewpager horizontal-scrolling android-pageradapter

我有一个ViewPager适配器,在这个适配器中我有一个可水平滚动的循环器视图(如小时选择器)。这个小时选择器滚动到页面开始的最后位置。一切正常,但这个滚动过程在页面开始时磕磕绊绊。当它需要关注最后一个位置时,它会关注前一个位置,然后关注最后一个位置。当我检查日志时,recylerview被创建超过2次(我知道由于寻呼机适配器的性质,2次是好的),但是对于一个页面,它被创建了2次。我无法弄清楚我做错了什么。 提前谢谢。

public class FavoritesViewPagerAdapter extends PagerAdapter implements View.OnClickListener, View.OnTouchListener {
private Activity mActivity;
private List<Station> mListFavoriteStations;

private Calendar calendar = Calendar.getInstance();
private SimpleDateFormat dateFormat = Util.dateFormatAdapters;
private FavoritesViewPagerAdapterListener mFavoritesViewPagerAdapterListener;
private Map<String, StationDetail> mFavoriteStationMap = new HashMap<>();


public interface FavoritesViewPagerAdapterListener {
    void onHourPickerScroll(boolean enablePaging);

    void onValueInfoClick();
}

public FavoritesViewPagerAdapter(Activity activity, List<Station> listFavoriteStations, FavoritesViewPagerAdapterListener favoritesViewPagerAdapterListener) {
    mActivity = activity;
    mListFavoriteStations = listFavoriteStations;
    mFavoritesViewPagerAdapterListener = favoritesViewPagerAdapterListener;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public int getCount() {
    return mListFavoriteStations.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    Log.d("Inside", "Pager");
    LayoutInflater inflater = LayoutInflater.from(mActivity);
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.adapter_viewpager_favorites_item, container, false);
    final ViewHolder holder = new ViewHolder(view);

    final String selectedStationId = mListFavoriteStations.get(position).getId();

    holder.horizontalPicker.setOnTouchListener(this);

    final ViewTreeObserver vto = holder.horizontalPicker.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int padding = holder.horizontalPicker.getWidth() / 2 - 25;
            holder.horizontalPicker.setPadding(padding, 0, padding, 0);
            holder.horizontalPicker.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            Log.d("Inside", "Pager" + padding);
        }
    });

    holder.textViewCurrentStation.setText(mActivity.getString(R.string.station_name_extension, mListFavoriteStations.get(position).getName().toUpperCase(new Locale("tr", "TR"))));
    holder.textViewDate.setText(dateFormat.format(calendar.getTime()));

    if (mFavoriteStationMap.containsKey(selectedStationId)) {
        StationDetail stationDetail = mFavoriteStationMap.get(selectedStationId);
        initHashMap(holder, stationDetail);
    } else {
        getStationDetail(selectedStationId, holder);
    }

    container.addView(view);
    return view;
}

//...

private void getStationDetail(final String selectedStationId, final ViewHolder holder) {
    WSUtil.getCall(mActivity, null, WSAdapter.getWSI().getStationDetail(selectedStationId), null, null, true, false,
            new WSUtil.WSCallback<ServiceRootObjectGetObject<StationDetail>>() {
                @Override
                public void onSuccess(final ServiceRootObjectGetObject<StationDetail> response) {
                    if (response.getObjects() != null) {
                        final StationDetail stationDetail = response.getObjects();
                        //...
                        fillStationDetail(stationDetail, holder);
                        Log.e("Recycler", "in on success");
                        createdStationDetail(stationDetail, holder);
                        Log.d("Recycler", "Pager 1");
                    }
                }

                @Override
                public void onFailure() {
                }
            }, null);
}

//...

private void fillStationDetail(final StationDetail stationDetail, final ViewHolder holder) {
    holder.textViewCurrentStation.setText(mActivity.getString(R.string.station_name_extension, stationDetail.getStationInfo().getName().toUpperCase(new Locale("tr", "TR"))));


    PickerLayoutManager pickerLayoutManager = new PickerLayoutManager(mActivity, PickerLayoutManager.HORIZONTAL, false);
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(holder.horizontalPicker);
    holder.horizontalPicker.setLayoutManager(pickerLayoutManager);

    pickerLayoutManager.setOnScrollStopListener(new PickerLayoutManager.onScrollStopListener() {
        @Override
        public void selectedView(View view) {
            int index = (int) view.getTag();
            Log.e("Recycler", "" + index);
            fillPageData(holder, stationDetail, index);
        }
    });

    fillHourPicker(stationDetail, holder);
    holder.horizontalPicker.smoothScrollBy(100000, 0);

}

private void fillPageData(ViewHolder holder, StationDetail stationDetail, int index) {


    SimpleDateFormat formatHour = Util.simpleDateFormatAllDate;
    String dateString = formatHour.format(stationDetail.getAQIHourly()[index].getDataDate());
    holder.textViewDate.setText(dateString);
}

private void fillHourPicker(StationDetail stationDetail, ViewHolder holder) {
    List<String> newHourList = new ArrayList<>();
    if (stationDetail.getAQIHourly() != null) {
        for (int i = 0; i <= stationDetail.getAQIHourly().length - 1; i++) {
            AQI aqi = stationDetail.getAQIHourly()[i];
            Date date = aqi.getDataDate();
            SimpleDateFormat format = Util.simleDateFormatOnlyHour;
            String DateToStr = format.format(date);
            newHourList.add(DateToStr);
        }
    }

    holder.horizontalPicker.setAdapter(new HourPickerAdapter(newHourList));
    //holder.horizontalPicker.smoothScrollToPosition(stationDetail.getAQIHourly().length - 1);
}

}

我的水平小时选择器适配器:

public class HourPickerAdapter extends RecyclerView.Adapter<HourPickerAdapter.ViewHolder> {
private List<String> mHourList;

public HourPickerAdapter(List<String> mHourList) {
    this.mHourList = mHourList;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.adapter_hour_picker_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.itemView.setTag(position);
    holder.textViewHour.setText(mHourList.get(position));
}

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

class ViewHolder extends RecyclerView.ViewHolder {

    private TextView textViewHour;

    public ViewHolder(View itemView) {
        super(itemView);
        textViewHour = itemView.findViewById(R.id.textViewHour);
    }
}

}

这是我的选择器布局管理器(用于监听回收站视图滚动事件):

public class PickerLayoutManager extends LinearLayoutManager {
private PickerLayoutManager.onScrollStopListener onScrollStopListener;

public PickerLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    int orientation = this.getOrientation();
    if (orientation == 0) {
        int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
        return scrolled;
    } else {
        return 0;
    }
}

public void onScrollStateChanged(int state) {
    super.onScrollStateChanged(state);
    if (state == 0 && this.onScrollStopListener != null) {
        int selected = 0;
        int widthHalf = this.getWidth() / 2;
        int widthMin = widthHalf - 100;
        int widthMax = widthHalf + 100;

        for (int i = 0; i < this.getChildCount(); ++i) {
            if (widthMin < this.getChildAt(i).getX() && widthMax > this.getChildAt(i).getX()) {
                selected = i;
                break;
            }
        }

        this.onScrollStopListener.selectedView(this.getChildAt(selected));
    }

}

public void setOnScrollStopListener(PickerLayoutManager.onScrollStopListener onScrollStopListener) {
    this.onScrollStopListener = onScrollStopListener;
}

public interface onScrollStopListener {
    void selectedView(View var1);
}

}

这是日志:

enter image description here

0 个答案:

没有答案