ViewPager 嵌套 fragment 中无法显示fragment中的listview内容

我在viewpager中用fragment作为加载页,然后在fragment中嵌入了一个listview,单独测试时fragment能载入listview内容,但是在viewpager中加载fragment时,无法显示listview内容,fragment其他组件能显示
viewpager 代码:
import java.util.ArrayList;
import java.util.List;

import com.nineteen.myandroid.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AppUninstall extends FragmentActivity {
ViewPager viewPager;
// 内容页
List views = new ArrayList();
// 标题页
List titles = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.app_uninstall);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.id.appun_person, null);
    views.add(view);
    titles.add("个人应用");
    view = inflater.inflate(R.layout.test_relatvelayout, null);
    views.add(view);
    titles.add("系统预装");
    view = inflater.inflate(R.layout.test_button, null);
    views.add(view);
    titles.add("猜你不喜欢");
    viewPager.setAdapter(new MyPagerAdapter(views, titles));
}

class MyPagerAdapter extends PagerAdapter {
    List<View> views;
    List<String> titles;

    public MyPagerAdapter(List<View> views, List<String> titles) {
        this.views = views;
        this.titles = titles;
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    // 获取到标题
    public CharSequence getPageTitle(int position) {
        return titles.get(position);

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(views.get(position));
        return views.get(position);

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

}

}

viewpage XML: android:id="@+id/appun_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

fragment代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.nineteen.myandroid.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PersonApp extends FragmentActivity {
List> list = new ArrayList>();

ListView listView;
int[] icon = { R.drawable.a005, R.drawable.a006, R.drawable.a007,
        R.drawable.a008, R.drawable.a009, R.drawable.a010, R.drawable.a011,
        R.drawable.a012, R.drawable.a013, R.drawable.a014, R.drawable.a015,
        R.drawable.a016, R.drawable.a017, R.drawable.a018, R.drawable.a019 };
String[] name = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
        "12", "13", "14", "14" };
float[] size = { 1.10f, 1.10f, 1.1f, 1.1f, 1.1f, 1.1f, 1.1f, 1.1f, 1.1f,
        1.1f, 1.1f, 1.1f, 1.1f, 1.1f, 2.0f };

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.app_uninstall_listview);
    listView = (ListView) findViewById(R.id.app_uninstall_listview);
    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

    for (int i = 0; i < icon.length; i++) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("icon", icon[i]);
        map.put("name", name[i]);
        map.put("size", size[i]);
        list.add(map);

        SimpleAdapter adapter = new SimpleAdapter(this, list,
                R.layout.app_uninstallitem, new String[] { "icon", "name",
                        "size" }, new int[] { R.id.appun_imageview,
                        R.id.appun_nameview, R.id.app_unitem_size });
        listView.setAdapter(adapter);
    }

}

}

fragment XML: android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="2dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="手机内存" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:text="已用" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.31" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GB" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="," />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剩余" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.15" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GB" />
    </LinearLayout>

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:max="100"
        android:progress="52" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="2dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="SD卡" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="104dp"
            android:text="已用" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.31" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GB" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="," />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剩余" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.15" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GB" />
    </LinearLayout>

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:max="100"
        android:progress="52" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:padding="3dp"
        android:text="最近使用" />

    <ListView
        android:id="@+id/app_uninstall_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>

1个回答

需要重新创建Adapter

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问