Java 碎片不是';t显示RecyclerView或任何元素
我试着在我的屏幕上显示两个片段,目前只有一个片段出现,它是垂直的,而不是水平的,我认为这是因为我的第二个片段没有出现。 正在讨论的不起作用的片段是我的MapFragmentJava 碎片不是';t显示RecyclerView或任何元素,java,xml,android-studio,android-fragments,android-recyclerview,Java,Xml,Android Studio,Android Fragments,Android Recyclerview,我试着在我的屏幕上显示两个片段,目前只有一个片段出现,它是垂直的,而不是水平的,我认为这是因为我的第二个片段没有出现。 正在讨论的不起作用的片段是我的MapFragment public class MapFragment extends Fragment { //Private Classfields private MapData map; private MyAdapter adapter; @Override public View onCre
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);
}
}
首先,我将显示提交事务的代码
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();
}
}
}
因此,选择器片段显示良好,除了垂直
现在我将发布与上述活动相关的主要xml
<?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>
因此,地图应该限制在顶部,占据屏幕的3/4
现在,我将显示映射片段代码,后跟映射片段的xml
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);
}
}
公共类MapFragment扩展了片段{
//私有类字段
私有地图数据地图;
专用MyAdapter适配器;
@凌驾
创建视图上的公共视图(布局、充气机、视图组用户界面、捆绑包)
{
//获取布局引用,实例化所有视图对象。
//读取XML并基于它创建UI。
//返回根视图对象。
视图=充气机。充气(R.layout.fragment\u map,ui,false);
//在下面设置任何事件处理程序。
//设置RecyclerView
RecycleView RecycleView=(RecycleView)view.findViewById(R.id.MapRecycleView);
//指定其布局方式。
setLayoutManager(新的GridLayoutManager(getActivity(),MapData.HEIGHT,GridLayoutManager.HORIZONTAL,false));
//准备好你的数据。
map=MapData.getInstance();
//创建适配器
适配器=新的MyAdapter(映射);
//将适配器连接到回收器视图。
recyclerView.setAdapter(适配器);
返回视图;
}
//片段内部嵌套的ViewHolder
私有类MyDataVHolder扩展了RecyclerView.ViewHolder
{
private ImageView topLeftImageView、topRightImageView、bottomLeftImageView、bottomRightImageView、mainImageView;
公共MyDataVHolder(Layoutiner li,视图组父级)
{
super(li.inflate(R.layout.grid_单元格,父单元格,false));
//将RecyclerViews运行时高度除以单元格数
//必须适合该空间(MapData.HEIGHT)
//添加1以说明舍入误差可能会留下1像素的间距。
//“大小”值将成为栅格单元的宽度和高度。
int size=parent.getMeasuredHeight()/MapData.HEIGHT+1;
ViewGroup.LayoutParams lp=itemView.getLayoutParams();
lp.宽度=尺寸;
lp.高度=尺寸;
//抓取UI元素
//textView=(textView)itemView.findViewById(R.id.list\u数据);
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);
}
公共void绑定(MapElement MapElement)
{
//设置图像视图。
//ImageView iv=。。。;
//MapElement me=。。。;
//iv.setImageResource(me.getNorthWest());
如果(mapElement==null)
{
System.out.println(“Map元素为null”);
}
其他的
{
System.out.println(“映射元素不为null”);
}
setImageResource(mapElement.getNorthWest());
setImageResource(mapElement.getNorthEast());
setImageResource(mapElement.getSouthWest());
bottomRightImageView.setImageResource(mapElement.getSouthwest());
if(mapElement.getStructure()!=null)
{
mainImageView.setImageResource(mapElement.getStructure().getDrawableId());
}
}
}
私有类MyAdapter扩展了RecyclerView.Adapter
{
私有地图数据地图;
公共MyAdapter(MapData inMap)
{
map=inMap;
}
@非空
@凌驾
公共MyDataVHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
LayoutInflater li=LayoutInflater.from(getActivity());//片段方法
返回新的MyDataVHolder(li,父级);
}
@凌驾
public void onBindViewHolder(@NonNull MyDataVHolder,int位置){
int row=位置%MapData.HEIGHT;
int column=位置/MapData.HEIGHT;
MapElement currentMapElement=map.get(行、列);
holder.bind(currentMapElement);
}
@凌驾
public int getItemCount(){
返回map.HEIGHT*map.WIDTH;
}
}
私有无效更新(int pos)
{
适配器已更改(pos);
}
}
不,这是包含单个RecyclerView的映射XML
<?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"
/>
下面是片段膨胀的网格单元xml
<?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>
我不知道为什么这不起作用,猜测这一定是我和我的同龄人所缺少的东西
干杯,如果有任何其他代码,你想为上下文我会挂在周围
提前谢谢你的帮助