Reputation: 979
I have 3 RecyclerViews in a LinearLayout. My goal is to show all of them sequentially, so the 1st RecylerView is on top, the 2nd RecyclerView is in the middle and the 3rd RecyclerView is at the bottom. But the 3rd RecyclerView does not appear on the screen. In the following you can see my files:
// GroupsScreenActivity.java
public class GroupsScreenActivity extends AppCompatActivity {
private RecyclerView recyclerViewGoldenGroup;
private RecyclerView recyclerViewSilverGroup;
private RecyclerView recyclerViewBronceGroup;
private GoldenRecyclerViewAdapter goldenGroupRecyclerViewAdapter;
private SilverGroupRecyclerViewAdapter silverGroupRecyclerViewAdapter;
private BronceRecyclerViewAdapter bronceGroupRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groups_screen);
// get a reference to the golden recyclerView
recyclerViewGoldenGroup = (RecyclerView) findViewById(R.id.rvGoldenGroup);
// create the adapter
goldenGroupRecyclerViewAdapter = new GoldenRecyclerViewAdapter(getSampleArrayList());
// create the LinearLayoutManager
LinearLayoutManager goldenLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// set the LayoutManager
recyclerViewGoldenGroup.setLayoutManager(goldenLayoutManager);
// set the adapter
recyclerViewGoldenGroup.setAdapter(goldenGroupRecyclerViewAdapter);
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
// get a reference to the silver RecyclerView
recyclerViewSilverGroup = (RecyclerView) findViewById(R.id.rvSilverGroup);
// create the adapter
silverGroupRecyclerViewAdapter = new SilverGroupRecyclerViewAdapter(getSampleArrayList());
// create the LinearLayoutManager
LinearLayoutManager silverLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// set the LayoutManager
recyclerViewSilverGroup.setLayoutManager(silverLayoutManager);
// set the adapter
recyclerViewSilverGroup.setAdapter(silverGroupRecyclerViewAdapter);
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// get the reference to the bronce RecyclerView
recyclerViewBronceGroup = (RecyclerView) findViewById(R.id.rvBronceGroup);
// create the adapter
bronceGroupRecyclerViewAdapter = new BronceRecyclerViewAdapter(getSampleArrayList());
// create the LayoutManager
LinearLayoutManager bronceLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// set the LayoutManager
recyclerViewBronceGroup.setLayoutManager(bronceLayoutManager);
// set the adapter
recyclerViewSilverGroup.setAdapter(bronceGroupRecyclerViewAdapter);
}
private ArrayList<Object> getSampleArrayList() {
ArrayList<Object> items = new ArrayList<>();
items.add(new User("Dany Targaryen", "Valyria"));
items.add(new User("Rob Stark", "Winterfell"));
items.add("image");
items.add(new User("Jon Snow", "Castle Black"));
items.add("image");
items.add(new User("Tyrion Lanister", "King's Landing"));
return items;
}
}
In the following, you can see the layout file. This consists of a ConstraintLayout with a vertical LinearLayout which has 3 RecyclerViews as its children:
<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=".GroupsScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvGoldenGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvSilverGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvBronceGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
For the sake of brevity, I only add the RecyclerView.Adapter subclass of the top RecyclerView. The other two are simply the same:
public class GoldenRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// The items to display in your RecyclerView
private List<Object> items;
private final int USER = 0, IMAGE = 1;
// Provide a suitable constructor (depends on the kind of dataset)
public GoldenRecyclerViewAdapter(List<Object> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.items.size();
}
// we need to override this method to tell the
// "RecyclerView" about the type of view to inflate
// based on the position
// we'll return USER or IMAGE based on the type object
// in the data we have
@Override
public int getItemViewType(int position) {
if(items.get(position) instanceof User){
return USER;
}
else if(items.get(position) instanceof String){
return IMAGE;
}
return -1;
}
// we need to override this method
// to tell the "RecyclerView.Adapter" about which "RecyclerView.ViewHolder"
// object to create based on the "viewType" returned
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater
.from(viewGroup.getContext());
switch (viewType){
case USER:
View v1 = inflater.inflate(
R.layout.layout_viewholder1,
viewGroup,
false);
viewHolder = new ViewHolder1(v1);
break;
case IMAGE:
View v2 = inflater.inflate(
R.layout.layout_viewholder2,
viewGroup,
false);
viewHolder = new ViewHolder2(v2);
break;
default:
View view = inflater.inflate(
android.R.layout.simple_list_item_1,
viewGroup,
false);
viewHolder = new RecyclerViewSimpleTextViewHolder(view);
break;
}
return viewHolder;
}
// we override this method
// to configure the "ViewHolder" with actual data that needs to be
// displayed
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case USER:
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
break;
case IMAGE:
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2);
break;
default:
RecyclerViewSimpleTextViewHolder vh = (RecyclerViewSimpleTextViewHolder) viewHolder;
configureDefaultViewHolder(vh, position);
break;
}
}
private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
vh.getTv().setText((CharSequence) items.get(position));
}
private void configureViewHolder1(ViewHolder1 vh1, int position) {
User user = (User) items.get(position);
if (user != null) {
vh1.getLabel1().setText("Name: " + user.getName());
vh1.getLabel2().setText("Hometown: " + user.getHomeTown());
}
}
private void configureViewHolder2(ViewHolder2 vh2) {
vh2.getIvExample().setImageResource(R.drawable.ic_launcher_background);
}
}
I hope somebody can help. This is how it looks like. As you can see, only the 1st and 2nd RecylerView are displayed on the screen.
Upvotes: 0
Views: 477
Reputation: 634
Replace the following portion of layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvGoldenGroup"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvSilverGroup"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvBronceGroup"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
</LinearLayout>
Upvotes: 1
Reputation: 1315
You are set the BronceAdapter to the SilverRecyclerView in this
recyclerViewSilverGroup.setAdapter(bronceGroupRecyclerViewAdapter);
Replace the RecyclerView
to the Bronze one and should work.
Hope it helps.
Upvotes: 1