The entire page of The gridview slide, slide up and down, the page of the gridview flip
Statement in advance: this blog is based on a TV set-top box and uses a remote control for all operations.
Reprinted please indicate the source: http://blog.csdn.net/harryweasley/article/details/46784667
Before reading this article, you need to learn about smooth scrolling.
For smooth scrolling of the gridview, you can view the http://blog.csdn.net/harryweasley/article/details/46714059
Let's take a look at the application ,:
In this example, there are six complete items on one page.
See the documents compiled into excel, as shown in:
The successful bidding is the first position of the complete last line on each page.
The idea is as follows: assume that the gridview is at the 3 position. When I click the button again, the whole page is flipped to the 6 position, and 6 is aligned with the top side.
:
Suppose that the current position of the gridview is 6. When I click the up button again, the whole page is turned up, which is exactly the same as the previous page.
The general functions are as follows:
Package com. example. test; import java. util. arrayList; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. keyEvent; import android. view. layoutInflater; import android. view. view; import android. view. view. onKeyListener; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. w Idget. baseAdapter; import android. widget. gridView; import android. widget. imageView; import android. widget. textView; public class MainActivity extends Activity {GridView gv;/*** selected position value */int selected; /*** whether there is an upward animation */boolean isUp = false;/*** whether there is a downward animation */boolean isDown = false; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. Activity_main); gv = (GridView) findViewById (R. id. gridview); gv. setAdapter (new MyBaseAdapter (this); gv. setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {selected = position; if (isDown) {isDown = false; gv. smoothScrollToPositionFromTop (position, 0, 1000);} if (isUp) {isUp = false; // align the first line of the page with the top edge gv. smoothScrollToPositionFromTop (position-3, 0, 1000) ;}@ Overridepublic void onNothingSelected (AdapterView <?> Parent) {}}); gv. setOnKeyListener (new OnKeyListener () {@ Overridepublic boolean onKey (View v, int keyCode, KeyEvent event) {// if the complete last line on a page is, and click the down button if (isAllPageDown () & keyCode = KeyEvent. KEYCODE_DPAD_DOWN & event. getAction () = KeyEvent. ACTION_DOWN) {isDown = true;} // if you are in the first row of each page and click the up button if (isAllPageUp () & keyCode = KeyEvent. KEYCODE_DPAD_UP & event. getAction () = KeyEvent. ACTION_DOWN) {isUp = true;} return false ;}}) ;}// whether to complete the last line of private boolean isAllPageUp () on a page () {// determine the total number of pages.-1 is not required here, because the last row int rawNum = 20/6 + 1 is required to slide upwards; for (int I = 1; I <= rawNum; I ++) {if (selected = 6 * I) {return true;} if (selected = 6 * I + 1) {return true ;} if (selected = 6 * I + 2) {return true ;}} return false ;}// if the complete last row of a page is displayed, and click the down button private boolean isAllPageDown () {// find the total number of pages-1, because the last row of int rawNum = 20/6 is not required to slide down; for (int I = 1; I <= rawNum; I ++) {if (selected = 3 + 6 * (I-1) {return true ;} if (selected = 3 + 6 * (I-1) + 1) {return true;} if (selected = 3 + 6 * (I-1) + 2) {return true ;}} return false;} class MyBaseAdapter extends BaseAdapter {private Context context; ArrayList <Integer> list = new ArrayList <Integer> (); public MyBaseAdapter (Context context) {this. context = context; for (int I = 0; I <20; I ++) {list. add (I) ;}@ Overridepublic int getCount () {return list. size () ;}@ Overridepublic Object getItem (int position) {return null ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = LayoutInflater. from (context ). inflate (R. layout. item_program, parent, false); holder = new ViewHolder (); holder. imageView = (ImageView) convertView. findViewById (R. id. program_image); holder. name = (TextView) convertView. findViewById (R. id. program_name); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. name. setText ("application" + list. get (position); return convertView;} class ViewHolder {ImageView imageView; TextView name ;}}}
The entire code has been uploaded, download path: http://download.csdn.net/detail/harryweasley/8874079
As you can see in this Article, each row has three items, one page and two lines, and one page has six items. What should we do if there are other cases?
Next article, I will summarize, write to adapt to a variety of gridview page layout method, the article link http://blog.csdn.net/harryweasley/article/details/46811981
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.