Fragment中不显示数据?

忌酒c
  • 2
新手上路,请多包涵

想在fragment上的listview显示数据,但是只能显示个listview背景色

图片描述

导航栏的位置,应该显示底下那个新闻数据,但是空空如也。

mainactivity代码


public class MainActivity extends FragmentActivity {


    private TabLayout tab_title;
    private ViewPager vp_pager;

    private List<String> list_title;                                      //tab名称列表
    private List<View> listViews;

    private View newsView;                                                //定义新闻页面
    private View sportView;                                               //定义体育页面
    private View funView;                                                 //定义娱乐页面
    private View science;                                                 //定义科技界面

    private viewAdapter vAdapter;                                         //定义以view为切换的adapter

    private final static String TAG="MainActivity";


    private int[] tabImg;

    private ArrayList<NewsBean> newsBeans =null;

    private DrawerLayout drawerLayout;
    private RelativeLayout rightLayout;
    private ImageView leftMenu, rightMenu;
    private ListView mListView;
    private List<ContentModel> mList;

    private ArrayList<Data> datas = null;
    private FragmentManager fManager = null;
    private long exitTime = 0;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//
        //去掉title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

//        if (getSupportActionBar() != null){
//            getSupportActionBar().hide();
//        }
//        setContentView(R.layout.activity_main);
        setContentView(R.layout.main);
        mContext = this;



        //1.找到控件
        ListView lv_news =  findViewById(R.id.lv_news);
        System.out.println("找到lv_show");
        //2.获取新闻数据用list封装
        ArrayList<NewsBean> allNews = NewsUtils.getAllNews(mContext);
        NewsBean a = allNews.get(0);
        System.out.println("newsbean:++++"+a);
        //3.创建一个adapter设置给listview
        NewsAdapter newsAdapter = new NewsAdapter(mContext, allNews);
        lv_news.setAdapter(newsAdapter);


        //4.设置listview条目的点击事件
        //lv_news.setOnItemClickListener(this);

        //初始化
        initControls();
        viewChanage();

        initData();

        leftMenu = (ImageView) findViewById(R.id.leftmenu);
        rightMenu = (ImageView) findViewById(R.id.rightmenu);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        rightLayout = (RelativeLayout) findViewById(R.id.right);
        mListView = (ListView) findViewById(R.id.left_listview);


        System.out.println("找到了");
        ContentAdapter adapter = new ContentAdapter(this,mList);
        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                ContentModel contentModel = (ContentModel) parent.getItemAtPosition(position);
                int i = contentModel.getId();
                switch (i){
                    case 1:
                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
//                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
//                        startActivity(intent);
                        Toast.makeText(MainActivity.this,"正在进一步开发",Toast.LENGTH_LONG).show();
                        break;

                    case 3:
//                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
//                        startActivity(intent);
                        Toast.makeText(MainActivity.this,"正在进一步开发",Toast.LENGTH_LONG).show();
                        break;

                    case 4:
//                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
//                        startActivity(intent);
                        Toast.makeText(MainActivity.this,"正在进一步开发",Toast.LENGTH_LONG).show();
                        break;
                }

//                FragmentTransaction bt = fm.beginTransaction();
//                switch ((int) id) {
//                    case 1:
//                        //bt.replace(R.id.content, new NewsFragment());
//
//                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
//                        startActivity(intent);
//
//                        break;
//                    case 2:
//                        bt.replace(R.id.content, new SubscriptionFragment());
//                        break;
//
//                    default:
//                        break;
//                }
                //bt.commit();
                //drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });

        leftMenu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击了左边菜单",Toast.LENGTH_LONG).show();
                drawerLayout.openDrawer(Gravity.LEFT);
            }
        });
        rightMenu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(Gravity.RIGHT);
            }
        });
        rightLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                drawerLayout.closeDrawer(Gravity.RIGHT);
            }
        });

