Activity 中的多个 ViewPager

2023-12-31

我试图在一项活动中实现多个 ViewPager,但它并没有真正起作用。实现这一点的最佳方法是什么。我被困住了!

这是活动

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;

import com.viewpagerindicator.CirclePageIndicator;

public class NewWorkoutActivity extends Activity{

    ViewPager corePager;
    ViewPager timePager;
    ViewPager equipmentPager;
    CirclePageIndicator coreIndicator;
    CirclePageIndicator timeIndicator;
    CirclePageIndicator equipmentIndicator;
    Button startWorkoutButton;

    PagerAdapter coreAdapter;
    PagerAdapter timeAdapter;
    PagerAdapter equipmentAdapter;

     String[] timeSets;
     String[] coreTargets;
     String[] equipments;

     int[] timeImages;
     int[] coreImages;
     int[] equipmentImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_workout);


         coreTargets = new String[] { "Full body", "Core",
                 "Legs", "Upper Body"};

         coreImages = new int[] { R.drawable.button_target_core,  R.drawable.button_target_core2,
                 R.drawable.button_target_core3,  R.drawable.button_target_core4};

         timeSets = new String[] { "15 Minutes", "20 Minutes", 
                 "30 Minutes", "45 Minutes"};

         timeImages = new int[] { R.drawable.button_target_core,  R.drawable.button_target_core2,
                 R.drawable.button_target_core3,  R.drawable.button_target_core4};

          equipments = new String[] { "Rope", "Kette Bell",
                 "Weight", "Hat"};

         equipmentImages = new int[] { R.drawable.button_target_core,  R.drawable.button_target_core2,
                 R.drawable.button_target_core3,  R.drawable.button_target_core4};



        startWorkoutButton=(Button)findViewById(R.id.startWorkoutButton);
        corePager=(ViewPager)findViewById(R.id.corepager);
        timePager=(ViewPager)findViewById(R.id.timepager);
        equipmentPager=(ViewPager)findViewById(R.id.equipmentpager);
        coreIndicator=(CirclePageIndicator)findViewById(R.id.coreindicator);
        timeIndicator=(CirclePageIndicator)findViewById(R.id.timeindicator);
        equipmentIndicator=(CirclePageIndicator)findViewById(R.id.equipmentindicator);

           coreAdapter = new MyViewPagerAdapter(NewWorkoutActivity.this, coreTargets, coreImages);
            // Binds the Adapter to the ViewPager
            corePager.setAdapter(coreAdapter);

            timeAdapter = new MyViewPagerAdapter(NewWorkoutActivity.this, timeSets, timeImages);
            // Binds the Adapter to the ViewPager
            timePager.setAdapter(timeAdapter);

            equipmentAdapter = new MyViewPagerAdapter(NewWorkoutActivity.this, equipments, equipmentImages);
            // Binds the Adapter to the ViewPager
            corePager.setAdapter(coreAdapter);

            // ViewPager Indicators

            coreIndicator.setViewPager(corePager);
            equipmentIndicator.setViewPager(equipmentPager);
            timeIndicator.setViewPager(timePager);

    }

}





and My Adapter





import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    String[] desc;
    int[] image;
    LayoutInflater inflater;

    public MyViewPagerAdapter(Context context, String[] desc,  int[] image) {
        this.context = context;
        this.desc = desc;
        this.image = image;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables
        TextView desctext;
        ImageView vpimage;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.body_page, container,
                false);

        // Locate the TextView in viewpager_item.xml
        desctext = (TextView) itemView.findViewById(R.id.vptext);

        // Capture position and set to the TextViews
        desctext.setText(desc[position]);

        // Locate the ImageView in viewpager_item.xml
        vpimage = (ImageView) itemView.findViewById(R.id.vpimage);
        // Capture position and set to the ImageView
        vpimage.setImageResource(image[position]);

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

logcat 告诉我 ViewPager 没有适配器实例。


这对我有用:

试试这个代码。对它没有太大的改变。

NOTE:我使用的图像只是随机图像..你可以使用你自己的图像。

public class MainActivity extends ActionBarActivity {

    Context context;

    String[] timeSets;
    String[] coreTargets;
    String[] equipments;

    int[] timeImages;
    int[] coreImages;
    int[] equipmentImages;

    MyAdapter adapter1, adapter2, adapter3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        coreTargets = new String[]{"Full body", "Core",
                "Legs", "Upper Body"};

