2017-04-05 92 views
1

我試圖用兩種不同的佈局實現RecyclerView。在MainActivity我首先創建ListItem對象並將其添加到列表中,然後將其添加到適配器,但它不顯示。而且我正在調用創建ListItem對象的方法,並將它添加到列表中,然後通知適配器,但它仍不顯示。這是我到目前爲止的代碼:具有不同佈局的Android RecyclerView不起作用

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="[email protected]" 
     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="[email protected]" 
     android:layout_below="@+id/tvLastName" 
     android:layout_marginTop="5dp" 
     android:padding="5dp" /> 

</RelativeLayout> 
+0

你可以發佈'activity_main'嗎? – azizbekian

+0

@azizbekian剛剛添加。 – Nikolaj

+1

你可以把'ConstraintLayout'改成'FrameLayout'嗎? – azizbekian

回答

1

的問題是ConstraintLayout。因爲您沒有爲您的RecyclerView指定任何限制。

要麼指定約束條件,要麼移動到另一個佈局(例如FrameLayout)。

但特別是在這種情況下,您不需要父級佈局,因爲它是無用的,只要您的視圖層次結構中只有RecyclerView即可。