【问题标题】:ListView not showing up in fragementListView 未显示在片段中
【发布时间】:2021-10-04 00:56:03
【问题描述】:

我正在尝试为片段中的 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>

请让我知道我做错了什么。

【问题讨论】:

    标签: java android listview android-fragments android-arrayadapter


    【解决方案1】:

    实际上你只是在你的主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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      相关资源
      最近更新 更多