2017-10-25 17 views
0

activity_main.xmlListViewFragmentを基準にして1つがFrameLayoutです。このfragmentをこのactivityに表示します。しかし、それが動作していないListViewは表示されません。問題はどこにあるのか知っていますか?ListViewFragmentのリストビューがアクティビティに表示されません

メインの活動のxml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.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"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_list" 
/> 
</android.support.constraint.ConstraintLayout> 

主な活動:

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // here Im trying to add the fragment A to the activity but the listview is not appearing 
     getSupportFragmentManager(). 
       beginTransaction().add(R.id.fragment_list,new ListViewFragment()).commit(); 

    } 
} 

リストビューのxml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/listView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
/> 

ListViewFragment:

public class ListViewFragment extends Fragment { 
    private ListView editor=null; 
    ArrayList<String> items = new ArrayList<>(); 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View result=inflater.inflate(R.layout.fragment_list, container, false); 
     editor=(ListView) (result.findViewById(R.id.listView)); 
     ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items); 
     editor.setAdapter(arrayAdapter); 
     return result; 
    } 
} 

答えて

2

でframeLayoutに高さと幅を追加します。

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fragment_list" /> 

また、あなたのListViewフラグメントは、フレームレイアウトのようなレイアウトでそれを包むlayout.So親としてリストビューを持つことができません。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</FrameLayout> 
+0

ありがとうございますが、リストビューも表示されません。 – JonD

+0

更新済みです。確認してください。 – Anonymous

関連する問題