本文介绍了调用里面的Andr​​oid碎片功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用viewpager.I有两个fragments.Fragment1和Fragment2.Iam呼吁双方fragments.ie两个不同的功能,在片段1功能1和函数2上fragment2.When我启动应用程序,无论是我的android应用荫这两个函数被调用together.I想只有当我要刷我Fragment2.How能做到这一点叫功能1的​​启动和函数2?在此先感谢.....

下面是我的code,

片段1 =>

 公共类片段1片段扩展{
    公共静态字符串feedurl =htt​​p://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml;
    静态字符串URL =;
    静态最后弦乐KEY_HEAD =项; //父节点
    静态最后弦乐KEY_DATE =pubdate的;
    公共静态字符串headflag =;
    INT F = 0;
    GridView控件列表;
        HeadlinesAdapter适配器;
        私人TextView的mMessageView;
    私人按钮mClearButton;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        视图V = inflater.inflate(R.layout.first_fragment,集装箱,FALSE);
        返回伏;
    }    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);        功能1(); //这里我打电话功能1
        populate_listview();        }
     公共无效populate_listview()
     {
         URL =htt​​p://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml;
         ArrayList的<&HashMap的LT;字符串,字符串>>新闻列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();        XMLParser的解析器=新XMLParser的();
        字符串XML = parser.getXmlFromUrl(URL);
        文档的文档= parser.getDomElement(XML);
        节点列表NL = doc.getElementsByTagName(KEY_HEAD);
        节点列表itemLst = doc.getElementsByTagName(项目);
        串MarqueeStr =;        的for(int i = 0; I< nl.getLength();我++){
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
            元素e =(元)nl.item(I)
                        newsList.add(地图);
     }
        名单=(GridView控件)getActivity()findViewById(R.id.grid)。
        适配器=新的适配器1(getActivity(),新闻列表);
                list.setAdapter(适配器);      }
}

Fragment2 =>

 公共类Fragment2扩展片段{
    公共静态字符串feedurl =htt​​p://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml;
    静态字符串URL =;
    静态最后弦乐KEY_HEAD =项; //父节点
    静态最后弦乐KEY_DATE =pubdate的;
    公共静态字符串headflag =;
    INT F = 0;
    GridView控件列表;
    HeadlinesAdapter适配器;
    私人TextView的mMessageView;
    私人按钮mClearButton;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        视图V = inflater.inflate(R.layout.second_fragment,集装箱,FALSE);
        返回伏;
    }    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);        函数2(); //这里我打电话函数2
        populate_listview();        }
     公共无效populate_listview()
     {
         URL =htt​​p://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml;
         ArrayList的<&HashMap的LT;字符串,字符串>>新闻列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();        XMLParser的解析器=新XMLParser的();
        字符串XML = parser.getXmlFromUrl(URL);
        文档的文档= parser.getDomElement(XML);
        节点列表NL = doc.getElementsByTagName(KEY_HEAD);
        节点列表itemLst = doc.getElementsByTagName(项目);
        串MarqueeStr =;        的for(int i = 0; I< nl.getLength();我++){
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
            元素e =(元)nl.item(I)
                    newsList.add(地图);
     }
        名单=(GridView控件)getActivity()findViewById(R.id.grid2)。
        适配器=新HeadlinesAdapter(getActivity(),新闻列表);
                list.setAdapter(适配器);      }
}

主要活动=>

 公共类MainActivity扩展FragmentActivity {    私人ViewPager mViewPager;
    私人MessageLoader mLoader;
    私人按钮mSenderButton,mReceiverButton;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        //我们得到UI引用
        mViewPager =(ViewPager)findViewById(R.id.viewPager);
        mSenderButton =(按钮)findViewById(R.id.sender_button);
        mReceiverButton =(按钮)findViewById(R.id.receiver_button);
        //设置寻呼机适配器
        mViewPager.setAdapter(新MyAdapter(本));
        //设置接收器按钮侦听
        mReceiverButton.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v){
                mViewPager.setCurrentItem(1);
            }
        });        mSenderButton.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v){
                mViewPager.setCurrentItem(0);
            }
        });
    }
    私有类MyAdapter扩展FragmentPagerAdapter {        私人语境mContext;
        私有String [] =的frags {Fragment1.class.getName(),Fragment2.class.getName()};        公共MyAdapter(FragmentActivity活动){
            超级(activity.getSupportFragmentManager());
            mContext =活动;
        }        @覆盖
        公共对象instantiateItem(ViewGroup中的容器,INT位置){
            片段FRAG =(片段)super.instantiateItem(集装箱,位置);
            如果(FRAG的instanceof MessageLoader){
                mLoader =(MessageLoader)FRAG;
            }
            返回FRAG;
        }        @覆盖
        公共片段的getItem(INT POS){
            返回Fragment.instantiate(mContext,断肢[POS]);
        }        @覆盖
        公众诠释的getCount(){
            返回frags.length;
        }    }
}

下面是布局,

first_fragment.xml =>

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=#FFFF
    机器人:方向=垂直>    < RelativeLayout的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>    < / RelativeLayout的>    < GridView控件
        机器人:ID =@ + ID /格
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:为numColumns =2>
    < / GridView的>< / LinearLayout中>

second_fragment.xml =>

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=#FFFF
    机器人:方向=垂直>    < RelativeLayout的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>    < / RelativeLayout的>    < GridView控件
        机器人:ID =@ + ID / GRID2
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:为numColumns =2>
    < / GridView的>< / LinearLayout中>

