Android Development Notes (1)

Source: Internet
Author: User

1,listview the contents of the empty-time processing

When using the ListView or GridView, when the list is empty, it is sometimes necessary to display a special empty view to prompt the user

    private void Setupviews () {    log.debug ("");        Mlistview = (ListView) Findviewbyid (r.id.list);        ((ViewGroup) mlistview.getparent ()). AddView (Merrorview);        Mlistview.setemptyview (Merrorview);        Mprogressbar = (ProgressBar) Findviewbyid (r.id.pb_progress);    }
Also refer to: http://gundumw100.iteye.com/blog/1165673

3 properties in android:visibility in 2,androidThe visibility of controls or layouts in Android is android:visibility in 3 cases, such as View.visible,view.unvisible,view.gone in 3. View.visible is clearly visible, view.unvisible is not visible, but in this case it will occupy space. This means that if the control's android:visibility is set to View.unvisible, the control is hidden, but it still occupies the position of its layout in the picture, which is different from the meaning in C #. and View.goneThis means that the control is not visible and does not occupy space in the system layout.
Create event callback in 3,fragment, data communication methodIn some cases, it may be necessary to fragment and activity sharing events, a good practice is to define a callback interface in fragment, and then ask the host activity to implement it. When activity receives a callback through this interface, it can share this information with other fragment in the layout. For example, a news display app has two fragment in an activity, a fragment a displays a list of the title of the article, and a fragment B displays the article. So when an article is selected, fragment a must notify the activity and then activity informs fragment B to show the article. In this case, declare an interface such as Onarticleselectedlistener in fragment A:

public static class Fragmenta extends Listfragment {    ...    Container Activity must implement this interface public    interface Onarticleselectedlistener {public        void Onar ticleselected (Uri Articleuri);    }    ...}
This Onarticleselectedlistener interface is then included with the fragment activity, and the Overwrite onarticleselected () method notifies fragment B of what happened in fragment a. To ensure that the host activity implements this interface, the Onattach () method of fragment A (which is called by the system when the fragment is added to the activity) is converted by the input activity coercion type. Instantiate a Onarticleselectedlistener object:
public static class Fragmenta extends Listfragment {    Onarticleselectedlistener mlistener;    ...    @Override public    void Onattach (activity activity) {        Super.onattach (activity);        try {            Mlistener = (onarticleselectedlistener) activity;        } catch (ClassCastException e) {            throw new ClassCastException (activity.tostring () + "must implement Onarticleselectedlistener");        }    }    ...}
If the activity does not implement this interface, fragment will throw an classcastexception exception, and if it succeeds, Mlistener will be a reference to the activity implementation of the Onarticleselectedlistener interface, so by calling the Onarticleselectedlistener interface method, fragment A can share events with the activity. For example, if fragment A is a subclass of Listfragment, each time a user taps a list item, the system calls the Onlistitemclick () method in fragment, which can be called onarticleselected () in this method. method to share events with activity.
public static class Fragmenta extends Listfragment {    Onarticleselectedlistener mlistener;    ...    @Override public    void Onlistitemclick (ListView l, View v, int position, long ID) {        //Append The clicked item ' s RO W ID with the content provider uri        uri Noteuri = Contenturis.withappendedid (Articlecolumns.content_uri, ID);        Send the event and Uri to the host activity        mlistener.onarticleselected (Noteuri);    }    ...}





Android Development Notes (1)

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.