2017-10-25 3 views
0

Ich habe die activity_main.xml mit einer FrameLayout relativ zu einer ListViewFragment. Ich möchte dies fragment in diesem activity zeigen. Aber es funktioniert nicht ListView wird nicht angezeigt. Weißt du, wo das Problem ist?Listenansicht in der ListViewFragment wird nicht in der Aktivität

Haupttätigkeit 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> 

Haupttätigkeit:

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(); 

    } 
} 

Listview 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; 
    } 
} 

Antwort

2

In Höhe und Breite der framelayout.

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

Auch Ihr Listview Fragment kann nicht sein, Listenansicht als Eltern es mit einem Layout wie Frame-Layout wickelt 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

Danke, aber die Listview erscheint auch nicht. – JonD

+0

Aktualisierte Antwort. Überprüfen Sie es. – Anonymous

Verwandte Themen