java – ListView中的ViewPagers显示空白列表项

前端之家收集整理的这篇文章主要介绍了java – ListView中的ViewPagers显示空白列表项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
[编辑]:谢谢你们所有有意义的答案,现在问题解决了,谢谢你的帮助.类似的问题: Android: I am unable to have ViewPager WRAP_CONTENT

我正在尝试实现我的应用程序的UI:我希望每行都有一个带ViewPager的ListView.

这是我的文件

MainActivity.java

package com.condi;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setListAdapter(new CardListAdapter(this,getFragmentManager()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
}

CardListAdapter.java

package com.condi;

import java.util.List;

import android.app.FragmentManager;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class CardListAdapter extends BaseAdapter {

    private Context context;
    private FragmentManager fragmentManager;
    private List<Profile> profiles;

    CardListAdapter(Context context,FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
        this.profiles = new DatabaseHelper(context).getProfiles();
    }

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

    @Override
    public Profile getItem(int position) {
        return profiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        convertView = View.inflate(context,R.layout.profile_card,null);
        CardPagerAdapter mCardPagerAdapter = new CardPagerAdapter(
                fragmentManager);
        ViewPager viewPager = (ViewPager) convertView.findViewById(R.id.pager);
        viewPager.setAdapter(mCardPagerAdapter);
        viewPager.setId(R.id.pager);
        return convertView;
    }
}

profile_card.xml(问题来自wrap_content).

<?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="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="4dp"
        android:background="@drawable/card_background"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</FrameLayout>

CardPagerAdapter.java

package com.condi;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;

public class CardPagerAdapter extends FragmentPagerAdapter {

    public CardPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new Fragment1();
        case 1:
            return new Fragment2();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

Fragment1.java

package com.condi;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    public Fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.preference_category,container,false);
    }
}

Fragment2.java

package com.condi;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

    public Fragment2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,false);
    }
}

提供配置文件数据库运行良好.
问题是我得到一个空列表,其中包含正确的项目数,但根本没有显示ViewPager. screenshot of my app

我究竟做错了什么 ?

解决方法

尝试以xml更改ViewPager的高度. wrap_content不起作用.

猜你在找的Java相关文章