2014-10-08 3 views
0

버튼을 클릭하여 여러보기를 통해 앞뒤로 이동하고보기간에 왼쪽 또는 오른쪽으로 쓸어 넘기를 원합니다.고스트 효과 : 서로 위에 두 개의보기가 있습니다. 왜?

그래서 여러보기간에 스 와이프하기 위해 ViewPager를 구현하기로 결정했습니다.

레이아웃 XML :

여기 내 코드의

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/white" 
     > 

    <android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/viewPager"/> 

    <ImageView 
      android:id="@+id/apple" 
      android:layout_width="200sp" 
      android:layout_height="150sp" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/apple" 
      android:contentDescription="apple"/> 

    <TextView 
      android:id="@+id/number" 
      android:layout_width="100sp" 
      android:layout_height="55sp" 
      android:layout_marginTop="47dp" 
      android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/> 

    <Button 
      android:id="@+id/save" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/save" 
      android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/> 

    <Button 
      android:id="@+id/ignore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Ignore" 
      android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/> 

    <ImageView 
      android:id="@+id/back_nav_arrow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_action_back" 
      android:contentDescription="back"> 
      </ImageView> 


    <ImageView 
      android:id="@+id/forward_nav_arrow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_action_forward" 
      android:layout_alignParentTop="true" android:layout_alignParentEnd="true" 
      android:contentDescription="forward"> 

    </ImageView> 

</RelativeLayout> 

여기 내 활동입니다 :

public class CollectionPager extends Activity { 

    private PagerAdapter pagerAdapter; 
    ActionBar actionbar; 
    MyAdapter myAdapter; 
    private Context context; 
    private TextView textView; 
    private int currentPage; 
    ViewPager viewPager; 
    int progressChanged = 0; 

    public static final String TAG = "CollectionPager"; 

    public CollectionPager() { 
     context = this; 
    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     super.setContentView(R.layout.collection); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 

     myAdapter = new MyAdapter(); 
     viewPager.setAdapter(myAdapter); 

     ActionBar actionBar = getActionBar(); 
     if (actionBar != null) { 
      actionBar.hide(); 
     } 

     //Initialize the back button and add an onClick event listener to the button 
     final ImageView back_button = (ImageView) findViewById(R.id.back_nav_arrow); 
     back_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       //it doesn't matter if you're already in the first item 
       viewPager.setCurrentItem(viewPager.getCurrentItem() - 1); 
      } 
     }); 

     //Initialize the forward button and add an onClick event listener to the button 
     final ImageView forward_button = (ImageView) findViewById(R.id.forward_nav_arrow); 
     forward_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       //it doesn't matter if you're already in the last item 
       viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); 
      } 
     }); 

     final Button save_button = (Button) findViewById(R.id.save); 
     save_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       //save 
      } 
     }); 

     final Button ignore_button = (Button) findViewById(R.id.ignore); 
     ignore_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

      //ignore 

      } 
     }); 

     //Attach the page change listener inside the activity 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      // This method will be invoked when the current page is scrolled 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

      } 

      //This method will be invoked when a new page becomes selected 
      @Override 
      public void onPageSelected(int position) { 
       //get position 
       currentPage = position; 

      } 

      // Called when the scroll state changes: 
      // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING 
      @Override 
      public void onPageScrollStateChanged(int i) { 

       //get state 

      } 
     }); 

    } 

     private class MyAdapter extends PagerAdapter { 


      int NumberOfPages = 10; 

      LayoutInflater inflater = (LayoutInflater) CollectionPager.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      @Override 
      public int getCount() { 
       return NumberOfPages; 
      } 

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false); 

        ImageView imageView = (ImageView) view 
          .findViewById(R.id.apple); 
        imageView.setImageResource(R.drawable.apple); 
        parent.addView(view,0); 


        return view; 
      } 

      @Override 
      public void destroyItem(ViewGroup parent, int position, Object object) { 
       ((ViewPager) parent).removeView((View) object); 
      } 

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

      @Override 
      public Parcelable saveState() { 
       return null; 
      } 
     } 
    } 

