在片段 android 中创建列表视图

Creating listview in fragment android

首先感谢您的回答。我是 android 应用程序开发的新手。我制作了一个名为 fragment_list 的片段来创建列表视图 dynamically.The 问题是当应用程序运行时屏幕仍然存在 blank.I 已尝试制作一个包含图像和 TextView 的列表视图。我还没有实现 setOnItemClickListener()。我在 Main_Activity.java 中使用 Fragment Transaction 添加 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>

经过一点努力,我解决了这个问题,结果如下。添加代码可能会帮助其他可能遇到类似情况的人 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>