0

Прежде всего, спасибо за ответ. Я новичок в разработке приложений для Android. Я сделал фрагмент с именем fragment_list для динамического создания списка. Проблема в том, что при запуске приложения экран остается пустым. Я попытался создать список, в котором есть изображение и TextView. Мне еще предстоит реализовать setOnItemClickListener(). Я использовал Fragment Transaction в Main_Activity.java, чтобы добавить Fragment.Here файлы.Создание списка в фрагменте андроида

main_layout.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 

    android:id="@+id/groups" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

</LinearLayout> 

Main_Activity.java

package com.cloudplay.spotvideo; 

import android.app.Activity; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 

public class Main_Activity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    Fragment_List f1 = new Fragment_List(); 
    FragmentManager manager = getFragmentManager(); 

    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.add(R.id.groups, f1, "MAIN_LIST"); 
    transaction.addToBackStack("MAIN-LIST"); 
    transaction.commit(); 

} 

} 

fragment_list.xml

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


</ListView> 

Fragment_List.java

package com.cloudplay.spotvideo; 

import android.os.Bundle; 
import android.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 

public class Fragment_List extends ListFragment implements 
    AdapterView.OnItemClickListener { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    return inflater.inflate(R.layout.fragment_list, container, false); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 

    ArrayAdapter adapter = new ArrayAdapter(getActivity(), 
      R.layout.single_row, R.array.MainList); 
    setListAdapter(adapter); 
    getListView().setOnItemClickListener(this); 

} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 

} 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.cloudplay.spotvideo" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.cloudplay.spotvideo.Main_Activity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

single_row.xml

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

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="52dp" 
    android:text="" /> 

</LinearLayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<string name="app_name">SpotVideo</string> 
<string name="action_settings">Settings</string> 
<string name="hello_world">Hello world!</string> 

<string-array name="MainList"> 
    <item name="movies">Movies</item> 
    <item name="music">Music</item> 
</string-array> 
<string-array name="Film"> 
    <item name="hollywood">Hollywood</item> 
    <item name="bollywood">Bollywood</item> 
    <item name="tollywood">Tollywood</item> 
</string-array> 

</resources> 

ответ

1

После немного усилий мне удалось взломать эту проблему и здесь есть исход. Добавление кода, который может помочь другим, которые могут попасть в аналогичную ситуацию, например me.There много фрагментов, но я добавляю только код для фрагмента MainList, который является первым списком, который появляется при открытии приложения.

Main_Activity.java

package com.abc.def; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class Main_Activity extends Activity { 
FragmentManager manager; 
Fragment f10; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    manager = getFragmentManager(); 
    Add_MainList(getCurrentFocus()); 

} 

//Fragment Changer fuctions start 
public void Changer(int position) { 
    switch (position) { 
    case 0: 
     f10 = new Movie_List(); 
     Fragment_Change(getCurrentFocus()); 
     break; 
    case 1: 
     f10 = new Music_List(); 
     Fragment_Change(getCurrentFocus()); 
     break; 
    case 2: 
     f10 = new Documentary_List(); 
     Fragment_Change(getCurrentFocus()); 
     break; 
    case 3: 
     f10 = new Cartoons_List(); 
     Fragment_Change(getCurrentFocus()); 
     break; 
    case 4: 
     f10 = new DailySoaps_List(); 
     Fragment_Change(getCurrentFocus()); 
     break; 

    default: 
     break; 
    } 
} 

public void Fragment_Change(View v) { 
    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.addToBackStack("MAINLIST"); 
    transaction.replace(R.id.groups, f10, "MUSICLIST"); 
    transaction.commit(); 
} 


public void Add_MainList(View v) { 
    Main_List f1 = new Main_List(); 
    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.add(R.id.groups, f1, "MAINLIST"); 
    transaction.commit(); 
} 


//Replacing Main List With Fragment Ended 

//Fragment Changer For Movie Starts 

public void ChangerMovie(int position) { 
    switch (position) { 
    case 0: 
     // Replace_MainList_with_MovieList(getCurrentFocus()); 
     break; 
    case 1: 
     Replace_Movie_List_with_Movie_Grid(getCurrentFocus()); 
     break; 
    case 2: 
     // Replace_MainList_with_DocumentaryList(getCurrentFocus()); 
     break; 
    default: 
     break; 

    } 
} 

//Fragment Changer for Music starts 
public void ChangerMusic(int position){ 
    switch(position) { 
    case 0: 
     break; 
    case 1: 
     break; 
    case 2: 
     break; 
    default: 
     break; 
    } 
} 

public void Replace_Movie_List_with_Movie_Grid(View v) { 
    Bollywood_Grid f7 = new Bollywood_Grid(); 
    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.addToBackStack("MOVIELIST"); 
    transaction.replace(R.id.groups, f7, "BOLLYGRID"); 
    transaction.commit(); 
} 

//Fragment Changer for Movie ends 

//Error handling; Removing Main list 
public void Remove_MainList(View v) { 
    Main_List f1 = (Main_List) manager.findFragmentByTag("MAINLIST"); 
    FragmentTransaction transaction = manager.beginTransaction(); 
    if (f1 != null) { 
     transaction.remove(f1); 
     transaction.commit(); 
    } else { 
     Toast.makeText(this, "Main List was not added so can't be removed", 
       Toast.LENGTH_LONG).show(); 
    } 

} 
} 

MainList.java

package com.abc.def; 

import android.app.ListFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class Main_List extends ListFragment { 

public Main_List() { 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    return inflater.inflate(R.layout.mainlist_layout, container, false); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 

    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), 
      R.array.Mainlist, android.R.layout.simple_list_item_1); 
    setListAdapter(adapter); 

} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    Configs.mainlist_position = position; 
    ((Main_Activity)getActivity()).Changer(position); 

} 

} 

mainlist_layout.xml

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

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#87CEFA"> 
</ListView> 



<TextView 
    android:id="@android:id/empty" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:layout_gravity="center|center_vertical"/> 

</LinearLayout> 
Смежные вопросы