java - How to get a two level list view to work within a swipe tab layout -


i'm trying create app user can select item in list , click item in list opens page, i've got work on own , i've managed put swipe tab layout when user clicks on item doesn't did before in own activity can help?

i've been told need create activity class implements onarticleselected method , let mainactivity , softwaresearchactivity extend activity class avoid code-duplication.

but don't know means or how implement it. i've literally been doing month i'm not familiar of terms used.

i think of code in there had working before don't know how rearrange work within in swipe tab layout.

articlefragment.java

/*  * copyright (c) 2012 android open source project  *  * licensed under apache license, version 2.0 (the "license");  * may not use file except in compliance license.  * may obtain copy of license @  *  *      http://www.apache.org/licenses/license-2.0  *  * unless required applicable law or agreed in writing, software  * distributed under license distributed on "as is" basis,  * without warranties or conditions of kind, either express or implied.  * see license specific language governing permissions ,  * limitations under license.  */ package com.learn2crack.tab;  import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview;  public class articlefragment extends fragment {     final static string arg_position = "position";     int mcurrentposition = -1;      @override     public view oncreateview(layoutinflater inflater, viewgroup container,          bundle savedinstancestate) {          // if activity recreated (such screen rotate), restore         // previous article selection set onsaveinstancestate().         // necessary when in two-pane layout.         if (savedinstancestate != null) {             mcurrentposition = savedinstancestate.getint(arg_position);         }          // inflate layout fragment         return inflater.inflate(r.layout.article_view, container, false);     }      @override     public void onstart() {         super.onstart();          // during startup, check if there arguments passed fragment.         // onstart place because layout has been         // applied fragment @ point can safely call method         // below sets article text.         bundle args = getarguments();         if (args != null) {             // set article based on argument passed in             updatearticleview(args.getint(arg_position));         } else if (mcurrentposition != -1) {             // set article based on saved instance state defined during oncreateview             updatearticleview(mcurrentposition);         }     }      public void updatearticleview(int position) {         textview article = (textview) getactivity().findviewbyid(r.id.article);         article.settext(ipsum.articles[position]);         mcurrentposition = position;     }      @override     public void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);          // save current article selection in case need recreate fragment         outstate.putint(arg_position, mcurrentposition);     } } 

headlinesfragment.java

/*  * copyright (c) 2012 android open source project  *  * licensed under apache license, version 2.0 (the "license");  * may not use file except in compliance license.  * may obtain copy of license @  *  *      http://www.apache.org/licenses/license-2.0  *  * unless required applicable law or agreed in writing, software  * distributed under license distributed on "as is" basis,  * without warranties or conditions of kind, either express or implied.  * see license specific language governing permissions ,  * limitations under license.  */ package com.learn2crack.tab;  import android.app.activity; import android.os.build; import android.os.bundle; import android.support.v4.app.listfragment; import android.view.view; import android.widget.arrayadapter; import android.widget.listview;  public class headlinesfragment extends listfragment {     onheadlineselectedlistener mcallback;       // container activity must implement interface frag can deliver messages     public interface onheadlineselectedlistener {         /** called headlinesfragment when list item selected */         public void onarticleselected(int position);     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // need use different list item layout devices older honeycomb         int layout = build.version.sdk_int >= build.version_codes.honeycomb ?                 android.r.layout.simple_list_item_activated_1 : android.r.layout.simple_list_item_1;          // create array adapter list view, using ipsum headlines array         setlistadapter(new arrayadapter<string>(getactivity(), layout, ipsum.headlines));     }      @override     public void onstart() {         super.onstart();          // when in two-pane layout, set listview highlight selected list item         // (we during onstart because @ point listview available.)         if (getfragmentmanager().findfragmentbyid(r.id.article_fragment) != null) {             getlistview().setchoicemode(listview.choice_mode_single);         }     }      @override     public void onattach(activity activity) {         super.onattach(activity);          // makes sure container activity has implemented         // callback interface. if not, throws exception.         try {             mcallback = (onheadlineselectedlistener) activity;         } catch (classcastexception e) {             throw new classcastexception(activity.tostring()                     + " must implement onheadlineselectedlistener");         }     }      @override     public void onlistitemclick(listview l, view v, int position, long id) {         // notify parent activity of selected item         mcallback.onarticleselected(position);          // set item checked highlighted when in two-pane layout         getlistview().setitemchecked(position, true);     } } 

mainactivity.java

package com.learn2crack.tab;  import android.app.actionbar; import android.app.fragmenttransaction; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager;  public class mainactivity extends fragmentactivity implements  headlinesfragment.onheadlineselectedlistener {     viewpager tab;     tabpageradapter tabadapter;     actionbar actionbar;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          tabadapter = new tabpageradapter(getsupportfragmentmanager());          tab = (viewpager)findviewbyid(r.id.pager);         tab.setonpagechangelistener(                 new viewpager.simpleonpagechangelistener() {                     @override                     public void onpageselected(int position) {                          actionbar = getactionbar();                         actionbar.setselectednavigationitem(position);                    }                 });         tab.setadapter(tabadapter);          actionbar = getactionbar();         //enable tabs on action bar         actionbar.setnavigationmode(actionbar.navigation_mode_tabs);         actionbar.tablistener tablistener = new actionbar.tablistener(){              @override             public void ontabreselected(android.app.actionbar.tab tab,                     fragmenttransaction ft) {                 // todo auto-generated method stub              }              @override              public void ontabselected(actionbar.tab tab, fragmenttransaction ft) {                  tab.setcurrentitem(tab.getposition());             }              @override             public void ontabunselected(android.app.actionbar.tab tab,                     fragmenttransaction ft) {                 // todo auto-generated method stub              }};             //add new tab             actionbar.addtab(actionbar.newtab().settext("android").settablistener(tablistener));             actionbar.addtab(actionbar.newtab().settext("ios").settablistener(tablistener));             actionbar.addtab(actionbar.newtab().settext("windows").settablistener(tablistener));      }  @override public void onarticleselected(int position) {     // todo auto-generated method stub  }   } 

softwaresearchactivity.java

/*  * copyright (c) 2012 android open source project  *  * licensed under apache license, version 2.0 (the "license");  * may not use file except in compliance license.  * may obtain copy of license @  *  *      http://www.apache.org/licenses/license-2.0  *  * unless required applicable law or agreed in writing, software  * distributed under license distributed on "as is" basis,  * without warranties or conditions of kind, either express or implied.  * see license specific language governing permissions ,  * limitations under license.  */ package com.learn2crack.tab;  import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmenttransaction;  interface onheadlineselectedlistener {     /** called headlinesfragment when list item selected */     public void onarticleselected(int position); }  public class softwaresearchactivity extends fragmentactivity          implements headlinesfragment.onheadlineselectedlistener {      /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.news_articles);          // check whether activity using layout version         // fragment_container framelayout. if so, must add first fragment         if (findviewbyid(r.id.fragment_container) != null) {              // however, if we're being restored previous state,             // don't need , should return or else             // end overlapping fragments.             if (savedinstancestate != null) {                 return;             }              // create instance of examplefragment             headlinesfragment firstfragment = new headlinesfragment();              // in case activity started special instructions intent,             // pass intent's extras fragment arguments             firstfragment.setarguments(getintent().getextras());              // add fragment 'fragment_container' framelayout             getsupportfragmentmanager().begintransaction()                     .add(r.id.fragment_container, firstfragment).commit();         }     }      public void onarticleselected(int position) {         // user selected headline of article headlinesfragment          // capture article fragment activity layout         articlefragment articlefrag = (articlefragment)                 getsupportfragmentmanager().findfragmentbyid(r.id.article_fragment);          if (articlefrag != null) {             // if article frag available, we're in two-pane layout...              // call method in articlefragment update content             articlefrag.updatearticleview(position);          } else {             // if frag not available, we're in one-pane layout , must swap frags...              // create fragment , give argument selected article             articlefragment newfragment = new articlefragment();             bundle args = new bundle();             args.putint(articlefragment.arg_position, position);             newfragment.setarguments(args);             fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();              // replace whatever in fragment_container view fragment,             // , add transaction stack user can navigate             transaction.replace(r.id.fragment_container, newfragment);             transaction.addtobackstack(null);              // commit transaction             transaction.commit();         }     } } 

tabpageradapter.java

package com.learn2crack.tab;  import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter;  public class tabpageradapter extends fragmentstatepageradapter {     public tabpageradapter(fragmentmanager fm) {         super(fm);         // todo auto-generated constructor stub     }      @override     public fragment getitem(int i) {         switch (i) {         case 0:             //fragement android tab             return new headlinesfragment();         case 1:            //fragment ios tab             return new windows();         case 2:             //fragment windows tab             return new windows();         }         return null;      }      @override     public int getcount() {         // todo auto-generated method stub         return 3; //no of tabs     }       } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.viewpager     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="match_parent" />  article_view.xml <?xml version="1.0" encoding="utf-8"?>   <textview xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/article"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:textsize="18sp" /> 

news_article.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/fragment_container"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical" >  </linearlayout> 

layout-large folder

new_articles.xml

<?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="match_parent">      <fragment android:name="com.example.android.fragments.headlinesfragment"               android:id="@+id/headlines_fragment"               android:layout_weight="1"               android:layout_width="0dp"               android:layout_height="match_parent" />      <fragment android:name="com.example.android.fragments.articlefragment"               android:id="@+id/article_fragment"               android:layout_weight="2"               android:layout_width="0dp"               android:layout_height="match_parent" />  </linearlayout> 

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.learn2crack.tab"     android:versioncode="1"     android:versionname="1.0" >      <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <!-- following 2 permissions not required use      google maps android api v2, recommended. --> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_fine_location"/>      <uses-sdk         android:minsdkversion="15"         android:targetsdkversion="19" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.learn2crack.tab.mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>            <meta-data     android:name="com.google.android.maps.v2.aizasydtqicxqalanjtjyxy-ghqsg7rod4tm8hq"     android:value="aizasydtqicxqalanjtjyxy-ghqsg7rod4tm8hq"/>     </application>  </manifest> 


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -