的Andr​​oid如何把列表视图鉴于寻呼机片段 [英] Android how to put listview in view pager fragments

查看:115
本文介绍了的Andr​​oid如何把列表视图鉴于寻呼机片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了使用viewpager通过在Eclipse中使用该模板swipey标签应用

现在我得到延伸FragmentActivity这样一个活动。

 包com.example.abcd2;    进口java.util.Locale中;    进口android.os.Bundle;
    进口android.support.v4.app.Fragment;
    进口android.support.v4.app.FragmentActivity;
    进口android.support.v4.app.FragmentManager;
    进口android.support.v4.app.FragmentPagerAdapter;
    进口android.support.v4.app.NavUtils;
    进口android.support.v4.view.ViewPager;
    进口android.view.Gravity;
    进口android.view.LayoutInflater;
    进口android.view.Menu;
    进口android.view.MenuItem;
    进口android.view.View;
    进口android.view.ViewGroup;
    进口android.widget.TextView;    公共类MainActivity扩展FragmentActivity {        / **
         *本{@link android.support.v4.view.PagerAdapter},将提供
         *对于每个部分的片段。我们使用
         * {@link android.support.v4.app.FragmentPagerAdapter}衍生物,其
         *将让每一个片段加载在内存中。如果这成为太内存
         *密集,它可能是最好切换到
         * {@link android.support.v4.app.FragmentStatePagerAdapter}。
         * /
        SectionsPagerAdapter mSectionsPagerAdapter;        / **
         *本{@link ViewPager}将承载部分内容。
         * /
        ViewPager mViewPager;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);            //创建将返回一个片段为三个的适配器
            //应用的主要部分。
            mSectionsPagerAdapter =新SectionsPagerAdapter(
                    getSupportFragmentManager());            //设置的ViewPager与部分适配器。
            mViewPager =(ViewPager)findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);        }        @覆盖
        公共布尔onCreateOptionsMenu(菜单菜单){
            //充气菜单;如果是present这增加了项目操作栏。
            。getMenuInflater()膨胀(R.menu.main,菜单);
            返回true;
        }        / **
         * A {@link FragmentPagerAdapter}返回对应片段
         *章节/标签/页面。
         * /
        公共类SectionsPagerAdapter扩展FragmentPagerAdapter {            公共SectionsPagerAdapter(FragmentManager FM){
                超(FM);
            }            @覆盖
            公共片段的getItem(INT位置){
                //的getItem被称为实例化片段为给定的页面。
                //返回一个DummySectionFragment(定义为静态内部类
                //下面)附页码为它的独立参数。
                如果(位置== 0)
{
片段frag2 =新ShowFrag2();
                返回frag2;}
其他
{
片段片段=新DummySectionFragment();
                捆绑ARGS =新包();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
                fragment.setArguments(参数);
                返回片段;}
            }            @覆盖
            公众诠释的getCount(){
                //显示3总页​​数。
                返回3;
            }            @覆盖
            公共CharSequence的getPageTitle(INT位置){
                区域设置L = Locale.getDefault();
                开关(位置){
                情况下0:
                    返回的getString(R.string.title_section1).toUpperCase(升);
                情况1:
                    返回的getString(R.string.title_section2).toUpperCase(升);
                案例2:
                    返回的getString(R.string.title_section3).toUpperCase(升);
                }
                返回null;
            }
        }        / **
         *一个虚拟片段重新presenting应用程序的一部分,但只是
         *显示虚拟文本。
         * /
        公共静态类DummySectionFragment扩展片段{
            / **
             *片段参数重新presenting本节号
             *片段。
             * /
            公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;            公共DummySectionFragment(){
            }            @覆盖
            公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                    捆绑savedInstanceState){
                查看rootView = inflater.inflate(R.layout.fragment_main_dummy,
                        集装箱,FALSE);
                TextView的dummyTextView =(TextView中)rootView
                        .findViewById(R.id.section_label);
                dummyTextView.setText(Integer.toString(getArguments()调用getInt(
                        ARG_SECTION_NUMBER)));
                返回rootView;
            }
        }    }

在此,打造一个选项卡中一个新的片段,我使用的if-else基于的getItem()方法的位置,如图所示。
片段showfrag2是这样

 进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;公共类ShowFrag2扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
        //充气的布局该片段
        返回inflater.inflate(R.layout.frag2,集装箱,FALSE);
    }
}

我的问题是,如何在这个片段中的一个创建列表视图。我想只有以这种方式使用一个简单的列表视图

  LevelAdapter适配器=新LevelAdapter(这一点,
            R.layout.list_item,weather_data);
    ListView1的=(的ListView)findViewById(R.id.listView1);    listView1.setAdapter(适配器);

