简体   繁体   English

我的RecyclerView实现有什么问题?

[英]What's wrong with my implementation of RecyclerView?

I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView . 我正在关注Android开发人员关于如何设置RecyclerView的说明,但遇到一些错误,例如android.widget.LinearLayout cannot be cast to android.widget.TextView

The code taken from here : RecyclerView 从这里获取的代码: RecyclerView

This is my code, including all necessary files: 这是我的代码,包括所有必需的文件:

MainActivity.java: MainActivity.java:

package com.example.lastlocation.recyclerviewer;

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

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);
        String[] myDataset = new String[5];
        myDataset[0] = "Data0";
        myDataset[1] = "Data1";
        myDataset[2] = "Data2";
        myDataset[3] = "Data3";
        myDataset[4] = "Data4";

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

This is MyAdapter.java: 这是MyAdapter.java:

package com.example.lastlocation.recyclerviewer;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    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 MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(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.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {


        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder 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;
    }
}

this is activity_main.xml: 这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <View
        android:id="@+id/view"
        android:layout_width="386dp"
        android:layout_height="135dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="66dp"
        android:layout_height="64dp"
        android:layout_marginTop="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view"
        app:srcCompat="@android:color/holo_orange_dark" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="68dp"
        android:layout_height="30dp"
        android:layout_marginTop="8dp"
        android:text="John Doe"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />


    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="375dp"
        android:layout_height="367dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.428"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view"
        app:layout_constraintVertical_bias="1.0" />



</android.support.constraint.ConstraintLayout>

And this is the xml for a single text view called my_text_view.xml: 这是一个名为my_text_view.xml的单个文本视图的xml:

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


<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

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


    </LinearLayout> 

As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem. 如您所见,我几乎从Android Developer的网站复制了代码,但我仍然不知道问题出在哪里。

  public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

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

The problem is in your ViewHolder constructor it should be like that 问题是您的ViewHolder构造函数中应该是这样的

public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(View v) {
            super(v);
            mTextView = v.findViewById(R.id.your_text_view_id);
        }
    }


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


        View v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. 您尝试将LinearLayout(即构造函数中的View)分配给TextView,并且崩溃。 This will fix the problem. 这样可以解决问题。 The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it. 在ViewHolder中作为构造函数参数接收的视图是ViewHolder的根视图,在本例中为LinearLayout,从该根视图中可以找到其他视图。

You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below 您错误地夸大了行视图,因此没有以正确的方式在viewholder中分配视图,请如下更改代码

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

and change your viewholder as below 然后按如下所示更改您的观看者

public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(View v) {
            super(v);
            mTextView = v.findViewById(R.id.my_text_view);
        }
    }

Just Simply you need to change your onCreateViewHolder method by taking as 只是,您需要通过以下方式更改onCreateViewHolder方法:

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
return view;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM