switching view viewSwitcher

 

 

By VIewSwitcher switch views. This uses baseAdapter, still do not understand, to remember a note.

< The RelativeLayout
     xmlns: Android = "http://schemas.android.com/apk/res/android" 
    Android: layout_width = "match_parent" 
    Android: layout_height = "match_parent" > 
    <-! Define a ViewSwitcher component -> 
    < viewSwitcher
         Android: the above mentioned id = "@ + the above mentioned id / viewSwitcher" 
        Android: layout_width = "match_parent" 
        Android: layout_height = "match_parent"  /> 
    ! <- defined buttons to scroll on a screen -> 
    < the button
         Android: the above mentioned id = " + the above mentioned id @ / button_prev " 
        Android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:onClick="prev"
        android:text="&lt;" />
    <!-- 定义滚动到下一屏的按钮 -->
    <Button
        android:id="@+id/button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:onClick="next"
        android:text="&gt;" />
</RelativeLayout>
Main interface
Package Penalty for com.example.viewswitcher 

Import android.app.Activity
 Import android.os.Bundle
 Import android.view.ViewGroup
 Import android.graphics.drawable.Drawable
 Import android.view.LayoutInflater
 Import android.view.View
 Import android.widget. * // define a constant for each application program for displaying the number of screen display const Val = 12 is NUMBER_PER_SCREEN
 class the MainActivity: the Activity () 
{ // for all applications stored List collection system Private Val items = the ArrayList <the DataItem> ()
     // recording the program that is currently displayed on the screen several Private var screenNo = -1
    



    
    
    // total number of screens to save the program occupied 
    Private var screenCount: Int = 0
     Private var Switcher: ViewSwitcher =? Null 
    // create an object LayoutInflater 
    Private var inflater so that: LayoutInflater =? Null 
    // The BaseAdapter responsible for the GridView per screen display provided list item 
    Private Val Adapter = Object: BaseAdapter () 
    { 
        the override Fun getCount (): Int 
        { 
            // If you have reached the last screen, and the number of applications not divisible NUMBER_PER_SCREEN 
            return  IF (screenNo == screenCount - 1 && items.size NUMBER_PER_SCREEN%! = 0 ) 
            { 
                // the number of programs last screen shows the number of applications for the remainder of NUMBER_PER_SCREEN
                % items.size NUMBER_PER_SCREEN 
            } the else NUMBER_PER_SCREEN
             // Otherwise, the number of programs displayed per screen is NUMBER_PER_SCREEN 
        } 

        the override the getItem Fun (position: Int): the DataItem 
        { 
            // Calculation of position list item data according screenNo 
            return items [screenNo * + NUMBER_PER_SCREEN position] 
        } 

        the override Fun getItemId (position: Int): Long 
        { 
            return position.toLong () 
        } 

        the override the getView Fun (position: Int, convertView: View ? , parent: the ViewGroup): View 
        { 
            var View= ConvertView
             IF (convertView == null ) 
            { 
                // load the file layout R.layout.labelicon 
                View = inflater so that? .Inflate (R.layout.labelicon, null ) 
            } 
            // Get R.layout.labelicon component layout file ImageView for, and set the icon 
            Val imageView = View? .findViewById <the imageView> (R.id.imageview) 
            imageView ? .setImageDrawable (getItem (position) .drawable)
             // get R.layout.labelicon layout file TextView components, and set the text whom 
            Val View the textView = !!. the findViewById <the TextView> (R.id.textview) 
            textView.text =the getItem (position) .dataName
             return View 
        } 
    } 

    // represents the application within the class 
    class the DataItem (dataName var: var String ?, the drawable:? Drawable ) 

    the override the onCreate Fun (savedInstanceState: the Bundle ? ) 
    { 
        Super .onCreate (savedInstanceState) 
        the setContentView (R.layout.activity_main) 
        inflater so that = LayoutInflater.from ( the this @MainActivity)
         // Create a set of List elements 40, 40 comprising for analog applications 
        for (I in 0..39 ) 
        { 
            Val label = "" + i 
            Val drawableResources.getDrawable = (R.mipmap.ic_launcher, null ) 
            Val Item = the DataItem (label, the drawable) 
            the Items.Add (Item) 
        } 
        // total number of screen occupied by computing applications
         // if the number of applications divisible NUMBER_PER_SCREEN , result of the division is the total number of screens
         // If not divisible, the total number of screens should be combined with the result of division. 1 
        screenCount = IF (items.size% NUMBER_PER_SCREEN == 0) items.size / NUMBER_PER_SCREEN
         the else items.size / NUMBER_PER_SCREEN + 1 
        Switcher = findViewById (R.id.viewSwitcher) 
        Switcher ? .setFactory {
             //Load R.layout.slidelistview component is actually a GridView component 
            ? Inflater so that .inflate (R.layout.slidelistview, null ) 
        } 
        // to display the first screen when the page loads 
        the Next ( null ) 
    } 

    Fun the Next (v: View ? ) 
    { 
        IF (screenNo <screenCount - 1 ) 
        { 
            screenNo ++
             // a component ViewSwitcher display process is animated 
            ? Switcher .setInAnimation ( the this , R.anim.slide_in_right)
             // animate the assembly process ViewSwitcher hide the 
            switcher ?. setOutAnimation ( the this, R.anim.slide_out_left)
             // control next screen will be displayed GridView corresponding Adapter 
            (Switcher? .NextView AS GridView) .adapter = Adapter
             // click the right button to display the next screen
             // after learning gesture detection, can also display the next screen realized by gesture detection 
            switcher .showNext ()?   // 
        } 
    } 

    Fun PREV (V: View) 
    { 
        IF (screenNo> 0 ) 
        { 
            screenNo -
             // display process animate the assembly ViewSwitcher 
            switcher ? .setInAnimation ( the this , android.R.anim.slide_in_left)
             // animate the components of the hidden process ViewSwitcher
            Switcher? .setOutAnimation ( the this , android.R.anim.slide_out_right)
             // control next screen will be displayed GridView corresponding Adapter 
            (Switcher? .nextView AS GridView) .adapter = Adapter
             // click the left button on a display screen, of course, can use gestures
             // after learning gesture detection, can also be implemented on a display screen via gesture detection 
            Switcher? .showPrevious ()   // 
        } 
    } 
}
The main program
<? Xml Version = "1.0" encoding = "UTF-8" ?> 
< The SET xmlns: Android = "http://schemas.android.com/apk/res/android" > 
    ! <- Set dragged out from the left animation 
    android: duration Specifies the duration of the animation -> 
    < Translate
         Android: fromXDelta = "0" 
        Android: toXDelta = "- 100% P" 
        android: duration = "@ Android: Integer / config_mediumAnimTime"  /> 
</ SET >
Left slide animation
<? Xml Version = "1.0" encoding = "UTF-8" ?> 
< The SET xmlns: Android = "http://schemas.android.com/apk/res/android" > 
    ! <- set to come in from the right drag animation 
    android: duration Specifies the duration of the animation   -> 
    < Translate
         Android: fromXDelta = "100% P" 
        Android: toXDelta = "0" 
        android: duration = "@ Android: Integer / config_mediumAnimTime"  /> 
</ SET >
Right slide animation

 

Guess you like

Origin www.cnblogs.com/superxuezhazha/p/11519352.html