Simple fragment transfer of data, two buttons to achieve a switch

Source: Internet
Author: User
Tags commit json

Very simple two button implementation added in the data, in fact, two are the same only changed a URL ...

Do the comparison low. Please understand//the nonsense does not say above figure ~

The second button I'm not going to figure out the same as this one.

Can see two buttons to achieve the switch, the button I do not set the background and the selected color, is normal white and then click Switch Fragment.

I do not use this switch ViewPage, is to click the button to switch fragment and content

Attach the XML file first

Main layout:

is a radiogroup inside a set of two buttons, Radiogrouo inside the horizontal must write not write will cause button buttons no things

<radiogroup android:id= "@+id/rg" android:layout_width= "match_parent" android:layout_height= "50DP" Andro id:orientation= "Horizontal" > <radiobutton android:id= "@+id/btn_liuxing" Android:layout_widt H= "0DP" android:layout_weight= "1" android:layout_height= "Match_parent" android:text= "popular" a
        ndroid:button= "@null" android:gravity= "center" android:textsize= "20sp"/> <radiobutton Android:id= "@+id/bent_remen" android:layout_width= "0DP" android:layout_weight= "1" android:l
        ayout_height= "Match_parent" android:text= "hot" android:textsize= "20sp" android:button= "@null" android:gravity= "center"/> </RadioGroup> <framelayout android:id= "@+id/fl" android:layout _width= "Match_parent" android:layout_height= "match_parent" android:layout_below= "@+id/rg" ></FrameLayout >
Main layout code: Inside is the declaration fragment radiogroup Click Switch

Full use of V4 package

Import Android.os.Bundle;

Import Android.widget.RadioGroup;

    public class Mainactivity extends Appcompatactivity {//Fragment layout fill prepare//Fragment Manager Object Private Fragmentmanager manager;
    Fragment performer object private fragmenttransaction ft;
    Two button click event Initialization private Radiogroup RG;
    Private fragment_liuxing liuxing;

    Private Fragment_remen remen;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_main);
        Initialize Control RG = (Radiogroup) Findviewbyid (R.ID.RG);
        Fragment Manager Object manager = Getsupportfragmentmanager ();
        Fragment Performer Object ft = manager.begintransaction (); Liuxing = new fragment_liuxing ();
        Remen = new Fragment_remen ();
        The first page shows Ft.add (R.ID.FL, liuxing);

        Submit Event Ft.commit ();
            Radiogroup Click event Rg.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {@Override
                public void OnCheckedChanged (Radiogroup radiogroup, int i) {ft = Manager.begintransaction ();
                        switch (i) {///The first button toggles case
                        Here to write the replacement ft.replace (R.ID.FL, liuxing);
                        The second button toggles case (R.ID.FL, remen);
            }//must write without writing will cause the Whiteboard to have nothing ft.commit (); }
Fragment popular main layout code:

Network access is available using a third-party framework Okhttp copied and pasted into the app

Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;

Import Android.widget.ListView;
Import Com.squareup.okhttp.Call;
Import Com.squareup.okhttp.Callback;
Import com.squareup.okhttp.OkHttpClient;
Import Com.squareup.okhttp.Request;

Import Com.squareup.okhttp.Response;

Import java.util.List;

 /** * Created by Administrator on 2016/10/8.
    */public class Fragment_liuxing extends fragment {//Get ListView private ListView LV;
    Private list<musicinfo> List;
    Build adapter private baseadapter_liuxing adapter;  Handler Async Private Handler Handler = new Handler () {@Override public void Handlemessage (Message msg)
     {       Switch (msg.what) {case 1:string str = (String) msg.obj;
                    List = Parserjson.getparserjson (str);
                    adapter = new Baseadapter_liuxing (list,getactivity ());
                    Suitable for lv.setadapter (adapter);
    }; @Nullable @Override public View oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bun
        Dle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_liuxing,null);
        Initialize control LV = (ListView) View.findviewbyid (;
    return view;
        }//This method is no different from the previous one, @Override public void onactivitycreated (@Nullable Bundle savedinstancestate) {
        Super.onactivitycreated (savedinstancestate);
    Getokttp (); }//Network access third-party framework (OKHTTP): Compile ' com.squareup.okhttp:okhttp:2.7.5 ' public void gEtokttp () {okhttpclient client = new Okhttpclient ();
        Request Request = new Request.builder (). URL (contanst.url). build ();
        Call call = Client.newcall (request); Call.enqueue (New Callback () {//download failed @Override public void onfailure (Request request,
            IOException e) {log.i ("tag", "Download Failed");
                }//download succeeded @Override public void Onresponse (Response Response) throws IOException {
                String json = Response.body (). String ();
                Message message = new Message ();
                Pass message.what = 1;
                Message.obj = JSON;
            Handler.sendmessage (message);

Fragment Adapter Code:

Import Android.content.Context;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;

Import Android.widget.TextView;

Import Com.bumptech.glide.Glide;

Import java.util.List;

 /** * Created by Administrator on 2016/10/8.
    */public class Baseadapter_liuxing extends Baseadapter {private list<musicinfo> List;

    Private context context;
        Public baseadapter_liuxing (list<musicinfo> List, Context context) {This.context = context;
    This.list = list;
    } @Override public int getcount () {return list.size ();
    } @Override public Object getItem (int i) {return list.get (i);
    } @Override public long getitemid (int i) {return i;
    } Viewholder holder = null; @Override public view GetView (int position, view Convertview,ViewGroup parent) {if (Convertview = = null) {Layoutinflater layoutinflater = Layoutinflater.from (cont
            Convertview = Layoutinflater.inflate (r.layout.liuxing_item,null);
            Holder = new Viewholder ();
            HOLDER.IV = (ImageView) Convertview.findviewbyid (;
            Holder.tv_title = (TextView) Convertview.findviewbyid (;
            Holder.tv_author = (TextView) Convertview.findviewbyid (; Holder.tv_time = (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.