📜  Android中的AdapterViewFlipper与示例

📅  最后修改于: 2021-05-08 19:23:24             🧑  作者: Mango

AdapterViewFlipper类是ViewAnimator类的子类,用于在2个或更多视图之间切换,从而一次仅显示一个视图。它通常用于幻灯片中。它是过渡小部件的一个元素,可帮助在视图上添加过渡。对屏幕上的视图设置动画主要很有用。 AdapterViewFlipper在两个或多个视图(TextView,ImageView或任何布局)之间平滑切换,因此提供了一种通过适当的动画从一个视图转换到另一个视图的方法。以下是AdapterViewFlipper的预览示例。

AdapterViewFlipper

ViewFlipper和AdapterViewFlipper之间的区别

ViewFlipper和AdapterViewFlipper都是ViewAnimator的子类。 ViewFlipper最初用于显示所有固定的幻灯片视图。这意味着视图将不会被回收。 AdapterViewFlipper使用适配器填充数据(类似于ListView / Spinner / RecyclerView等),因此可以即时确定子项,并且可以回收表示子项的视图。因此,AdapterViewFlipper用于显示所有子视图。因此,有空间回收视图和动态加载视图。

创建AdapterViewFlipper的步骤

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

单击Res- >布局-> activity_main.xml,然后添加TextView以显示文本,并添加AdapterViewFlipper以显示功能。以下是activity_main.xml文件的完整代码。  

XML


  
    
    
  
    
    
    
  


XML



      
    
    
  
    
    
      


Java
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    AdapterViewFlipper adapterViewFlipper;
  
    int[] IMAGES = {
            R.drawable.ldeosai,
            R.drawable.lakedudipatsar,
            R.drawable.lrama,
            R.drawable.lakekachura
    };
  
    String[] NAMES = {
            "Deosai National Park",
            "Lake Dudipatsar",
            "Rama Meadows",
            "Lower Kachura Lake"
    };
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Link those objects with their respective id's
        // that we have given in .XML file
        adapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.adapterViewFlipper);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), NAMES, IMAGES);
        adapterViewFlipper.setAdapter(customAdapter);
        adapterViewFlipper.setFlipInterval(2600);
        adapterViewFlipper.setAutoStart(true);
    }
}
  
class CustomAdapter extends BaseAdapter {
    Context context;
    int[] images;
    String[] names;
    LayoutInflater inflater;
  
    public CustomAdapter(Context applicationContext, String[] names, int[] images) {
        this.context = applicationContext;
        this.images = images;
        this.names = names;
        inflater = (LayoutInflater.from(applicationContext));
    }
  
    @Override
    public int getCount() {
        return names.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 = inflater.inflate(R.layout.custom_adapter_layout, null);
  
        // Link those objects with their respective id's
        // that we have given in .XML file
        TextView name = (TextView) view.findViewById(R.id.name);
        ImageView image = (ImageView) view.findViewById(R.id.image);
  
        // Set the data in text view
        name.setText(names[position]);
          
        // Set the image in Image view
        image.setImageResource(images[position]);
        return view;
    }
}


步骤3:创建另一个布局文件

现在,通过右键单击res-> layout-> new- > Layout Resource File来创建另一个XML布局文件,并将其命名为custom_adapter_layout.xml 。在此文件中,添加一个ImageView和TextView以在适配器中使用它。以下是custom_adapter_layout.xml文件的完整代码。  

XML格式




      
    
    
  
    
    
      

步骤4:使用MainActivity。 Java文件

打开MainActivity并添加以下代码以启动AdapterViewFlipper。首先创建两个数组,一个用于图像,另一个用于名称。创建后,设置适配器以将数据填充到视图中。最后,设置自动启动和翻转间隔时间,以便AdapterViewFlipper在视图和当前视图之间切换,并且在给定的时间间隔后进入下一个视图。以下是MainActivity的完整代码。 Java文件。请参阅代码内的注释以了解代码。

Java

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    AdapterViewFlipper adapterViewFlipper;
  
    int[] IMAGES = {
            R.drawable.ldeosai,
            R.drawable.lakedudipatsar,
            R.drawable.lrama,
            R.drawable.lakekachura
    };
  
    String[] NAMES = {
            "Deosai National Park",
            "Lake Dudipatsar",
            "Rama Meadows",
            "Lower Kachura Lake"
    };
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Link those objects with their respective id's
        // that we have given in .XML file
        adapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.adapterViewFlipper);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), NAMES, IMAGES);
        adapterViewFlipper.setAdapter(customAdapter);
        adapterViewFlipper.setFlipInterval(2600);
        adapterViewFlipper.setAutoStart(true);
    }
}
  
class CustomAdapter extends BaseAdapter {
    Context context;
    int[] images;
    String[] names;
    LayoutInflater inflater;
  
    public CustomAdapter(Context applicationContext, String[] names, int[] images) {
        this.context = applicationContext;
        this.images = images;
        this.names = names;
        inflater = (LayoutInflater.from(applicationContext));
    }
  
    @Override
    public int getCount() {
        return names.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 = inflater.inflate(R.layout.custom_adapter_layout, null);
  
        // Link those objects with their respective id's
        // that we have given in .XML file
        TextView name = (TextView) view.findViewById(R.id.name);
        ImageView image = (ImageView) view.findViewById(R.id.image);
  
        // Set the data in text view
        name.setText(names[position]);
          
        // Set the image in Image view
        image.setImageResource(images[position]);
        return view;
    }
}

输出:在模拟器上运行

现在,使用USB电缆或仿真器连接设备并启动应用程序。您会看到将显示图像的Adaptiveflipping,该变化将在一定毫秒后发生变化。

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!