        coreImages = new int[]{R.drawable.ic_launcher, R.drawable.abc_ab_share_pack_holo_dark,
                R.drawable.abc_ab_stacked_solid_dark_holo, R.drawable.abc_ab_bottom_solid_dark_holo};

        timeSets = new String[]{"15 Minutes", "20 Minutes",
                "30 Minutes", "45 Minutes"};

        timeImages = new int[]{R.drawable.abc_ab_share_pack_holo_light, R.drawable.abc_ab_transparent_light_holo,
                R.drawable.abc_ic_ab_back_holo_light, R.drawable.abc_spinner_ab_pressed_holo_light};

        equipments = new String[]{"Rope", "Kette Bell",
                "Weight", "Hat"};

        equipmentImages = new int[]{R.drawable.abc_ic_clear, R.drawable.abc_textfield_searchview_holo_dark,
                R.drawable.abc_spinner_ab_focused_holo_light, R.drawable.abc_ab_stacked_solid_dark_holo};

        ViewPager view1 = (ViewPager) findViewById(R.id.viewpager1);
        ViewPager view2 = (ViewPager) findViewById(R.id.viewpager2);
        ViewPager view3 = (ViewPager) findViewById(R.id.viewpager3);

        adapter1 = new MyAdapter(coreTargets, coreImages);
        adapter2 = new MyAdapter(timeSets, timeImages);
        adapter3 = new MyAdapter(equipments, equipmentImages);

        view1.setAdapter(adapter1);
        view2.setAdapter(adapter2);
        view3.setAdapter(adapter3);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

适配器

    private class MyAdapter extends PagerAdapter {

        String[] desc;
        int[] image;


        public MyAdapter(String[] desc, int[] image) {

            super();
            this.desc = desc;
            this.image = image;


        }

        @SuppressLint("NewApi")
        @Override
        public void finishUpdate(ViewGroup container) {
            // TODO Auto-generated method stub
            super.finishUpdate(container);

        }

        @Override
        public int getCount() {

            return desc.length;

        }

        @Override
        public boolean isViewFromObject(View collection, Object object) {

            return collection == ((View) object);
        }

        @Override
        public Object instantiateItem(View collection, int position) {

            // Inflating layout
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Setting view you want to display as a row element
            View view = inflater.inflate(R.layout.items, null);

            TextView itemText = (TextView) view.findViewById(R.id.textViewMain);

            ImageView imageView = (ImageView) view.findViewById(R.id.imageViewmain);


            try {

                itemText.setText(desc[position]);

                imageView.setImageResource(image[position]);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ((ViewPager) collection).addView(view, 0);
            return view;

        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);

        }

    }
}

The layout看起来像这样:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mike.app.MainActivity$PlaceholderFragment"
    android:background="#ffc42823">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:id="@+id/viewpager1"
            android:background="#333333"
            />

        <View
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="#FFFFFF"></View>

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:id="@+id/viewpager2"
            android:background="#333333" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="#FFFFFF">></View>

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:id="@+id/viewpager3"
            android:background="#333333" />

    </LinearLayout>

</ScrollView>

项目布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mike.app.MainActivity$PlaceholderFragment"
    android:background="#ff10c7c6"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imageViewmain" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:text="Some Text"
        android:textSize="25sp"
        android:id="@+id/textViewMain"
        android:gravity="center" />

</LinearLayout>

这是你想要的 ?如果是的话,这对我有用......让我知道这是否有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Activity 中的多个 ViewPager 的相关文章

