child recyclerview方法没有调用(onCreateViewHolder,onBindViewHolder)。但getItemCount()方法正在调用

时间:2016-04-21 05:52:06

标签: android android-recyclerview

我有一个recyclerview,它将垂直显示数据。一行将水平显示数据,因此我使用了另一个recyclerview。这是来自第一个recyclerview的onBindViewHolder。

父recylerview正在运行,但是当我添加子回收器视图时。这些方法没有调用(onCreateViewHolder,onBindViewHolder)。可能是xml文件中存在问题。

父xml: -

viewstat

我在onCreateViewHolder中使用的子xml显示行

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.catrackapp.catrack.MainActivity" >


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="VIDEO LATEST"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:gravity="center" />

</RelativeLayout>

这个xml正在由child recyclerview使用。

<?xml version="1.0" encoding="utf-8"?>


<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</android.support.design.widget.CoordinatorLayout>

这些是代码父代和子代。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<com.google.android.youtube.player.YouTubeThumbnailView
    android:id="@+id/thumbnail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/no_thumbnail"
    android:adjustViewBounds="true" />

</LinearLayout>

请帮助我。我想在最近4天修复它。但现在能够解决。

更新: -

我使用它,现在孩子所有方法都在调用,但布局没有显示。

 public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {

    private List<Videos> moviesList;
   // private ThumbnailListener thumbnailListener;

    public class MyViewHolder extends RecyclerView.ViewHolder {
       // public VideoView videoView;
        //public ImageView imageView;
     //  YouTubeThumbnailView thumbnail;


        RecyclerView recyclerView1;
        public MyViewHolder(View view) {
            super(view);
           // videoView = (VideoView) view.findViewById(R.id.videoView);
           // imageView = (ImageView) view.findViewById(R.id.imageView);
          //  thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
            recyclerView1 = (RecyclerView) view.findViewById(R.id.recycler_view1);
          //  LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
           // recyclerView1.setLayoutManager(layoutManager);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
            recyclerView1.setLayoutManager(mLayoutManager);


        }
    }


    public MoviesAdapter(List<Videos> moviesList) {
        this.moviesList = moviesList;
        //thumbnailListener = new ThumbnailListener();
    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Videos movie = moviesList.get(position);
        System.out.println("first = " + movie.getImage());


        String url = movie.getVideo_url();
        final String m = url.substring(url.lastIndexOf('/') + 1);
        final String txt = movie.getPlain_text2();
        System.out.println("url = "+m);

        List<String> l = new ArrayList<String>();
        l.add(m);
        l.add(m);
        l.add(m);
        l.add(m);
        l.add(m);

        final MoviesAdapterHorizontal adapter = new MoviesAdapterHorizontal(l);
        holder.recyclerView1.setAdapter(adapter);
       // holder.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter.notifyDataSetChanged();

    }

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

    private final class ThumbnailListener implements
            YouTubeThumbnailView.OnInitializedListener,
            YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onInitializationSuccess(
                YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
            loader.setOnThumbnailLoadedListener(this);


            // thumbnailViewToLoaderMap.put(view, loader);
           // view.setImageResource(R.drawable.loading_thumbnail);
           String videoId = (String) view.getTag();
          loader.setVideo(videoId);
        }

        @Override
        public void onInitializationFailure(
                YouTubeThumbnailView view, YouTubeInitializationResult loader) {
            view.setImageResource(R.drawable.no_thumbnail);
        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView view, YouTubeThumbnailLoader.ErrorReason errorReason) {
            view.setImageResource(R.drawable.no_thumbnail);
        }
    }

}


//Horizontal images view.
public class MoviesAdapterHorizontal extends RecyclerView.Adapter<MoviesAdapterHorizontal.MyViewHolder> {

    private List<String> moviesList;
    private ThumbnailListener thumbnailListener;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        YouTubeThumbnailView thumbnail;
        public MyViewHolder(View view) {
            super(view);
               thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
        }
    }

    public MoviesAdapterHorizontal(List<String> moviesList) {
        this.moviesList = moviesList;
       thumbnailListener = new ThumbnailListener();
        System.out.println("called size = "+this.moviesList.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.hotrow, parent, false);
        System.out.println("onCreateViewHolder called");

        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final String url = moviesList.get(position);
        System.out.println("rahul url" + url);

        final String m = url.substring(url.lastIndexOf('/') + 1);
        System.out.println("url = "+m);

        holder.thumbnail.setTag(m);
        holder.thumbnail.initialize("AIzaSyDW-sxPUqy2rD6ZWs3vTNb0jKEKA21RjrY", thumbnailListener);

        holder.thumbnail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FullscreenDemoActivity.class);
                intent.putExtra("url", m);
                intent.putExtra("txt", "");
                startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        System.out.println("moviesList 2 size = "+this.moviesList.size());
        return this.moviesList.size();
    }

    private final class ThumbnailListener implements
            YouTubeThumbnailView.OnInitializedListener,
            YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onInitializationSuccess(
                YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
            loader.setOnThumbnailLoadedListener(this);
            String videoId = (String) view.getTag();
            loader.setVideo(videoId);
        }

        @Override
        public void onInitializationFailure(
                YouTubeThumbnailView view, YouTubeInitializationResult loader) {
            view.setImageResource(R.drawable.no_thumbnail);
        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView view, YouTubeThumbnailLoader.ErrorReason errorReason) {
            view.setImageResource(R.drawable.no_thumbnail);
        }
    }

}

我在日志中看到以下错误

 MoviesAdapterHorizontal adapter = new MoviesAdapterHorizontal(l);
        holder.recyclerView1.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        holder.recyclerView1.setAdapter(adapter);
        adapter.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:0)

CoordinatorLayout版面中,您无法将RecyclerView身高设置为android:layout_height="wrap_content"

尝试设置

android:layout_height="200dp"

OR

android:layout_height="match_parent"