具有不同布局的Android RecyclerView不起作用

时间:2017-04-05 19:37:49

标签: java android android-recyclerview recycler-adapter

我正在尝试使用两种不同的布局实现RecyclerView。在MainActivity我首先创建ListItem对象并将其添加到列表中,然后将其添加到适配器,但它没有显示。此外,我正在调用一个创建ListItem对象的方法,并将其添加到列表中,然后通知适配器,但它仍然没有显示。到目前为止,这是我的代码:

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="nl.iansoft.www.recyclerviewproblem.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"
        />

</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ArrayList<ListItem> listItems;
    private CustomAdapter customAdapter;
    private RecyclerView rvList;

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

        rvList = (RecyclerView)findViewById(R.id.rvList);
        listItems = new ArrayList<>();
        ListItem listItem = new ListItem("MyName", "MyLatsName", "MyEmail");
        listItems.add(listItem);
        customAdapter = new CustomAdapter(listItems);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(mLayoutManager);
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.setAdapter(customAdapter);

        addItemToTheList();
    }

    private void addItemToTheList(){
        ListItem listItem = new ListItem("MyName", "MyLatsName", "MyEmail");
        listItems.add(listItem);
        customAdapter.notifyDataSetChanged();
    }
}

CustomAdapter.java

public class CustomAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<ListItem> listItems;

    public class ListItem1ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvLastName;
        public TextView tvEmail;

        public ListItem1ViewHolder(View view) {
            super(view);
            tvName = (TextView) view.findViewById(R.id.tvName);
            tvLastName = (TextView) view.findViewById(R.id.tvLastName);
            tvEmail = (TextView) view.findViewById(R.id.tvEmail);
        }
    }

    public class ListItem2ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName2;
        public TextView tvLastName2;
        public TextView tvEmail2;

        public ListItem2ViewHolder(View view) {
            super(view);

            tvName2 = (TextView) view.findViewById(R.id.tvName2);
            tvLastName2 = (TextView) view.findViewById(R.id.tvLastName2);
            tvEmail2 = (TextView) view.findViewById(R.id.tvEmail2);
        }
    }


    public CustomAdapter(ArrayList<ListItem> listItems) {
        this.listItems = listItems;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        if(viewType == 1){
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.my_list_item, parent, false);
            return new CustomAdapter.ListItem1ViewHolder(itemView);
        }else{
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.my_list_item2, parent, false);
            return new CustomAdapter.ListItem2ViewHolder(itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(listItems.get(position).getName().equals("MyName")){
            return 1;
        }else{
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);
        if(holder.getItemViewType() == 1) {
            ListItem1ViewHolder listItem1ViewHolder = (ListItem1ViewHolder) holder;
            listItem1ViewHolder.tvName.setText(listItem.getName());
            listItem1ViewHolder.tvLastName.setText(listItem.getLastName());
            listItem1ViewHolder.tvEmail.setText(listItem.getEmail());
        }else{
            ListItem2ViewHolder listItem2ViewHolder = (ListItem2ViewHolder)holder;
            listItem2ViewHolder.tvName2.setText(listItem.getName());
            listItem2ViewHolder.tvLastName2.setText(listItem.getLastName());
            listItem2ViewHolder.tvEmail2.setText(listItem.getEmail());
        }
    }

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

ListItem.java

public class ListItem {

    private String name;
    private String lastName;
    private String email;

    public ListItem() {
    }

    public ListItem(String name, String lastName, String email) {
        this.name = name;
        this.lastName = lastName;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

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

my_list_item2.xml

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

    <TextView
        android:id="@+id/tvName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="Name2"
        android:layout_marginTop="15dp"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/tvLastName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="Last Name2"
        android:layout_below="@+id/tvName2"
        android:layout_marginTop="10dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/tvEmail2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="my@email.com"
        android:layout_below="@+id/tvLastName2"
        android:layout_marginTop="5dp"
        android:padding="5dp" />

</RelativeLayout>

my_list_item.xml

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

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="Name"
        android:layout_marginTop="15dp"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/tvLastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="Last Name"
        android:layout_below="@+id/tvName"
        android:layout_marginTop="10dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="my@email.com"
        android:layout_below="@+id/tvLastName"
        android:layout_marginTop="5dp"
        android:padding="5dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

问题出在ConstraintLayout。因为您尚未对RecyclerView

指定任何限制

指定约束,或移动到另一个布局(例如FrameLayout)。

但特别是在这种情况下,您不需要父布局,因为只要您的视图层次结构中只有RecyclerView,它就没用了。