Recyclerview项目不可见

时间:2017-01-31 07:32:25

标签: android android-recyclerview

我在片段中有一个recyclerview。我正在使用butterknife库。

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

然后适配器类将是

public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.MyViewHolder> {
    private List<StudentData> dataList;
    private Context context;

    public PeopleAdapter(List<StudentData> data, Context context) {
        this.dataList = data;
        this.context = context;
    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        StudentData data=dataList.get(position);
        holder.email.setText(data.getEmail());
        holder.name.setText(data.getName());
        holder.phone.setText(data.getPhoneNo());
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.name)
        TextView name;
        @BindView(R.id.email)
        TextView email;
        @BindView(R.id.phone)
        TextView phone;
        @BindView(R.id.image)
        TextView image;
    public MyViewHolder(View view) {
        super(view);
        ButterKnife.bind(this,view);
    }
}

}

我用

初始化了recyclerview
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(mLayoutManager);
        mRecyclerview.setAdapter(new PeopleAdapter(MyConfiguration.getTemData(),getActivity()));

数据生成方法

public static List<StudentData> getTemData(){
        List<StudentData> dataList=new ArrayList<>();
        for (int i=0;i<10;i++){
            dataList.add(new StudentData("myemail"+i+"@gmail.com",i*45234523+"","myname"+i));
        }
        return dataList;
    }

列表项

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="50dp"
        app:srcCompat="@drawable/profilepic"
        android:id="@+id/image"
        android:layout_marginTop="5dp"
        android:layout_height="60dp" />

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/student_listcard_left_margin"
        android:layout_marginRight="@dimen/student_listcard_rightmargin"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:textSize="18sp"
                android:textColor="@android:color/black" />

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

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_edit"
            android:id="@+id/imageView5"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

StudentData类

public class StudentData {
    private String name;
    private String email;

    public StudentData(String email, String phoneNo, String name) {
        this.email = email;
        this.phoneNo = phoneNo;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public StudentData() {

    }

    private String phoneNo;
    }

父视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ex.ex.fragments.student.StudentInformation">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabSelectedTextColor="@android:color/white"
            app:tabMode="scrollable"/>


    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewpager" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout

所以问题是recyclerview没有显示它的项目。请帮助我;

2 个答案:

答案 0 :(得分:2)

我不知道ButterKnife,但我认为是

以下是imageview

<ImageView
    android:layout_width="50dp"
    app:srcCompat="@drawable/profilepic"
    android:id="@+id/image"
    android:layout_marginTop="5dp"
    android:layout_height="60dp" />

并且您将其与textview绑定

@BindView(R.id.image)
TextView image;

所以请将其替换为

@BindView(R.id.image)
ImageView image;

答案 1 :(得分:0)

我的错误,我被忽略了。

我有一个viewpager,因为我做了所有这些。但是由嵌套的滚动视图包围的viewpager,其大小为空......

gdb

所以我删除了view pager的父级,它变成了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ex.ex.fragments.student.StudentInformation">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabSelectedTextColor="@android:color/white"
            app:tabMode="scrollable"/>


    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewpager" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
相关问题