Open top menu


Example: How to implement AdapterViewFlipper In android.And why we are used?


Simple ViewAnimator that will Aniamte between two or moreViews View that have been added to it.Only one Child is shown at a time.


activity_main.xml
....................................................................... 

xml version="1.0" encoding="utf-8"
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
tools:context="com.project.example.androidbeginnerpoint.MainActivity"



  
 >      
android:layout_width="match_parent"       
android:layout_height="match_parent"       
android:id="@+id/simpleAdapterViewFlipper" 

 </LinearLayout>
............................................ 


row_item.xml

............................................ 

xml version="1.0" encoding="utf-8"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="wrap_content"    
android:layout_height="wrap_content"    
android:background="#fff"    
android:gravity="center"    
android:orientation="vertical"

<ImageView        

android:id="@+id/fruitImage"        
android:layout_width="wrap_content"        
android:layout_height="150dp"       
 android:layout_gravity="center" />

 <TextView        
android:id="@+id/fruitName"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_gravity="center"        
android:layout_marginTop="2dp"        
android:textColor="#000" />

</LinearLayout>
.................................... 




MainActivity.Java

..................................... 

package com.project.example.androidbeginnerpoint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterViewFlipper;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private AdapterViewFlipper simpleAdapterViewFlipper;
    int[] fruitImages = {R.drawable.a, R.drawable.ga, R.drawable.s};     
    String fruitNames[] = {"Apple", "Green Apple", "Litchi"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleAdapterViewFlipper = (AdapterViewFlipper) 
findViewById(R.id.simpleAdapterViewFlipper);  
                 AdapterViewFlip customAdapter = new AdapterViewFlip
(getApplicationContext(), fruitNames, fruitImages);
        simpleAdapterViewFlipper.setAdapter(customAdapter);  
                simpleAdapterViewFlipper.setFlipInterval(3000);
        
        simpleAdapterViewFlipper.setAutoStart(true);
    }
}
............................................... 



AdapterViewFlip.Java
................................. 


package com.project.example.androidbeginnerpoint;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class AdapterViewFlip extends BaseAdapter {
    Context context;
    int[] fruitImages;
    String[] fruitNames;
    LayoutInflater inflter;

    public AdapterViewFlip(Context applicationContext, String[] fruitNames,
 int[] fruitImages) {
        this.context = applicationContext;
        this.fruitImages = fruitImages;
        this.fruitNames = fruitNames;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override    public int getCount() {
        return fruitNames.length;
    }

    @Override    public Object getItem(int position) {
        return null;
    }

    @Override    public long getItemId(int position) {
        return 0;
    }

    @Override    public View getView(int position, View view, ViewGroup parent) {
        view = inflter.inflate(R.layout.row_item, null);
        TextView fruitName = (TextView) view.findViewById(R.id.fruitName);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruitImage);
        fruitName.setText(fruitNames[position]);
        fruitImage.setImageResource(fruitImages[position]);
        return view;
    }
}

.................................... 






0 comments