2016-09-29 2 views
-1

이미지를 슬라이드하는 데모 프로젝트를 만들었습니다. 응용 프로그램을 열면 이미지가 자동으로 슬라이드해야합니다. 아래 코드를 게시하고 있습니다.이미지를 자동으로 슬라이드합니다.

activity_page_view (메인 레이아웃)

<?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:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="100pt" 
     android:layout_marginTop="9dp" 
     android:id="@+id/viewpager"/> 
</RelativeLayout> 

PageViewActivity (주요 활동)

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import java.util.ArrayList; 
import java.util.List; 

public class PageViewActivity extends FragmentActivity { 

    MyPageAdapter pageAdapter; 
    ViewPager pager; 

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

     List<Fragment> fragments = getFragments(); 

     pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); 

     pager = (ViewPager)findViewById(R.id.viewpager); 
     pager.setAdapter(pageAdapter); 
    } 


    private List<Fragment> getFragments(){ 
     List<Fragment> fList = new ArrayList<Fragment>(); 
     fList.add(MyFragment.newInstance(1)); 
     fList.add(MyFragment.newInstance(2)); 
     fList.add(MyFragment.newInstance(3)); 
     fList.add(MyFragment.newInstance(4)); 
     fList.add(MyFragment.newInstance(5)); 
     return fList; 
    } 
} 

MyFragment

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import com.bumptech.glide.Glide; 

import java.util.ArrayList; 

public class MyFragment extends Fragment { 

    public static final String IMAGE_URL = "IMAGE_URL"; 
    TextView messageTextView; 
    ImageView imageView; 

    public ArrayList<String> UrlsImage = new ArrayList<String>(); 
    public ArrayList<String> TagLine = new ArrayList<String>(); 

    public static MyFragment newInstance(Integer index) 
    { 
     MyFragment f = new MyFragment(); 
     Bundle bdl = new Bundle(1); 
     bdl.putInt(IMAGE_URL,index); 
     f.setArguments(bdl); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     int index = getArguments().getInt(IMAGE_URL); 



     UrlsImage.add("http://itradar.ir/wp-content/uploads/2016/08/root-android-5.jpg"); 
     UrlsImage.add("http://www.pcwebim.com/wp-content/uploads/2015/12/Android.jpg"); 
     UrlsImage.add("http://www.androidrootguide.com/wp-content/uploads/2014/10/Stock-Android-Wallpapers-Download.jpg"); 
     UrlsImage.add("https://ardroid.com/wp-content/uploads/2011/01/wpaper1294246633612.jpg"); 
     UrlsImage.add("http://www.androidcentral.com/sites/androidcentral.com/files/styles/w550h500/public/wallpapers/black-lloyd-7dk.jpg?itok=bGsIaB2R"); 


     TagLine.add("Android Pirate"); 
     TagLine.add("Android Alien"); 
     TagLine.add("Android Perfect"); 
     TagLine.add("Android Halloween"); 
     TagLine.add("Android Black"); 





     View v = inflater.inflate(R.layout.myfragment_layout, container, false); 
     messageTextView = (TextView)v.findViewById(R.id.textView); 
     imageView = (ImageView) v.findViewById(R.id.imageview); 

     Glide.with(this) 
       .load(UrlsImage.get(index-1)) 
       .into(imageView); 

     messageTextView.setText(TagLine.get(index-1)); 

     return v; 
    } 

} 

MyPageAdapter (사용자 정의 어댑터)

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

import java.util.List; 


public class MyPageAdapter extends FragmentPagerAdapter { 

    private List<Fragment> fragments; 

    public MyPageAdapter(FragmentManager fm,List<Fragment> fragments) { 
     super(fm); 
     this.fragments=fragments; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return this.fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return this.fragments.size(); 
    } 
} 

내가 코드를 변경해야

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:id="@+id/imageview"/> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginBottom="20dp" 
     android:layout_marginLeft="20dp" 
     android:textColor="@color/colorAccent"/> 

</RelativeLayout> 

을 myfragment_layout. 도움이 되길 바랍니다. 미리 감사드립니다.

답변

0

당신은 onCreate()이 같은 핸들러와 타이머를 사용하여 해당을 실행할 수 있습니다

final Handler handler = new Handler(); 

     final Runnable update = new Runnable() { 
      public void run() { 
       if (position == TOTAL_FRAMENT- 1) { 
        position = 0; 
       } else { 
        position++; 
       } 
       pager.setCurrentItem(position, true); 
      } 
     }; 

     new Timer().schedule(new TimerTask() { 
      @Override 
      public void run() { 
       handler.post(update); 
      } 
     }, 100, 1000); 
0

나는 당신이 ViewFlipper에 관심이있을 수 있다고 생각합니다. 기본적으로 애니메이터가있는 FrameLayout입니다. 워드 프로세서

: 가 추가 된 것을 두 개 이상의보기 사이를 애니메이션합니다

간단한 ViewAnimator. 한 번에 하나의 자녀 만 표시됩니다. 이 요청 된 경우, 자동으로 각 어린이 사이에 보통 간격으로 전환 할 수 있습니다.

0

는 단순히 활동에이 방법을 추가/조각

private Runnable update = new Runnable() { 
      public void run() { 
       //get the position from viewPager.addOnPageChangeListener 
       if (position == mAdapter.getCount()- 1) { 
        position = 0; 
       } else { 
        position++; 
       } 
       pager.setCurrentItem(position, true); 
       handler.post(update); 
      } 
     }; 

onCreate 단순히

new Handler().postDelayed(update,1000); 
번 전화
관련 문제