Android中的RecyclerView实现出错

时间:2017-02-05 03:29:06

标签: android android-recyclerview

有人可以解释一下RecyclerView在android中的工作原理吗?

我使用此网站作为参考:https://developer.android.com/training/material/lists-cards.html#CardView

但是当我编译相同的代码时,我收到了一个错误。

任何帮助将不胜感激。谢谢。

MyAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

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

        ViewHolder vh = new ViewHolder(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
        holder.mTextView.setText(mDataset[position]);

    }

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

RecyclerActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private String[] myDataset = {"hellow"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
}

my_text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>

activity_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bloxofcode.listviewcustom.RecyclerActivity">

    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

遇到错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.bloxofcode.listviewcustom, PID: 27529
                  java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                      at com.bloxofcode.listviewcustom.MyAdapter.onCreateViewHolder(MyAdapter.java:37)
                      at com.bloxofcode.listviewcustom.MyAdapter.onCreateViewHolder(MyAdapter.java:12)

2 个答案:

答案 0 :(得分:1)

首先改变你的my_text_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:
   android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" 
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

   <TextView 
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:id="@+id/text_view"/>

</LinearLayout>

现在更改您的MyAdapter.java类:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
       public TextView mTextView;
       public ViewHolder(View v) {
           super(v);
           mTextView = (TextView) v.findViewById(R.id.text_view);
       }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
       mDataset = myDataset;
    }

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

       ViewHolder vh = new ViewHolder(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
        holder.mTextView.setText(mDataset[position]);

    }

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

答案 1 :(得分:0)

RecyclerView is more advanced version of ListView with improved performance 

和其他好处。首先,你必须创建一个适配器类 onCreateViewHolder()膨胀布局(my_text_view.xml)和onBindViewHolder()为每一行设置适当的数据。

so first of all make some changes in your my_text_view.xml file and in adapter class.

1. add a TextView field in my_text_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2. Do following changes in MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;
    private static final String TAG = "MyAdapter";

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        // change 1: ViewHolder wants View
        public ViewHolder(View view) {
            super(view);

            // change 2:  instanciate the mTextView
         /*   mTextView = v;  delete this line  */
            mTextView = (TextView) view.findViewById(R.id.tv);
        }
    }

    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        //Change 3: inflate the layout
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
        // return the view
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
        Log.e(TAG, "data: " + mDataset[position]);

    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}


3. there is no need to change MyAdapter.java n its xml file.