不过,这已经在活动完成。我如何使用像这样的片段以上。另外,我无法使用ListFragment作为getView()方法返回一个片段。
请帮忙关于如何实现这一点。
谢谢
编辑:
这里是我的适配器code:

 包com.example.abcd3;进口android.app.Activity;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;公共类LevelAdapter扩展ArrayAdapter<平> {     静态上下文的背景下;
        静态INT layoutResourceId;
        静态层面的数据[] = NULL;    / *公共LevelAdapter(上下文的背景下,INT layoutResourceId,级别[]数据){
            超级(上下文,layoutResourceId,数据);
            this.layoutResourceId = layoutResourceId;
            this.context =背景;
            this.data =数据;
        } * /        公共LevelAdapter(ShowFrag1 showFrag1,INT的listItem,
                级别[] weather_data){
            超级(上下文,layoutResourceId,数据);
            this.layoutResourceId = layoutResourceId;
            this.context =背景;
            this.data =数据;            // TODO自动生成构造函数存根
        }        众长getItemId(INT位置){
            返回的位置;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            查看排= convertView;
            WeatherHolder支架=无效;            如果(行== NULL)
            {
                LayoutInflater充气=((活动)上下文).getLayoutInflater();
                行= inflater.inflate(layoutResourceId,父母,假);                持有人=新WeatherHolder();
                holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
                holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);
               // holder.imgIcon2 =(ImageView的)row.findViewById(R.id.imgIcon2);
                row.setTag(保持器);
            }
            其他
            {
                支架=(WeatherHolder)row.getTag();
            }            天气级数据= [位置]
            holder.txtTitle.setText(weather.title);
            holder.imgIcon.setImageResource(weather.icon);            返回行;
        }        静态类WeatherHolder
        {
            ImageView的imgIcon;
            TextView的txtTitle;
        // ImageView的imgIcon2;
        }}


解决方案

请确保您有的ListView R.layout.frag2 <定义/ code>并设置 ShowFrag2 onCreateView >。 (类似其活动所做的那样)

 公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    查看fragmentLayout = inflater.inflate(R.layout.frag2,集装箱,FALSE);    ListView控件ListView1的= fragmentLayout.findViewById(R.id.mylistview);    LevelAdapter适配器=新LevelAdapter(这一点,R.layout.list_item,weather_data);
    ListView1的=(的ListView)findViewById(R.id.listView1);
    listView1.setAdapter(适配器);    返回伏;}

I have implemented a swipey tabs app using viewpager by using this template in Eclipse

Now i get an activity which extends FragmentActivity like this

        package com.example.abcd2;

    import java.util.Locale;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.ViewPager;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class MainActivity extends FragmentActivity {

        /**
         * The {@link android.support.v4.view.PagerAdapter} that will provide
         * fragments for each of the sections. We use a
         * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
         * will keep every loaded fragment in memory. If this becomes too memory
         * intensive, it may be best to switch to a
         * {@link android.support.v4.app.FragmentStatePagerAdapter}.
         */
        SectionsPagerAdapter mSectionsPagerAdapter;

        /**
         * The {@link ViewPager} that will host the section contents.
         */
        ViewPager mViewPager;

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

            // Create the adapter that will return a fragment for each of the three
            // primary sections of the app.
            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        /**
         * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
         * one of the sections/tabs/pages.
         */
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

            @Override
            public Fragment getItem(int position) {
                // getItem is called to instantiate the fragment for the given page.
                // Return a DummySectionFragment (defined as a static inner class
                // below) with the page number as its lone argument.
                if(position==0)
{
Fragment frag2=new ShowFrag2();
                return frag2;}
else
{
Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;}
            }

            @Override
            public int getCount() {
                // Show 3 total pages.
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
                }
                return null;
            }
        }

        /**
         * A dummy fragment representing a section of the app, but that simply
         * displays dummy text.
         */
        public static class DummySectionFragment extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            public static final String ARG_SECTION_NUMBER = "section_number";

            public DummySectionFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                        container, false);
                TextView dummyTextView = (TextView) rootView
                        .findViewById(R.id.section_label);
                dummyTextView.setText(Integer.toString(getArguments().getInt(
                        ARG_SECTION_NUMBER)));
                return rootView;
            }
        }

    }

In this, to create a new fragment in one of the tabs, i use if-else based on position in the getItem() method as shown. The fragment showfrag2 is this

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

public class ShowFrag2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.frag2, container, false);
    }
}

My question is, how do i create a listview in one of this fragments . I would like to use a simple listview only in this manner

LevelAdapter adapter = new LevelAdapter(this,
            R.layout.list_item, weather_data);


    listView1 = (ListView)findViewById(R.id.listView1);



    listView1.setAdapter(adapter);

But this has been done in an activity. How can i use something like this in the fragment above. Also, i cannot use ListFragment as the getView() method is returning a fragment. Please help regarding how to implement this. Thanks EDIT: Here is my adapter code :

    package com.example.abcd3;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LevelAdapter extends ArrayAdapter<Level> {

     static Context context;
        static int layoutResourceId;   
        static Level data[] = null;

    /*    public LevelAdapter(Context context, int layoutResourceId, Level[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }*/

        public LevelAdapter(ShowFrag1 showFrag1, int listItem,
                Level[] weather_data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;

            // TODO Auto-generated constructor stub
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            WeatherHolder holder = null;

            if(row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new WeatherHolder();
                holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
                holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
               // holder.imgIcon2=(ImageView)row.findViewById(R.id.imgIcon2);


                row.setTag(holder);
            }
            else
            {
                holder = (WeatherHolder)row.getTag();
            }

            Level weather = data[position];
            holder.txtTitle.setText(weather.title);
            holder.imgIcon.setImageResource(weather.icon);

            return row;
        }

        static class WeatherHolder
        {
            ImageView imgIcon;
            TextView txtTitle;
        //    ImageView imgIcon2;
        }

}

解决方案

Make sure you have ListView defined in R.layout.frag2 and set the adapter to listview in onCreateView of ShowFrag2. (Similar to the way its done in activity)

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

    View fragmentLayout = inflater.inflate(R.layout.frag2, container, false);

    ListView listView1 = fragmentLayout.findViewById(R.id.mylistview);

    LevelAdapter adapter = new LevelAdapter(this, R.layout.list_item, weather_data);
    listView1 = (ListView)findViewById(R.id.listView1);
    listView1.setAdapter(adapter);

    return v;

}

这篇关于的Andr​​oid如何把列表视图鉴于寻呼机片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