无法以编程方式对回收者视图进行充气

时间:2015-11-27 03:05:30

标签: android android-fragments android-recyclerview android-inflate

我想在片段中以编程方式对滑块和回收器视图进行充气。 滑块成功膨胀,滑动刷新也正常运行。

  

日志中没有错误,但是,后面的视图已经通货膨胀

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View fragmentView = inflater.inflate(R.layout.fragment_conversation, container, false);
        RelativeLayout fragmentRelativeLayout = (RelativeLayout)fragmentView.findViewById(R.id.conversation_fragment_main);

        View header = LayoutInflater.from(getActivity()).inflate(R.layout.slider, container, false);

        View recylerList = LayoutInflater.from(getActivity()).inflate(R.layout.news_layout_recyler_view,container,false);

        headerSlider = (SliderLayout)header.findViewById(R.id.slider);

        TextSliderView textSliderView = new TextSliderView(getActivity().getApplicationContext());
        // initialize a SliderLayout
        textSliderView
                .description("Hello")
                .image("http://media2.intoday.in/indiatoday/images/stories/pawan-kalyan-story-650_010215012726.jpg")
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);

        //add your extra information
        textSliderView.bundle(new Bundle());
        textSliderView.getBundle()
                .putString("extra", "POWER STAR");

        headerSlider.addSlider(textSliderView);

        headerSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        headerSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        headerSlider.setCustomAnimation(new DescriptionAnimation());
        headerSlider.setDuration(4000);
        headerSlider.addOnPageChangeListener(this);

        fragmentRelativeLayout.addView(header);
        fragmentRelativeLayout.addView(recylerList);


        newsList = (RecyclerView)recylerList.findViewById(R.id.listNews);
        //newsList = (RecyclerView) recylerList.findViewById(R.id.listNews);
        newsList.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));

        callBackListener = this;

        newsListObject = retrieveFromLocalDb.retrieveNewsList();

        // Initializing Swipe To Refresh Layout
       //swipeToRefreshNewsList = (SwipeRefreshLayout)fragmentView.findViewById(R.id.swipeToRefreshNews);
        swipeToRefreshNewsList = (SwipeRefreshLayout) recylerList.findViewById(R.id.swipeToRefreshNews);
        swipeToRefreshNewsList.setOnRefreshListener(this);

        adapter = new recylerViewNewsAdapter(getActivity(),newsListObject);

        if(newsListObject.isEmpty()) {
            new loadNewsListInBackground(this).execute();
        }

        adapter.setNewsDataList((ArrayList<newsModelStore>) newsListObject);
        newsList.setAdapter(adapter);
        newsList.setLayoutManager(new LinearLayoutManager(getActivity()));



        return fragmentView;
    }

news_layout_recyler_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeToRefreshNews"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listNews"
        >

    </android.support.v7.widget.RecyclerView>


</android.support.v4.widget.SwipeRefreshLayout>

我很确定我可以在调试日志中看到数据。我不知道它为什么没有出现。适配器如下:

 package com.example.prabhukonchada.fanstationmvp;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by prabhukonchada on 26/11/15.
 */
public class recylerViewNewsAdapter extends RecyclerView.Adapter<recylerViewNewsAdapter.newsListViewHolder> {

    List<newsModelStore> newsModelStoreList = Collections.emptyList();
    private LayoutInflater inflater;
    Context context;

    public recylerViewNewsAdapter(Context context,List<newsModelStore> newsModelStoreList)
    {
        this.newsModelStoreList = newsModelStoreList;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public newsListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.news_layout,parent,false);
        newsListViewHolder newsHolder = new newsListViewHolder(view);

        return newsHolder;
    }

    @Override
    public void onBindViewHolder(newsListViewHolder holder, int position) {

        holder.newsInformation.setText(newsModelStoreList.get(position).getNewsInformation());

        if(newsModelStoreList.get(position).getImageUrl()!= null) {
            Picasso.with(context).load(newsModelStoreList.get(position).getImageUrl()).into(holder.newsImage);
        }else
        {
            Drawable userImageDrawable = context.getResources().getDrawable(R.drawable.super_user);
            holder.newsImage.setImageDrawable(userImageDrawable);
        }

    }

    @Override
    public int getItemCount() {
        return 0;
    }

    class newsListViewHolder extends RecyclerView.ViewHolder
    {
        TextView newsInformation;
        ImageView newsImage;
        TextView timeStatus;
        ImageView awesome;
        TextView awesomenessCount;
        View view;

        public newsListViewHolder(View itemView)
        {
            super(itemView);
            newsInformation = (TextView)itemView.findViewById(R.id.newsInformation);
            newsImage = (ImageView)itemView.findViewById(R.id.newsImage);
//            timeStatus = (TextView)itemView.findViewById(R.id.started_timestamp);
//            awesome = (ImageView)itemView.findViewById(R.id.awesomeImage);
//            awesomenessCount = (TextView)itemView.findViewById(R.id.awesomenessCount);
       }
    }

    public void setNewsDataList(ArrayList<newsModelStore> newsModelStoreList)
    {
        this.newsModelStoreList = newsModelStoreList;
        notifyDataSetChanged();
    }


}

1 个答案:

答案 0 :(得分:1)

难怪,您返回的回收器视图适配器没有记录:

 @Override
    public int getItemCount() {
        return 0;
    }

将其更改为newsModelStoreList.size()