Nitrogen247
Lurker
I'm trying to have 2 fragments on my screen, currently only one is showing up and its vertical instead of horizontal which I assume is because my second fragment isn't showing up.
The Fragment in question that isn't working is my MapFragment.
Firstly ill show the code where I commit the transactions.
```
public class MainActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
MapFragment mapFrag = (MapFragment) fragmentManager.findFragmentById(R.id.map);
SelectorFragment selectorFrag = (SelectorFragment) fragmentManager.findFragmentById(R.id.selector);
if (mapFrag == null)
{
//Create the Fragment Object
mapFrag = new MapFragment();
//Queues the operation to attach a Fragment
//Tells it where to add and what to add.
//Commit makes it happen
fragmentManager.beginTransaction().add(R.id.map, mapFrag).commit();
}
if (selectorFrag == null)
{
//Create the Fragment Object
selectorFrag = new SelectorFragment();
//Queues the operation to attach a Fragment
//Tells it where to add and what to add.
//Commit makes it happen
fragmentManager.beginTransaction().add(R.id.selector, selectorFrag).commit();
}
}
}
```
So the Selector Fragment displays fine except for being Vertical.
Now ill post my main xml associated with the above Activity.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<FrameLayout
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/selector"
/>
<FrameLayout
android:id="@+id/selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/map"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
So the Map should be constrained to the top and take up 3/4 of the screen.
Now Ill show the Map Fragment Code followed by the xml for the Map Fragment.
```
public class MapFragment extends Fragment {
//Private Classfields
private MapData map;
private MyAdapter adapter;
@override
public View onCreateView(LayoutInflater inflater, ViewGroup ui, Bundle bundle)
{
//Takes a layout reference, instantiates all View objects.
//Reads the XML and creates the UI Based on it.
//Returns the root View object.
View view = inflater.inflate(R.layout.fragment_map, ui, false);
//Setup any event handlers below.
//Setting up RecyclerView
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.mapRecyclerView);
//Specify how it should be laid out.
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), MapData.HEIGHT, GridLayoutManager.HORIZONTAL, false));
//Have your data ready.
map = MapData.getInstance();
//Create your adapter
adapter = new MyAdapter(map);
//Attach Adapter to Recycler View.
recyclerView.setAdapter(adapter);
return view;
}
//Nested ViewHolder inside Fragment
private class MyDataVHolder extends RecyclerView.ViewHolder
{
private ImageView topLeftImageView, topRightImageView, bottomLeftImageView, bottomRightImageView, mainImageView;
public MyDataVHolder(LayoutInflater li, ViewGroup parent)
{
super(li.inflate(R.layout.grid_cell, parent, false));
//Divide the RecyclerViews runtime height by the number of cells
//that must fit in that space(MapData.HEIGHT)
//Add 1 to account for rounding errors that might leave a 1 pixel gap.
//Size value becomes the width and the height of the grid cell.
int size = parent.getMeasuredHeight()/MapData.HEIGHT + 1;
ViewGroup.LayoutParams lp = itemView.getLayoutParams();
lp.width = size;
lp.height = size;
//Grab UI Elements
//textView = (TextView) itemView.findViewById(R.id.list_data);
topLeftImageView = (ImageView) itemView.findViewById(R.id.topLeftImageView);
topRightImageView = (ImageView) itemView.findViewById(R.id.topRightImageView);
bottomLeftImageView = (ImageView) itemView.findViewById(R.id.bottomLeftImageView);
bottomRightImageView = (ImageView) itemView.findViewById(R.id.bottomRightImageView);
mainImageView = (ImageView) itemView.findViewById(R.id.mainImageView);
}
public void bind(MapElement mapElement)
{
//Set the image views.
//ImageView iv = ...;
//MapElement me = ...;
//iv.setImageResource(me.getNorthWest());
if (mapElement == null)
{
System.out.println("Map Element is null.");
}
else
{
System.out.println("Map Element is not null.");
}
topLeftImageView.setImageResource(mapElement.getNorthWest());
topRightImageView.setImageResource(mapElement.getNorthEast());
bottomLeftImageView.setImageResource(mapElement.getSouthWest());
bottomRightImageView.setImageResource(mapElement.getSouthEast());
if (mapElement.getStructure() != null)
{
mainImageView.setImageResource(mapElement.getStructure().getDrawableId());
}
}
}
private class MyAdapter extends RecyclerView.Adapter<MyDataVHolder>
{
private MapData map;
public MyAdapter(MapData inMap)
{
map = inMap;
}
@NonNull
@override
public MyDataVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater li = LayoutInflater.from(getActivity()); //Fragment Method
return new MyDataVHolder(li, parent);
}
@override
public void onBindViewHolder(@NonNull MyDataVHolder holder, int position) {
int row = position % MapData.HEIGHT;
int column = position / MapData.HEIGHT;
MapElement currentMapElement = map.get(row, column);
holder.bind(currentMapElement);
}
@override
public int getItemCount() {
return map.HEIGHT * map.WIDTH;
}
}
private void updateUI(int pos)
{
adapter.notifyItemChanged(pos);
}
}
```
No here is the Map XML containing a singular RecyclerView.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapRecyclerView"
/>
```
Here is the grid cell xml that the Fragment inflates.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/topLeftImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/topRightImageView"
app:layout_constraintBottom_toTopOf="@id/bottomLeftImageView"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/topRightImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toRightOf="@id/topLeftImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomRightImageView"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/bottomLeftImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/topLeftImageView"
app:layout_constraintRight_toLeftOf="@id/bottomRightImageView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/bottomRightImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toRightOf="@id/bottomLeftImageView"
app:layout_constraintTop_toBottomOf="@id/topRightImageView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/mainImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
Not sure why this isn't working and guessing it must be something fiddly that me and my peers are missing.
Cheers, if there is any other code you would like for context ill be hanging around.
Thanks for the help in advance.
The Fragment in question that isn't working is my MapFragment.
Firstly ill show the code where I commit the transactions.
```
public class MainActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
MapFragment mapFrag = (MapFragment) fragmentManager.findFragmentById(R.id.map);
SelectorFragment selectorFrag = (SelectorFragment) fragmentManager.findFragmentById(R.id.selector);
if (mapFrag == null)
{
//Create the Fragment Object
mapFrag = new MapFragment();
//Queues the operation to attach a Fragment
//Tells it where to add and what to add.
//Commit makes it happen
fragmentManager.beginTransaction().add(R.id.map, mapFrag).commit();
}
if (selectorFrag == null)
{
//Create the Fragment Object
selectorFrag = new SelectorFragment();
//Queues the operation to attach a Fragment
//Tells it where to add and what to add.
//Commit makes it happen
fragmentManager.beginTransaction().add(R.id.selector, selectorFrag).commit();
}
}
}
```
So the Selector Fragment displays fine except for being Vertical.
Now ill post my main xml associated with the above Activity.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<FrameLayout
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/selector"
/>
<FrameLayout
android:id="@+id/selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/map"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
So the Map should be constrained to the top and take up 3/4 of the screen.
Now Ill show the Map Fragment Code followed by the xml for the Map Fragment.
```
public class MapFragment extends Fragment {
//Private Classfields
private MapData map;
private MyAdapter adapter;
@override
public View onCreateView(LayoutInflater inflater, ViewGroup ui, Bundle bundle)
{
//Takes a layout reference, instantiates all View objects.
//Reads the XML and creates the UI Based on it.
//Returns the root View object.
View view = inflater.inflate(R.layout.fragment_map, ui, false);
//Setup any event handlers below.
//Setting up RecyclerView
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.mapRecyclerView);
//Specify how it should be laid out.
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), MapData.HEIGHT, GridLayoutManager.HORIZONTAL, false));
//Have your data ready.
map = MapData.getInstance();
//Create your adapter
adapter = new MyAdapter(map);
//Attach Adapter to Recycler View.
recyclerView.setAdapter(adapter);
return view;
}
//Nested ViewHolder inside Fragment
private class MyDataVHolder extends RecyclerView.ViewHolder
{
private ImageView topLeftImageView, topRightImageView, bottomLeftImageView, bottomRightImageView, mainImageView;
public MyDataVHolder(LayoutInflater li, ViewGroup parent)
{
super(li.inflate(R.layout.grid_cell, parent, false));
//Divide the RecyclerViews runtime height by the number of cells
//that must fit in that space(MapData.HEIGHT)
//Add 1 to account for rounding errors that might leave a 1 pixel gap.
//Size value becomes the width and the height of the grid cell.
int size = parent.getMeasuredHeight()/MapData.HEIGHT + 1;
ViewGroup.LayoutParams lp = itemView.getLayoutParams();
lp.width = size;
lp.height = size;
//Grab UI Elements
//textView = (TextView) itemView.findViewById(R.id.list_data);
topLeftImageView = (ImageView) itemView.findViewById(R.id.topLeftImageView);
topRightImageView = (ImageView) itemView.findViewById(R.id.topRightImageView);
bottomLeftImageView = (ImageView) itemView.findViewById(R.id.bottomLeftImageView);
bottomRightImageView = (ImageView) itemView.findViewById(R.id.bottomRightImageView);
mainImageView = (ImageView) itemView.findViewById(R.id.mainImageView);
}
public void bind(MapElement mapElement)
{
//Set the image views.
//ImageView iv = ...;
//MapElement me = ...;
//iv.setImageResource(me.getNorthWest());
if (mapElement == null)
{
System.out.println("Map Element is null.");
}
else
{
System.out.println("Map Element is not null.");
}
topLeftImageView.setImageResource(mapElement.getNorthWest());
topRightImageView.setImageResource(mapElement.getNorthEast());
bottomLeftImageView.setImageResource(mapElement.getSouthWest());
bottomRightImageView.setImageResource(mapElement.getSouthEast());
if (mapElement.getStructure() != null)
{
mainImageView.setImageResource(mapElement.getStructure().getDrawableId());
}
}
}
private class MyAdapter extends RecyclerView.Adapter<MyDataVHolder>
{
private MapData map;
public MyAdapter(MapData inMap)
{
map = inMap;
}
@NonNull
@override
public MyDataVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater li = LayoutInflater.from(getActivity()); //Fragment Method
return new MyDataVHolder(li, parent);
}
@override
public void onBindViewHolder(@NonNull MyDataVHolder holder, int position) {
int row = position % MapData.HEIGHT;
int column = position / MapData.HEIGHT;
MapElement currentMapElement = map.get(row, column);
holder.bind(currentMapElement);
}
@override
public int getItemCount() {
return map.HEIGHT * map.WIDTH;
}
}
private void updateUI(int pos)
{
adapter.notifyItemChanged(pos);
}
}
```
No here is the Map XML containing a singular RecyclerView.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapRecyclerView"
/>
```
Here is the grid cell xml that the Fragment inflates.
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/topLeftImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/topRightImageView"
app:layout_constraintBottom_toTopOf="@id/bottomLeftImageView"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/topRightImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toRightOf="@id/topLeftImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomRightImageView"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/bottomLeftImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/topLeftImageView"
app:layout_constraintRight_toLeftOf="@id/bottomRightImageView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/bottomRightImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toRightOf="@id/bottomLeftImageView"
app:layout_constraintTop_toBottomOf="@id/topRightImageView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
/>
<ImageView
android:id="@+id/mainImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
Not sure why this isn't working and guessing it must be something fiddly that me and my peers are missing.
Cheers, if there is any other code you would like for context ill be hanging around.
Thanks for the help in advance.