gpt4 book ai didi

java - Android - 带滑动功能的 PagerAdapter

转载 作者:行者123 更新时间:2023-11-30 00:35:13 28 4
gpt4 key购买 nike

我的第一个 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);
}
}

输出:

enter image description here

这是一个很好的tutorial关于 PagerAdapter。希望这有助于理解 PagerAdapter 的基本使用。

编码愉快~

关于java - Android - 带滑动功能的 PagerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43530791/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com