我非常简单的Android RecycleView测试带来的麻烦

时间:2018-12-04 20:37:15

标签: android android-recyclerview

美好的一天。

我有一个非常简单的Android RecycleView测试:

在build.gradle中:

dependencies {
...
implementation 'com.android.support:recyclerview-v7:28.0.0'
...
}

在布局activity_main.xml中:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

在布局activity_list_item.xml中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/itemNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="itemNumber"/>
</FrameLayout>

在MainActivity.java中:

   package com.as.recyclerview_test;

   import ...; 

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

        public static String[] myDataset = {"111", "222", "333"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mRecyclerView = findViewById(R.id.my_recycler_view);
       mRecyclerView.setHasFixedSize(true);
       mLayoutManager = new LinearLayoutManager(this);
       mRecyclerView.setLayoutManager(mLayoutManager);
       mAdapter = new MyAdapter(myDataset);
       mRecyclerView.setAdapter(mAdapter);
   }
}

在MyAdapter.java中:

package com.as.recyclerview_test; 

import ...;

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

    private String[] mDataset;

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView itemNumber;
        public MyViewHolder(TextView v) {
            super(v);
            itemNumber = v.findViewById(R.id.itemNumber);
        }
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        TextView v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.activity_list_item, viewGroup, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.itemNumber.setText(mDataset[position]);
    }

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

}

运行该项目时,我收到应用停止消息,并且

致命异常:主要

使用

java.lang.ClassCastException:android.widget.FrameLayout无法在com.as.recyclerview_test.MyAdapter.onCreateViewHolder

上转换为android.widget.TextView。 >

我使用了官方的[https://developer.android.com/guide/topics/ui/layout/recyclerview#java]模式。

我的代码有什么问题? 预先谢谢你...

2 个答案:

答案 0 :(得分:0)

onCreateViewHolder方法出错。您正在尝试在扩展布局时将FrameLayout投射到TextView。这是正确的方法。

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.activity_list_item, viewGroup, false);
    return new MyViewHolder(v);
}

另外,您应该将View传递给ViewHolder构造函数,而不是TextView。

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView itemNumber;
    public MyViewHolder(View v) {
        super(v);
        itemNumber = v.findViewById(R.id.itemNumber);
    }
}

答案 1 :(得分:0)

您可以尝试以下方法:

FrameLayout view  = (FrameLayout)(LayoutInflater.From(viewGroup.getContext()).
    .inflate(R.layout.activity_list_item, viewGroup, false));

MyViewHolder vh = new MyViewHolder(view);
return MyViewHolder;