GridView & ListView (Android Box development)

Source: Internet
Author: User

I. How to solve the bug of focus in the GridView Android4.4

Android4.4, there is a bug about the focus of the GridView. This bug is not easy to find, but it is obvious in the development of the TV box. The specific performance is that the GridView will always hold a focus, as long as there is data, there will always be a child view is the selected state. And, when the data is from scratch, the GridView will also grab the spotlight.

The last way to fix this bug is to build a custom gridview, rewrite a method,

@Overridepublic Boolean Isintouchmode () {    return! ( Hasfocus () &&!super.isintouchmode ());}

I did not understand why, but adding this sentence did solve the bug.

Two. The implementation of the Girdview with the magnified effect of the selected item

Wrap the static scale animation in the Animafactory class first.

/** * Zoom animation, for zooming control * @param startscale control's start size magnification * @param endscale control's end size magnification * @return */public static Animation Zoomanimat Ion (float Startscale, float endscale, int duration) {    Scaleanimation anim = new Scaleanimation (Startscale, Endscale, S Tartscale, Endscale,            animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);    Anim.setfillafter (true);    Anim.setduration (duration);    return anim;}

Then add the following method to the custom GridView.

This method is called when 
/** * implements this class of Onfocuschangelistener * @param v * @param hasfocus */public void Onfocuschange (View V, boolean HASFOC    US) {if (Hasfocus) {Zoominview (Lastview);    } else {Zoomoutview (Lastview); }}/** * When calling Onitemselectedlistener outside, call this method * @param parent * @param view * @param position * @param id */public void OnI    Temselected (adapterview<?> Parent, view view, int position, long id) {Zoominview (view);    if (view! = Lastview) {Zoomoutview (Lastview); } Lastview=view;} /** * magnification * @param v */private void Zoominview (View v) {if (v! = null) {v.startanimation (Animafactory.zoomanima    tion (1.0f, 1.1f, 100)); }}/** * Reduced * @param v */private void Zoomoutview (View v) {if (v! = null) {v.startanimation (Animafactory.zooman    Imation (1.1f, 1.0f, 100)); }}

The effect of the Onselected method is to enlarge the view that is being selected to zoom out the last selected view, which is lastview. The Lastview is then updated to the currently selected view.

This method is called when the Onselectedlistener is set to the GridView outside.

Similarly, set the Onfucuschange method to be called in Onfocuschangelistener.

Here's one more thing to say, although the selected item is enlarged, it's not necessarily drawn at the top. In order to ensure that it will not be blocked, we will add a method.

/** * @description: < Modify the default load order (core code) > * @see android.view.viewgroup#getchilddrawingorder (int, int) * @param ChildCount the number of items displayed in the current screen * @param i position in the array, starting from 0 to ChildCount-1 * @return Loading order */@Overrideprotected int Getchilddrawingord ER (int childCount, int i) {    if (this.getselecteditemposition ()! =-1) {        if (i + this.getfirstvisibleposition () = = This.getselecteditemposition ()) {//This is the item return that was originally to be refreshed in the last            childCount-1;        }        if (i = = childCount-1) {//This is the last item to be refreshed,            return this.getselecteditemposition ()-this.getfirstvisibleposition ();        }    }    return i;}

After adding this code, add a sentence to the construction method,

Setchildrendrawingorderenabled (TRUE);

To ensure that the loading order is correct.

Three. Reset the data setlist () method to be aware of

There are two ways to reset the data, if the data passed in the adapter, then directly to the GridView call Setadapter can reset the data.

Another, in adapter, is to write the setlist method to pass in the data. Like what:

public void setlist (list<videoobject> List) {    notifydatasetinvalidated ();    Mlist=list;    Notifydatasetchanged ();}

The Notifidatasetchange () method is better understood because data is refreshed after resetting the data. What does the notifydatasetinvalidated () do? Its role is to inform Adatper that the old data is invalid. If you do not write this line of code, you may have an array out-of-bounds exception when resetting the data.

Four. About paging load data

Because the TV box does not have a touch screen, the operating system is controlled by a remote control focus. So paging is not loaded through "pull-down", but rather through focus. And the judgment statement is implemented in Onselectedchangelistener.

First, write the code to determine the number of rows in the row to which you want to request data. Here I am set as lastlinenum.

Lastlinenum = (List.size ()-1)/gridview.getnumcolumns ();

The list here is the data that gets, and then the listener is set.

Gridview.setonitemselectedlistener (New Onitemselectedlistener () {    @Override public    void onitemselected ( Adapterview<?> Parent, view view, int position, long id) {        gridview.onitemselected (parent, view, position, id);        //If paging is loaded in the last row, then paging data is required if        (lastlinenum! = 0 && position/gridview.getnumcolumns () = = Lastlinenum) {
   
    //request data here and update view        }        @Override public        void onnothingselected (adapterview<?> parent) {        }});
   

Request data to update the view, Addlist in adapter, and then Notifydatasetchange.

Five. Get the location of the data being displayed and how to fix the bug

Both the GridView and the ListView have a method of getfirstvisibleposition (), which is the location to get the data that is being displayed for the first one. But this method has a flaw, that is, when the data, only half or even less, this method to determine that it has been shown. However, it is not fully displayed.

The function I was going to implement was when there was no data on the upper side of the ListView, there was an arrow on the top side and there was an arrow on the lower side of the ListView when there was no data displayed. Both of these arrows are imageview.

Later the solution to this flaw is in the following way.

Lvchildcategory.setonscrolllistener (New Onscrolllistener () {public void onscrollstatechanged (abslistview view, int SC rollstate) {} public void onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount {if (Totalitemcount = = 0 | | visibleitemcount = = 0) {//When no data is displayed, two arrows on the upper and lower sides are not displayed Ivarrayup.set            Visibility (view.invisible);            Ivarraydown.setvisibility (view.invisible);        Return                } else {//when there is data to display if (Firstvisibleitem > 0) {//when the first displayed data is not the first data, the arrow displays            Ivarrayup.setvisibility (view.visible); } else {//when the first displayed data is the first data, add the judgment///The top coordinate of the first data int firsttop = View.getchildat (                0). GetTop ();                The upper coordinate of the listview int paddingtop = View.getlistpaddingtop ();                If the top coordinate of the first data does not exceed the bounds of the ListView, then the UP arrow does not show int spaceabove = Paddingtop-firsttop; If (spaceabove <= 0) ivarrayup.setvisibility (view.invisible); }//down ARROW with up ARROW same as if (Firstvisibleitem + VisibleItemCount < Totalitemcount) {Ivarrayd            Own.setvisibility (view.visible);                } else {int lastbottom = View.getchildat (View.getchildcount ()-1). Getbottom ();                int paddingbottom = View.getlistpaddingbottom ();            if (Lastbottom <= (View.getheight ()-paddingbottom)) ivarraydown.setvisibility (view.invisible); }        }    }});

The upper coordinate of the child view is obtained and the top coordinate of the ListView is judged. Resolve this flaw.

Six. The GridView View small details

In the GridView, selector affects the layout of sub-layouts. Generally not obvious, but if the selector is a thick layer of shadow, sub-layout plus selector will shrink one lap.

To solve this problem, you can adjust the horizontalspacing and verticalspacing properties of the Girdview, respectively, to represent the sub-layout transverse and longitudinal gaps, which can be set to a negative number. If the sub-layout is shrunk too much after adding selector, these two values are adjusted to negative numbers to resolve the problem.

At the same time, the child layout width of the GridView is not adjustable, and the width can be adjusted by setting NumColumns and horizontalspacing.

GridView & ListView (Android Box development)

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.