2016-09-29 79 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(); 
    } 
} 

myfragment_layout

<?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> 

我應該在哪裏修改代碼。 希望幫忙。提前感謝。

回答

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