main.xml中=>

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    <的TableRow
        机器人:layout_width =match_parent
        机器人:layout_height =50dp
        机器人:背景=#CCCCCC
        机器人:重力=center_vertical
        机器人:paddingTop =3DP>        <按钮
            机器人:ID =@ + ID / receiver_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文字=标题/>        <按钮
            机器人:ID =@ + ID / sender_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文字=喀拉拉/>
    < /&的TableRow GT;    < android.support.v4.view.ViewPager
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / viewPager
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>< / LinearLayout中>


解决方案

您可以使用此code捕获刷卡事件:

  mViewPager.setOnPageChangeListener(新OnPageChangeListener(){@覆盖
公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels)
{}@覆盖
公共无效使用onPageSelected(INT位置){
       如果(位置== 0){
        功能1();
       }否则如果(位置== 1){
        函数2();
       }}@覆盖
公共无效onPageScrollStateChanged(INT状态){}
});

调用你的函数中的任何最适合覆盖方法

In my android application Iam using viewpager.I have two fragments.Fragment1 and Fragment2.Iam calling two different function on both fragments.ie, 'function1' on Fragment1 and 'function2' on fragment2.When I start the application, both these two functions are called together.I want to call 'function1' on startup and 'function2' only when I swipe to Fragment2.How can I do this? Thanks in advance.....

Here is my code,

Fragment1=>

public class Fragment1 extends Fragment{


    public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
    static String URL = "";
    static final String KEY_HEAD = "item"; // parent node
    static final String KEY_DATE = "pubDate";
    public static String headflag="";
    int f=0;
    GridView list;
        HeadlinesAdapter adapter;
        private TextView mMessageView;
    private Button mClearButton;

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

        View v = inflater.inflate(R.layout.first_fragment, container, false);


        return v;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        function1();//here I am calling function1
        populate_listview();

        }


     public void populate_listview()
     {


         URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
         ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);
        NodeList nl = doc.getElementsByTagName(KEY_HEAD);
        NodeList itemLst = doc.getElementsByTagName("item");
        String MarqueeStr="";

        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
                        newsList.add(map);


     }
        list=(GridView)getActivity().findViewById(R.id.grid);
        adapter=new Adapter1(getActivity(), newsList);
                list.setAdapter(adapter);

      }




}

Fragment2=>

   public class Fragment2 extends Fragment{


    public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
    static String URL = "";
    static final String KEY_HEAD = "item"; // parent node
    static final String KEY_DATE = "pubDate";
    public static String headflag="";
    int f=0;
    GridView list;
    HeadlinesAdapter adapter;
    private TextView mMessageView;
    private Button mClearButton;

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

        View v = inflater.inflate(R.layout.second_fragment, container, false);


        return v;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        function2();//here I am calling function2
        populate_listview();

        }


     public void populate_listview()
     {


         URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
         ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);
        NodeList nl = doc.getElementsByTagName(KEY_HEAD);
        NodeList itemLst = doc.getElementsByTagName("item");
        String MarqueeStr="";

        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
                    newsList.add(map);


     }
        list=(GridView)getActivity().findViewById(R.id.grid2);
        adapter=new HeadlinesAdapter(getActivity(), newsList);
                list.setAdapter(adapter);

      }




}

Main Activity=>

public class MainActivity extends FragmentActivity {

    private ViewPager mViewPager;
    private MessageLoader mLoader;
    private Button mSenderButton, mReceiverButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // We get UI references
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mSenderButton = (Button) findViewById(R.id.sender_button);
        mReceiverButton = (Button) findViewById(R.id.receiver_button);
        // set pager adapter
        mViewPager.setAdapter(new MyAdapter(this));
        // set receiver button listener
        mReceiverButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mViewPager.setCurrentItem(1);
            }
        });

        mSenderButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mViewPager.setCurrentItem(0);
            }
        });
    }


    private class MyAdapter extends FragmentPagerAdapter{

        private Context mContext;
        private String[] frags = {Fragment1.class.getName(), Fragment2.class.getName()};

        public MyAdapter(FragmentActivity activity) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment frag = (Fragment) super.instantiateItem(container, position);
            if(frag instanceof MessageLoader){
                mLoader = (MessageLoader) frag;
            }
            return frag;
        }

        @Override
        public Fragment getItem(int pos) {
            return Fragment.instantiate(mContext, frags[pos]);
        }

        @Override
        public int getCount() {
            return frags.length;
        }

    }




}

Here is the layouts,

first_fragment.xml =>

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffff"
    android:orientation="vertical" >



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >



    </RelativeLayout>

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2" >
    </GridView>

</LinearLayout>

second_fragment.xml =>

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffff"
    android:orientation="vertical" >



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >



    </RelativeLayout>

    <GridView
        android:id="@+id/grid2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2" >
    </GridView>

</LinearLayout>

main.xml=>

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#CCCCCC"
        android:gravity="center_vertical"
        android:paddingTop="3dp" >

        <Button
            android:id="@+id/receiver_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Headlines" />

        <Button
            android:id="@+id/sender_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Kerala" />
    </TableRow>

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
解决方案

You can capture the swipe event by using this code:

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{

}

@Override
public void onPageSelected(int position) {
       if(position==0){
        function1();
       }else if(position==1){
        function2();
       }

}

@Override
public void onPageScrollStateChanged(int state) {

}
});

call your function in any of the best suited overridden method

这篇关于调用里面的Andr​​oid碎片功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:37