onClickEvent이 감지되었지만 여기에 뷰에 무슨 일이 일어나고 있는지에 대한 스크린 샷입니다. 서로 위에 두보기입니다. 하나의보기가 화면에 고정되고 다른보기는 올바르게 스크롤됩니다.

enter image description here

나는 이런 이유를 모르겠어요. 내 코드에서 이것이 발생하게하는 원인은 무엇입니까?

편집 : 여기에 문제 하이라이트 영상입니다 : https://www.dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0

+0

내가 당신의 4 개 버튼을 저장하여 예 넣고있는 하나의 수평 선형 레이아웃을 가지고 당신을 조언하고 싶습니다 귀하의 주요 활동의 당신의 클릭 리스너를 할당 무시 .... 그래서 에서 아래의 선형 레이아웃을 호출하여 페이지 뷰를 보거나 선형 레이아웃의 맨 아래를 유지하여 뷰 호출기를 넣을 수 있습니다. 이것이 도움이 될 것이라는 희망이 있습니다. –

+0

예를 들어 주시겠습니까? horizontalScrollView를 사용 하시겠습니까? 버튼 위에 부모 레이아웃 안에 뷰 페이지를 포함시켜야합니까? –

답변

1

변경

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

@Override 
public boolean isViewFromObject(View v, Object o) { 
    return parent == object; 
} 

업데이트

에 : 영화를 시청 한 후

문제는 몇 가지를 넣어 정적 웻지를 뷰 페이지 상단에 추가하십시오. 레이아웃이 같은 될 것입니다 의미

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/white" 
    > 

<ImageView 
     android:id="@+id/apple" 
     android:layout_width="200sp" 
     android:layout_height="150sp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/apple" 
     android:contentDescription="apple"/> 

<TextView 
     android:id="@+id/number" 
     android:layout_width="100sp" 
     android:layout_height="55sp" 
     android:layout_marginTop="47dp" 
     android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/> 

<Button 
     android:id="@+id/save" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/save" 
     android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/> 

<Button 
     android:id="@+id/ignore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/Ignore" 
     android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/> 

<ImageView 
     android:id="@+id/back_nav_arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_back" 
     android:contentDescription="back"> 
     </ImageView> 


<ImageView 
     android:id="@+id/forward_nav_arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_forward" 
     android:layout_alignParentTop="true" android:layout_alignParentEnd="true" 
     android:contentDescription="forward"> 

</ImageView> 

:

이것은 당신이 main_activity.xml 당신이 레이아웃 아래 instantiateItem 사용에 setContentView

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/white" 
     > 

    <android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/viewPager"/> 

</RelativeLayout> 

다음 기능하여 activity에 할당해야합니다입니다

주 활동 :

,
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    super.setContentView(R.layout.collection); 
    viewPager = (ViewPager) findViewById(R.id.viewPager); 

    myAdapter = new MyAdapter(); 
    viewPager.setAdapter(myAdapter); 

    ActionBar actionBar = getActionBar(); 
    if (actionBar != null) { 
     actionBar.hide(); 
    } 

    //Attach the page change listener inside the activity 
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     // This method will be invoked when the current page is scrolled 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     //This method will be invoked when a new page becomes selected 
     @Override 
     public void onPageSelected(int position) { 
      //get position 
      currentPage = position; 

     } 

     // Called when the scroll state changes: 
     // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING 
     @Override 
     public void onPageScrollStateChanged(int i) { 

      //get state 

     } 
    }); 

} 

는이 함수

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

       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false); 

       ImageView imageView = (ImageView) view 
         .findViewById(R.id.apple); 
       imageView.setImageResource(R.drawable.apple); 

       final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow); 
       back_button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 

        //it doesn't matter if you're already in the first item 
        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1); 
       } 
       }); 



       parent.addView(view,0); 


       return view; 
     } 
+0

이 경우 상위를 확인할 수 없습니다. return view == object;를 의미합니까? –

+0

Btw, 여전히 문제가 발생했습니다. ( –

+0

조각을 사용하지 않는 이유는 무엇입니까? – mmlooloo

관련 문제