//        //1.获取Fragment的管理者
//        FragmentManager fragmentManager = getFragmentManager();
//        //2.开启事物
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        //3.动态替换
//        FragmentTransaction replace = transaction.replace(R.id.lv_tt, new NewsFragment());
//        //4.commit
//        transaction.commit();


    }




    /**
     * 初始化控件
     */
    private void initControls()
    {
        //title
        tab_title = (TabLayout)findViewById(R.id.tab_title);
        vp_pager = (ViewPager)findViewById(R.id.vp_pager);
        //initBottomNavigationBar();
        //bottom
        //为tabLayout上的图标赋值
        tabImg = new int[]{R.drawable.icon_three,R.drawable.icon_one,R.drawable.icon_two ,R.drawable.icon_four};
    }

    /**
     * 采用在viewpager中切换 view 的方式,并添加了icon的功能
     */
    private void viewChanage()
    {
        listViews = new ArrayList<>();
        LayoutInflater mInflater = getLayoutInflater();
        //title
        newsView = mInflater.inflate(R.layout.fragment_news, null);
        sportView = mInflater.inflate(R.layout.fragment_sports, null);
        funView = mInflater.inflate(R.layout.fragment_amusement, null);
        science = mInflater.inflate(R.layout.fragment_science,null);

        listViews.add(newsView);
        listViews.add(sportView);
        listViews.add(funView);
        listViews.add(science);


        list_title = new ArrayList<>();
        list_title.add("新闻");
        list_title.add("体育");
        list_title.add("娱乐");
        list_title.add("科技");

        //设置TabLayout的模式,这里主要是用来显示tab展示的情况的
        //TabLayout.MODE_FIXED          各tab平分整个工具栏,如果不设置,则默认就是这个值
        //TabLayout.MODE_SCROLLABLE;    // 适用于多tab的,也就是有滚动条的,一行显示不下这些tab可以用这个
        //                              当然了,你要是想做点特别的,像知乎里就使用的这种效果
        tab_title.setTabMode(TabLayout.MODE_FIXED); //绑定标题数据


//        tab_title.setTabMode(TabLayout.MODE_SCROLLABLE);
        //设置tablayout距离上下左右的距离
        //tab_title.setPadding(20,20,20,20);

        //为TabLayout添加tab名称
        tab_title.addTab(tab_title.newTab().setText(list_title.get(0)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(1)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(2)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(3)));

        vAdapter = new viewAdapter(this,listViews,list_title,tabImg);
        vp_pager.setAdapter(vAdapter);
        //将tabLayout与viewpager连起来
        tab_title.setupWithViewPager(vp_pager);
    }
    private void initData() {

        mList = new ArrayList<ContentModel>();
        mList.add(new ContentModel(R.drawable.icon_one, "新闻", 1));
        mList.add(new ContentModel(R.drawable.icon_two, "订阅", 2));
        mList.add(new ContentModel(R.drawable.icon_three, "图片", 3));
        mList.add(new ContentModel(R.drawable.icon_four, "视频", 4));
        mList.add(new ContentModel(R.drawable.icon_one, "跟帖", 5));
        mList.add(new ContentModel(R.drawable.icon_four, "投票", 6));
    }
}

Fragment部分:


public class NewsFragment extends Fragment {

    private ListView lv_news;
    private ListView list;
    private SimpleAdapter adapter;
    protected Context mContext;
//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        this.mActivity = activity;
//    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext =  context;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }

    private ListView ListView;

    //第一次画ui的时候调用,要显示Fragment自己的内容
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


        //通过打气筒把一个布局转换成view对象

        //View rootview=inflater.inflate(R.layout.fragment_news,container,false);

        View view = LayoutInflater.from(this.getActivity()).inflate(R.layout.fragment_news, null);




        //4.设置listview条目的点击事件
        lv_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
        });*/



        //1.找到控件
        ListView lv_news =  (ListView) view.findViewById(R.id.lv_newss);

        System.out.println("找到1111111");

        //2.获取新闻数据用list封装
        ArrayList<NewsBean> allNews = NewsUtils.getAllNews(getActivity());

        System.out.println("找到newsbean");
        //3.创建一个adapter设置给listview
        NewsAdapter newsAdapter = new NewsAdapter(getActivity(),allNews);
        lv_news.setAdapter(newsAdapter);

        //4.设置listview条目的点击事件
        //lv_news.setOnItemClickListener(this);


        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

主布局xml

 <android.support.design.widget.TabLayout
            android:id="@+id/tab_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/blue_light"
            app:tabTextColor="@color/material_orange" />
        <!--app:tabTextAppearance="@style/tablayoutIcon"-->

        <android.support.v4.view.ViewPager
            android:id="@+id/vp_pager"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_weight="1" />

fragment布局


    <?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="@+id/lv_newss"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="@color/colorPrimary">
    </ListView>



</LinearLayout><?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="@+id/lv_newss"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="@color/colorPrimary">
    </ListView>



</LinearLayout>
回复
阅读 3.3k
3 个回答

背景色有,那说明是你的ListView数据没有加载成功吧,你把ListView去掉,换成普通的TextView看看有没有内容显示

是不是你数据加载那一块有问题呀。

    //2.获取新闻数据用list封装
    ArrayList<NewsBean> allNews = NewsUtils.getAllNews(getActivity());
  

这个是异步的么?

1,先排除ListView布局是否有问题,比如有正常的宽高。
2,排除adapter数据是否有数据
3,再排除adapter的数据刷新是否有问题
4,再排除你的item是否是可见的

宣传栏