升级到23.2.1后,RecyclerView不显示任何内容

时间:2016-10-11 06:49:08

标签: java android listview android-recyclerview adapter

我的活动页脚中有一个带有不同ViewType的RecyclerView。我遵循了有关如何将ListView(因为我之前使用过)转换为RecyclerView的所有步骤,并且它在我第一次运行时工作。在我将其更新为23.2.1之后再次测试时,它没有显示任何内容,并且日志中没有错误消息。

RecyclerView位于具有3种不同视图类型的FrameLayout中。我已经将RecyclerView的layout_height更改为WRAP_CONTENT,因为我已经更新到23.2.1 ..

我不知道我哪里错了。我已经阅读了与此相关的所有问题但仍然没有运气。

这是我的ViewHolder:

public class GenericViewHolder extends RecyclerView.ViewHolder {

        private TextView scheme;
        private TextView date;
        private TextView price;
        private TextView news;
        private ImageView egaLogo;
        private TextView egaText;

        public GenericViewHolder(View convertView, int viewType) {
            super(convertView);
            if (viewType == TYPE_BROKER) {
                scheme = (TextView) convertView.findViewById(R.id.scheme);
                date = (TextView) convertView.findViewById(R.id.date);
                price = (TextView) convertView.findViewById(R.id.price);
            } else if (viewType == TYPE_NEWS) {
                news = (TextView) convertView.findViewById(R.id.news);
                news.setVisibility(convertView.GONE);
            } else if (viewType == TYPE_EGA) {
                egaLogo = (ImageView) convertView.findViewById(R.id.ega_logo);
                egaText = (TextView) convertView.findViewById(R.id.ega_text);
            }
        }
    }

适配器:

public class SpotPriceAdapter extends RecyclerView.Adapter<GenericViewHolder> {

    private List<SpotPrice> data;
    private Context context;

    public SpotPriceAdapter(Context context, List<SpotPrice> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public GenericViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View itemView;
        GenericViewHolder gViewHolder;

        if (viewType == TYPE_BROKER) {
            itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.spot_price_broker, viewGroup, false);
            gViewHolder = new GenericViewHolder(itemView, viewType);
            return gViewHolder;
        } else if (viewType == TYPE_NEWS) {
            itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.spot_price_news, viewGroup, false);
            gViewHolder = new GenericViewHolder(itemView, viewType);
            return gViewHolder;
        } else if (viewType == TYPE_EGA) {
            itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.spot_price_ega, viewGroup, false);
            gViewHolder = new GenericViewHolder(itemView, viewType);
            return gViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder (GenericViewHolder holder, int position) {
        if (holder.getItemViewType() == TYPE_BROKER) { 
            // For Broker Prices View
        } else if (holder.getItemViewType() == TYPE_NEWS) { 
            // For News View
        } else if (holder.getItemViewType() == TYPE_EGA) { 
            // For EGA View
        }
    }

    private SpotPrice getItem (int position) {
        return data.get (position);
    }

    private boolean isPositionNews (int position) {
        return position == data.size () - 1;
    }

    private boolean isPositionEGA (int position) {
        return position == data.size() - 2;
    }

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

    @Override
    public int getItemViewType (int position) {
        if (isPositionNews(position)) {
            return TYPE_NEWS;
        }
        if (isPositionEGA(position)) {
            return TYPE_EGA;
        }
        return TYPE_BROKER;
    }

}

如果有人可以帮助我,我会非常感激!!

修改

这是onCreateView上的Adapter调用代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_spot_price, container, false);

    spotList = (RecyclerView) view.findViewById(R.id.spot_list);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false) {
        @Override
        public boolean canScrollHorizontally() {
            return false;
        }
    };

    spotList.setLayoutManager(layoutManager);

    Animation marquee = AnimationUtils.loadAnimation(getActivity(), R.anim.marquee);
    //spotList.startAnimation(marquee);

    //Try and get the spot prices from share preferences first
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String jsonSpotPrices = sp.getString(SpotPrice.PREF_ENTRY,"[]"); //Default empty JSON Array.
    List<SpotPrice> spotPrices = SpotPrice.jsonArrayToList(jsonSpotPrices);
    spotPriceAdapter = new SpotPriceAdapter(getActivity(), spotPrices);
    spotList.setAdapter(spotPriceAdapter);

    return view;
}

0 个答案:

没有答案