ListView 未显示在片段中
ListView not showing up in fragement
我正在尝试为片段中的 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>
和主要代码 activity:
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>
请让我知道我做错了什么。
实际上,您只是将 fragment_first 布局包含到主 xml,而不是将 fragment_first 包含在主 xml 中,将容器添加到该位置,然后替换这个容器和你的片段,你的主要 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;
}
}
我正在尝试为片段中的 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>
和主要代码 activity:
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>
请让我知道我做错了什么。
实际上,您只是将 fragment_first 布局包含到主 xml,而不是将 fragment_first 包含在主 xml 中,将容器添加到该位置,然后替换这个容器和你的片段,你的主要 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;
}
}