如何解决ListView 没有出现在片段中
我正在尝试为片段中的 ListView 使用数组适配器,但出于某种原因,每当我运行它时,它根本不显示。我不确定我哪里出错了。真的很新,所以可能有很多错误的空间。
这是片段的代码:
public class FirstFragment extends Fragment {
ArrayList<String> arrayList=new ArrayList<>();
@Override
public View onCreateView(
LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState
) {
View firstLayOut = inflater.inflate(R.layout.fragment_first,container,false);
ListView listView=firstLayOut.findViewById(R.id.listview);
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("cat");
arrayList.add("dog");
arrayList.add("egg");
arrayList.add("f");
arrayList.add("g");
arrayList.add("h");
arrayList.add("k");
arrayList.add("l");
arrayList.add("m");
ArrayAdapter adapter1=new ArrayAdapter(firstLayOut.getContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(adapter1);
return firstLayOut;
}
public void onViewCreated(@NonNull View view,Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
}
}
这里是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@color/BackgroundLight"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="fill_parent">
</ListView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/BackgroundGray"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To frag2"
app:backgroundTint="@color/buttonBlueL" />
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
以及主要活动的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
}
和xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@color/BackgroundLight"
tools:context=".MainActivity">
<include
layout="@layout/fragment_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
请告诉我我做错了什么。
解决方法
实际上你只是在主xml中包含fragment_first布局,而不是在主xml中包含fragment_first,在那个位置添加一个容器,然后用你的片段替换这个容器,你的主xml看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@color/BackgroundLight"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_container" />
</RelativeLayout>
之后将此 main_container 替换为您的片段,如下所示
public class MainActivity extends AppCompatActivity {
Fragment FirstFragment;
FragmentManager fragmentManager;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting fragment manager and begin transaction and finally replace your main_container to fragment
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new FirstFragment(),"First Fragment").commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。