无法设置Recycler View

时间:2016-01-19 00:26:11

标签: android android-recyclerview

我一直在使用ListView并立即尝试使用Recycler视图。一直试图按照基本设置的文档,但令人沮丧地无法这样做。

ViewHolder类应该接受TextView参数,但是在文档站点中,示例是输入View作为参数,我不知道它应该如何工作。我试图用TextView强制转换它并得到一个ClassCastException。我在上面注释了错误,这使得我无法编译。错误在MyAdapter类中。

如果我只是在阅读文档时表示不满,我会道歉,否则这来自官方网站是非常荒谬的。 Official page

MainActivity

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);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        String[] myDataset = {"item 1", "item 2"};

        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
}

MyAdapter Class

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

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

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

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false);

        //THIS is the line causing error. v is View. I should be inputting a textview. 
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

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

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

}

my textview 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView" />
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

你从膨胀中得到LinearLayout

View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
// v <- LinearLayout

现在,您可以

  1. 删除包装LinearLayout,以便在xml中获得TextView,然后需要转换适配器

    ViewHolder vh = new ViewHolder((TextView) v);
    
  2. 调整ViewHolder以接受View或LinearLayout

    public ViewHolder(View v) {
      mTextView = (TextView) v.findViewById(R.id.text);
    }
    

答案 1 :(得分:1)

您可以执行以下操作之一。或者更改ViewHolder类的构造函数,将其作为参数View,如此

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

        public ViewHolder(View v) {
            super(v);
        }
}

并更改onCreateViewHolder这样的方法

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

        ViewHolder vh = new ViewHolder(v);
        vh.name = (TextView) v.findViewById(R.id.textView);

        return vh;
    }

或者您可以保留构造函数,并像这样更改onCreateViewHolder方法

@Override
public MemebershipAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.site_item, parent, false);

    TextView mTextView = (TextView) v.findViewById(R.id.textview);

    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(mTextView);

    return vh;
}

答案 2 :(得分:0)

您的ViewHolder必须如下所示:

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        mTextView = v.findViewById(R.id.textViewId);
    }
}
不要忘记把android:id =&#34; @ + id / textViewId&#34;到布局中的TextView