我创建了一个幻灯片图像的演示项目。打开我的应用程序后,我需要自动滑动图像。我在下面发布我的代码。
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 :(得分: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);
答案 1 :(得分:0)
我认为您可能对ViewFlipper感兴趣。它基本上是一个带动画师的FrameLayout。
来自文档:
简单的ViewAnimator,可以在两个或多个视图之间设置动画 已添加到它。一次只能展示一个孩子。如果 要求,可以在每个孩子之间自动翻转 间隔。
答案 2 :(得分: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);