随机推荐

  • AttributeError:模块“pandas”没有属性“to_csv”

    我从 csv 文件中取出了一些行 如下所示 pd DataFrame CV data take 5 columns CV data columns 并对其执行一些功能 现在我想再次将其保存在 csv 中 但出现错误module pandas
  • 如何更改 shell/bash 中的列名称?

    这个问题对于大多数人来说可能太简单了 但我真的不想手动做 所以 假设我在终端中有一个文件 如下所示 dz2t edison s GWAS head PHENO GWASID CHILDID VSTNUM GENDER GA 1001 100
  • 使用 ng-repeat 生成的项目中的增量计数器

    我用 ng repeat 生成了一个列表 其中每个项目都有一个计数变量 在列表项中 我有一个链接 我想在单击它时增加计数 我不知道如何用 Angular JS 方式解决这个问题 Plunker http plnkr co edit HfeL
  • Android googlemaps v2完成加载事件或回调

    我想在谷歌地图加载后做一些事情 maptiles已被填充 无论如何可以实现这一点吗 正如所指出的qubz https stackoverflow com users 496625 qubz ViewTreeObserver可用于实现地图加载
  • Angular2 检测模板视图中的元素是否具有类

    我们使用 bootstrap 有时它会自动向 DOM 元素添加类 附加到这些元素并检测何时将特定 css 类添加到组件模板子元素的最佳方法是什么 假设我有这个组件 import Component ViewChild ElementRef
  • 防止默认事件操作不起作用...?

    I m trying to add keyboard shortcuts on my website to make fast navigation possible using the keyboard I m running into
  • JPA本机查询返回类

    在 JPA 中 我定义了一个本机 sql 它将返回 String NamedNativeQuery name alert query select distinct c accountId from account c resultClas
  • Flutter Firestore 服务器端时间戳

    我需要在使用 Flutter 应用程序添加到 Firestore 的新文档上添加服务器端时间戳 我明白我应该使用FieldValue serverTimestamp但我不知道在哪里可以找到这个 截至9月5日 更新cloud firestor
  • 如何在 Tkinter 中使用 Canvas 绘制点?

    我想在Tkinter中画一个点 现在我正在使用Canvas做到了 但我没有找到这样的方法来画点Canvas class Canvas提供了一个方法叫做crete line x1 y1 x2 y2 所以我尝试设置x1 x2 y1 y2画一个点
  • 自动检查父复选框

    我有一个 AJAX 请求 该请求会拉入一个文件夹列表 每个列表项旁边都有一个复选框 如果我选中父文件夹旁边的复选框 则会通过执行以下操作自动检查所有子文件夹 var checkChildCheckBoxes function var isA
  • 容器启动后在 Docker 上运行 mongorestore

    我正在尝试设置一个运行 MongoDB 的容器 该容器在启动时使用 mongorestore 填充数据 这个想法是快速建立一个用于测试和模拟的虚拟数据库 我的 Dockerfile 如下所示 FROM mongo bionic COPY d
  • 在 Eclipse Neon 中启用 JSDT 大纲视图

    显示 JavaScript 源文件内容的大纲视图问题并不新鲜 而且它甚至找到了答案 https stackoverflow com a 11062205 2749913 然而 这个答案在 Neon 4 6 0 中不再有效 我打开了在两个单独
  • Delphi增量变化?从 D7 到 D2009

    是否有网站或文档详细说明每个版本所做的更改 比如说 D7 gt D2005 gt D2006 gt D2007 gt D2009 语言特点 数据库功能 RTTI IDE 必须查看 Google 缓存才能找到其中的几个 Delphi 2005
  • Nvidia 驱动程序更新后无法在 Qt 5.3 上编译 GLSL 着色器

    我有一些非常基本的 openGL 着色器 用于编译 但在 Nvidia 驱动程序更新后不再编译 每个着色器都以以下行开头 版本150核心 我曾经使用 QGLShaderProgram addShaderFromSourceFile 方法编译
  • 简单的 C++ 继承示例,有什么问题吗? [复制]

    这个问题在这里已经有答案了 可能的重复 派生类中具有相同名称但不同签名的函数 https stackoverflow com questions 411103 function with same name but different si
  • 错误:验证失败:可成像的图像必须存在,rails-5.0,paperclip-5

    当我尝试提交表单时 发生以下错误 Validation failed Images imageable must exist并渲染相同的new html erb view 如果我评论file field in new html erb 产品
  • 如何清除 System.Net 客户端 DNS 缓存?

    我在更改 HOSTS 文件时使用 NET WebRequest 我发现 System Net 不遵守这些更改 我怎样才能让它这样做 我有许多服务器在单个主机名后面进行负载平衡 比方说 example com 我想单独定位其中的几个 因此我的
  • 如何为每个 CSV 文件创建单独的 Pandas DataFrame 并给它们指定有意义的名称?

    我已经彻底搜索过 但找不到我在这个问题上寻找的指导 所以我希望这个问题不是多余的 我有几个代表光栅图像的 csv 文件 我想对它们进行一些统计分析 所以我尝试为每个文件创建一个 Pandas 数据框 这样我就可以将它们切片并绘制它们 但我在
  • 我应该使用shared_ptr还是unique_ptr

    我一直在使用 pimpl 习惯用法制作一些对象 但我不确定是否使用std shared ptr http en cppreference com w cpp memory shared ptr or std unique ptr http
  • Activity 中的多个 ViewPager

    我试图在一项活动中实现多个 ViewPager 但它并没有真正起作用 实现这一点的最佳方法是什么 我被困住了 这是活动 import android app Activity import android os Bundle import