Mobile Mall The next day, each function button switch and display different interface

Source: Internet
Author: User

These days evening practice three, generally to 10 points more back, so the update is relatively late.

Code has been uploaded to the code cloud, interested can be downloaded to see

Https://git.oschina.net/joy_yuan/ShoppingMall


In the previous project, the page switch was done with a basepage, and then, using the construction method, the control was initialized when the construct was called, and then the data was loaded when the specific page was switched.

In this project, is to do a basefragment extends Fragment, and then follow the life cycle of Fragment:

1, OnCreate ()

2, Oncreateview ()

3, onactivitycreated ()

To complete initialization and loading of data. About these three methods, the previous blog, the first day of the mobile store has been explained, these three methods are in chronological order, from the first to start execution, to the last execution completed, so you can initialize the control in the Oncreateview, onactivitycreated loading data

Each of the 2 methods have advantages, it depends on everyone's liking.


One, switch each page, display different content

The first page of the homepage is displayed, simply write a textview in it

package com.yuanlp.shoppingmall.home.fragment;import android.annotation.suppresslint;import  android.content.context;import android.graphics.color;import android.view.gravity;import  android.view.view;import android.widget.textview;import com.yuanlp.shoppingmall.base.basefragement;/ The fragment */@SuppressLint ** * created by  Yuriping  on 2017/8/8. *  Main Page (" Validfragment ") public class homefragment extends basefragement{     private textview textview;     @SuppressLint ("Validfragment")      public homefragment (Context context)  {    }    /* *     *  force subclasses to implement him, let's show this layout in Oncreateview.      *     *  @return      */      @Override     public View initview ()  {        textview=new textview (context) The context         textview.setgravity (Gravity.CENTER) in the parent class of the   //call;         textview.settextsize (+);         textview.settextcolor (color.red);        return  textview;    }     @Override     public void  initdata ()  {        super.initdata ();         textview.settext ("Main Page");         System.out.println ("Start loading Data--------------------------");     }}

2. Switch pages in mainactivity

A, first in the OnCreate to initialize each page (temporarily 2 pages, the other can be initialized)

Baselist=new arraylist<basefragement> (); Baselist.add (new Homefragment (this)); Baselist.add (new Shoppingfragment (this));

B, set Radiogroup listener event, when Radiogroup's selected button changes, callback the method, and switch the view page

Mrgmain.setoncheckedchangelistener (New myoncheckedchangelistener ()); Mrgmain.check (R.id.rb_home); class  MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {      @Override     public void oncheckedchanged (Radiogroup group,   @IdRes  int checkedid)  {        switch  ( Checkedid) {            case r.id.rb_home:                 position=0;                  System.out.println ("OnCheckedChanged:--------111----------------------------------------- ");   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (TAG,  " OnCheckedChanged:--------111-----------------------------------------  ");                 break;            case r.id.rb_ cart:                position= 1;                break;             default:                 position=0;       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (tag,  "onCheckedChanged:---------------- ---------------------------------  ");                 break;        }          Setfragement ();   //switch view page     } 

C, according to the current selected state, switch page

private void Setfragement () {System.out.println ("setfragement:--------------------------------------------");    LOG.D (TAG, "setfragement:--------------------------------------------");    Fragmentmanager Fragmentmanager=getsupportfragmentmanager ();    Fragmenttransaction transaction = Fragmentmanager.begintransaction ();    Basefragement Basefragement=baselist.get (position);    Transaction.replace (r.id.framelayout,basefragement); Transaction.commit ();}


This blog is mainly to summarize the method of switching fragment in 2, from the ease of the degree, or this relatively simple point.

In this debugging, there is Lod.d () This android comes with the output, there is also System.out.println () This Java comes with the output, but sometimes, if the error, LOG.D () This method is not to display the contents of it, And the Java comes with this output can be normal output, you can let us know where the program execution to the error.


Interested can download my 2 code to see.

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1954653

Mobile Mall The next day, each function button switch and display different interface

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: info-contact@alibabacloud.com 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.