PagerView的一个子页面里的ListView显示不出来

Android初学者,最近的一个仿QQ的作业,看了网上的做法用PagerView做了个类似QQ那种底部标签栏可以切换页面的样式,然后我在其中一个页面里加了个ListView,在ListView里面放了几项数据,但是显示不出来,困扰了我好久,网上找了好久,就是解决不了,求大神帮忙看看,谢谢谢谢!!!
把listview单独出来运行可以显示,或者在listview所在的布局里添加其他类型的组件也可以显示,维度listview不显示

这是实现三个Tab切换页面的Java代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Top_bottom extends Activity implements android.view.View.OnClickListener {
    private ViewPager mViewPager;// 用来放置界面切换
    private PagerAdapter mPagerAdapter;// 初始化View适配器
    private List<View> mViews = new ArrayList<View>();// 用来存放Tab01-03
    // 三个Tab,每个Tab包含一个按钮
    private LinearLayout mTabMessage;
    private LinearLayout mTabContacts;
    private LinearLayout mTabDynamic;
    // 三个按钮
    private ImageButton mMessageImg;
    private ImageButton mContactsImg;
    private ImageButton mDynamicImg;
    //顶部标题
    private TextView textView;
    //底部标签
    private TextView textView_message;
    private TextView textView_contacts;
    private TextView textView_dynamic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top_bottom);
        textView=(TextView)findViewById(R.id.top_textview);
        textView_message=(TextView)findViewById(R.id.textView_message);
        textView_contacts=(TextView)findViewById(R.id.textView_contacts);
        textView_dynamic=(TextView)findViewById(R.id.textView_dynamic);
        initView();
        initViewPage();
        initEvent();
    }

    private void initEvent() {
        mTabMessage.setOnClickListener(this);
        mTabContacts.setOnClickListener(this);
        mTabDynamic.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *ViewPage左右滑动时
             */
            @Override
            public void onPageSelected(int arg0) {
                int currentItem = mViewPager.getCurrentItem();
                switch (currentItem) {
                    case 0:
                        textView.setText("消息");
                        resetImg();
                        mMessageImg.setBackground(getResources().getDrawable(R.drawable.messages,null));
                        textView_message.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        break;
                    case 1:
                        textView.setText("联系人");
                        resetImg();
                        mContactsImg.setBackground(getResources().getDrawable(R.drawable.contactss,null));
                        textView_contacts.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        break;
                    case 2:
                        textView.setText("动态");
                        resetImg();
                        mDynamicImg.setBackground(getResources().getDrawable(R.drawable.qzones,null));
                        textView_dynamic.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 初始化设置
     */
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        // 初始化三个LinearLayout
        mTabMessage = (LinearLayout) findViewById(R.id.tab_message);
        mTabContacts = (LinearLayout) findViewById(R.id.tab_contacts);
        mTabDynamic = (LinearLayout) findViewById(R.id.tab_dynamic);
        // 初始化三个按钮
        mMessageImg = (ImageButton) findViewById(R.id.tab_message_img);
        mContactsImg = (ImageButton) findViewById(R.id.tab_contacts_img);
        mDynamicImg = (ImageButton) findViewById(R.id.tab_dynamic_img);
    }

    /**
     * 初始化ViewPage
     */
    private void initViewPage() {

        // 初始化三个布局
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View message = mLayoutInflater.inflate(R.layout.message, null);
        View contacts = mLayoutInflater.inflate(R.layout.contacts, null);
        View dynamic = mLayoutInflater.inflate(R.layout.dynamic,null);

        mViews.add(message);
        mViews.add(contacts);
        mViews.add(dynamic);

        // 适配器初始化并设置
        mPagerAdapter = new PagerAdapter() {

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

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViews.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return mViews.size();
            }
        };
        mViewPager.setAdapter(mPagerAdapter);
    }

    /**
     * 判断哪个要显示,及设置按钮图片
     */
    @Override
    public void onClick(View arg0) {

        switch (arg0.getId()) {
            case R.id.tab_message:
                textView.setText("消息");
                mViewPager.setCurrentItem(0);
                resetImg();
                mMessageImg.setBackground(getResources().getDrawable(R.drawable.messages,null));
                textView_message.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            case R.id.tab_contacts:
                textView.setText("联系人");
                mViewPager.setCurrentItem(1);
                resetImg();
                mContactsImg.setBackground(getResources().getDrawable(R.drawable.contactss,null));
                textView_contacts.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            case R.id.tab_dynamic:
                textView.setText("动态");
                mViewPager.setCurrentItem(2);
                resetImg();
                mDynamicImg.setBackground(getResources().getDrawable(R.drawable.qzones,null));
                textView_dynamic.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            default:
                break;
        }
    }

    /**
     * 把所有图片变暗
     */
    private void resetImg() {
        mMessageImg.setBackground(getResources().getDrawable(R.drawable.message,null));
        mContactsImg.setBackground(getResources().getDrawable(R.drawable.contacts,null));
        mDynamicImg.setBackground(getResources().getDrawable(R.drawable.qzone,null));
        textView_message.setTextColor(getResources().getColor(R.color.colorfont));
        textView_contacts.setTextColor(getResources().getColor(R.color.colorfont));
        textView_dynamic.setTextColor(getResources().getColor(R.color.colorfont));
    }

    public void onBackPressed() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        startActivity(intent);
    }
}
<?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">

    <include layout="@layout/activity_top"></include>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <include layout="@layout/activity_bottom"></include>
</LinearLayout>

这是实现ListView的Java代码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dynamic extends AppCompatActivity {
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic);
        listView=(ListView)findViewById(R.id.listview_dynamic);
        String title[]={"好友动态","退出QQ"};
        int img[]={R.drawable.qqzone,R.drawable.exit};
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>(); //List集合用来封装数据
        for (int x=0;x<title.length;x++){
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("title",getResources().getStringArray(R.array.title)[x]);
            map.put("img",img[x]);
            list.add(map);       //将封装后的每个Item的数据添加到List集合中
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(Dynamic.this,list,R.layout.dynamic_style,
                new String[]{"title","img"},new int[]{R.id.textView21,R.id.imageView2});
        listView.setAdapter(simpleAdapter);
    }

}

2个回答

你的实现listView的代码在哪里被调用了???

weixin_44786406
卖女孩的小火柴z 然后下面再把这个view的对象add到viewpager的对象里面
9 个月之前 回复
weixin_44786406
卖女孩的小火柴z 在我贴的第一个代码块里的// 初始化三个布局 LayoutInflater mLayoutInflater = LayoutInflater.from(this); View message = mLayoutInflater.inflate(R.layout.message, null); View contacts = mLayoutInflater.inflate(R.layout.contacts, null); View dynamic = mLayoutInflater.inflate(R.layout.dynamic,null);,最后这一句就是
9 个月之前 回复

问题已解决!!!琢磨出来了

weixin_40292154
不争亦不屑 我现在做项目的时候也遇到你所述的问题,同样困扰了很久,你对此问题已解决了,还望你指点一二
大约一个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问