- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的第一个 Android 应用程序有一个大问题
目标:
我想在滑动(左/右)时将 View 更改为 ViewPager 元素
问题
我写了一些代码行,但是没有用._。
我报告我的代码:
主 Activity .java
package com.example.macbookpro.myapplication;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Element element=new Element("nome1","surname1");
Element element1=new Element("nome2","surname2");
ArrayList<Element> elementArrayList=new ArrayList<Element>();
elementArrayList.add(element);
elementArrayList.add(element1);
ViewPager viewPager=(ViewPager) findViewById(R.id.swipeView);
ElementAdapter elementAdapter=new ElementAdapter(elementArrayList,this);
viewPager.setAdapter(elementAdapter);
}
}
元素适配器.java
public class ElementAdapter extends PagerAdapter {
private ArrayList<Element> element=new ArrayList<Element>();
private Context ctx;
public ElementAdapter(ArrayList<Element> element, Context ctx) {
this.element = element;
this.ctx = ctx;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = inflater.inflate(R.layout.element,container,false);
TextView nome = (TextView) item_view.findViewById(R.id.nome);
TextView surname= (TextView) item_view.findViewById(R.id.surname);
nome.setText(element.get(position).getNome());
surname.setText(element.get(position).getSurname());
container.addView(item_view);
return item_view;
}
@Override
public int getCount() {
return this.element.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
元素.java
public class Element {
private String nome;
private String surname;
public Element(String nome, String surname) {
this.nome = nome;
this.surname = surname;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.macbookpro.myapplication.MainActivity">
<View
android:id="@+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<android.support.v4.view.ViewPager
android:id="@+id/swipeView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_alignBottom="@id/centerShim5"
android:visibility="visible" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/button" />
</RelativeLayout>
元素.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView
android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="20dp"
android:layout_alignLeft="@+id/surname"
android:layout_alignStart="@+id/surname"
android:layout_above="@+id/surname" />
<TextView
android:id="@+id/surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp"
android:textStyle="normal|bold|italic"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
请帮帮我。我快疯了
最佳答案
问题出在您的 ElementAdapter
类中。
更新 ElementAdapter
如下:
public class ElementAdapter extends PagerAdapter {
private ArrayList<Element> element = new ArrayList<Element>();
private Context ctx;
public ElementAdapter(ArrayList<Element> element, Context ctx) {
this.element = element;
this.ctx = ctx;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Use ViewGroup
ViewGroup item_view = (ViewGroup) inflater.inflate(R.layout.element, container ,false);
TextView nome = (TextView) item_view.findViewById(R.id.nome);
TextView surname= (TextView) item_view.findViewById(R.id.surname);
nome.setText(element.get(position).getNome());
surname.setText(element.get(position).getSurname());
container.addView(item_view);
return item_view;
}
@Override
public int getCount() {
return this.element.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
// Remove specific view
container.removeView((View) view);
}
}
输出:
这是一个很好的tutorial关于 PagerAdapter
。希望这有助于理解 PagerAdapter
的基本使用。
编码愉快~
关于java - Android - 带滑动功能的 PagerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43530791/
我的 Activity 中有一个 ViewPager。 ViewPager 在滑动寻呼机时显示不同的值。但是我得到一个 IllegalStateException: PagerAdapter chan
我正在使用带有 ActionBar 选项卡的 ViewPager 示例,该标签取自 Android 文档 here . 不幸的是,当我调用 addTab 方法时,应用程序崩溃并出现以下异常: Ille
我正在构建一个简单的图库应用。图片与服务器同步。现在我有一个简单的 gridView 显示图像,onClick 图像在 viewPager 中打开。就像 android 默认图库一样。 问题是- ja
我正在尝试使用静态类将值传递给 View ,而不是使用 intent,因为我必须传递大量数据。有时我会得到这个错误,但找不到主要原因是什么 错误:- java.lang.IllegalStateExc
我正在为我的应用程序使用 UniversalImageLoader。我的 ImageAdapter 扩展了 PagerAdapter。我需要 currentImage 作为位图。通过回调方法 publ
我有一个 ViewPager 和一个 PagerAdapter,它从远程服务器(通过它们的 ID)下载一些图像并显示它们。它几乎完美地工作,除了从第一页滑动到第二页时有点慢(比如 1 秒),而之后的所
我做了一个 PagerAdapter,它工作得很好,但是当我 Handlebars 机水平放置时,很多像 TextView 这样的东西是不可见的,因为它们在“下方”手机屏幕。我知道我可以放一个 Scr
我正在尝试制作一个图像寻呼机,看起来我已经根据我查阅的所有教程做对了,但我得到的只是一个空白屏幕。适配器的 instantiateItem 方法上的断点告诉我它正在被调用并且所有正确的信息都被设置到
我尝试从 firebase 数据库加载数据库并得到这个异常。它是关于 notifyDataSetChanged 的。我试图在 SlideShowAapter 中放入 getCount() 但它没有用。
我想弄清楚,下面的事情是如何用 android UI 完成的。我不认为我是第一个遇到这个问题的人,但我不知道要搜索什么,也没有找到任何有用的信息。 我的情况: 目前我有一个扩展 FragmentSta
我正在开发一个包含 ViewPager View 的应用程序, 我创建了一个 PagerAdapter,它具有 View ,PagerAdapter 的 instantiateItem() 方法在 c
我正在尝试开发一个应用程序,它将显示数字 1-10 以及该数字的声音。 使用 mobile.tutsplus 中的示例进行水平滑动,我已经达到了所有数字都正确显示但是当我们从 2 返回时 1 的声音不
我想获取我的 PagerAdapter 可见 View 的当前位置 我没有看到像 getPosition() 这样明显的函数,我想要一个。 我想在那个位置将一个对象添加到它的数组列表中,但我需要先知道
我正在使用以下示例来暗示我的 viewPager: http://code.google.com/p/viewpagerexample/issues/list 这个例子的问题是我不知道如何设置我的起始
我想使用 PagerAdapter 扩展来循环我的 Viewpager 和 Im。我在网上找到的所有东西都是 Fragments 的一个例子。 我想用普通的页面适配器解决这个问题。因此,当我从第一个
所以我一直在努力解决这个问题。我是一个非常新的 android 开发人员,所以理解一些概念对我来说仍然非常困难。 在我的布局中,我使用 PagerAdapter 在页面之间水平滑动。我想在着陆布局中有
下面的代码适用于我的 viewpager 适配器。 import android.content.Context; import android.support.v4.view.Pag
我正在尝试在 PagerAdapter 中加载 Mapfragment。这是适配器代码, public class ShopPagerAdapter extends PagerAdapter {
我正在尝试构建一个寻呼机来按顺序显示图像。但是我收到了这个错误。 The application's PagerAdapter changed the adapter's contents witho
我的第一个 Android 应用程序有一个大问题 目标: 我想在滑动(左/右)时将 View 更改为 ViewPager 元素 问题 我写了一些代码行,但是没有用._。 我报告我的代码: 主 Acti
我是一名优秀的程序员,十分优秀!