2017-04-20 33 views
1

我有一個很大的問題,我的第一個Android應用程序的Android - PagerAdapter與刷卡

目標:

我想更改視圖到ViewPager元素,當我刷卡(左/右)

問題

我寫了一些代碼行,但不工作._。

我報我的代碼:

MainActivity.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); 


    } 
} 

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); 
    } 
} 

Element.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> 

Element.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> 

請幫助我。我越來越瘋狂

+0

在哪裏做了初始化elementArrayList? – FAT

+0

@FerdousAhamed in MainActivity: 'Element element = new Element(「nome1」,「surname1」); Element element1 = new Element(「nome2」,「surname2」); ArrayList elementArrayList = new ArrayList (); elementArrayList.add(element); elementArrayList.add(element1);' – Juantums

+0

發佈您的MainActivity完整代碼 – FAT

回答

1

問題出在您的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); 
    } 
} 

OUTPUT:

enter image description here

這裏是一個很好tutorialPagerAdapter。希望這有助於瞭解PagerAdapter的基本用法。

快樂編碼〜

+0

我明白我的錯誤,我以這種方式嘗試(與ViewGroup),但我的錯誤是在「isViewFromObject」方法,我留下了「返回false」,當然他沒有給我一個結果。非常感謝你,你讓我看到了一個讓我真的很瘋狂的錯誤 – Juantums

+0

你是對的。您也應該在destroyItem()方法中使用container.removeView((View)視圖),否則您的應用程序將在頁面更改期間崩潰。 – FAT

+0

順便說一句,如果我的解決方案可以幫助您找到問題並且看起來很有用,那麼請將此標記爲已接受的答案,並儘可能提供upvote。在此先感謝 – FAT