标签:type idt relative 声明 false port bool fse empty
滚动条 viewPager
<android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="250dp"></android.support.v4.view.ViewPager>
class MyAdapter extends PagerAdapter { @Override public int getCount() { return vpMax; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { position = position % lists.size(); ImageView iv = lists.get(position); container.addView(iv); return iv; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } }
<?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.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="250dp"></android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:background="#66000000" android:paddingTop="10dp" android:paddingBottom="10dp"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:gravity="center" android:text="天道酬勤,追梦无疆" android:textColor="#fff" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:gravity="center" android:orientation="horizontal" android:paddingBottom="5dp"></LinearLayout> </RelativeLayout>
private void initDotData() { for (int i = 0; i < lists.size(); i++) { View view = new View(getApplicationContext()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(7, 7); if (i != 0) { params.leftMargin = 7; } view.setLayoutParams(params); view.setBackgroundResource(R.drawable.selctor_dot); ll.addView(view); } }
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { changeUI(position); } @Override public void onPageScrollStateChanged(int state) { } });
private void changeUI(int position) { position = position % lists.size(); View child = ll.getChildAt(position); tv.setText(descs[position]); child.setSelected(true); if (currentSelectDot != null) { currentSelectDot.setSelected(false); } currentSelectDot = child; }
@Override public int getCount() { return vpMax; }
@NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { position = position % lists.size(); ImageView iv = lists.get(position); container.addView(iv); return iv; }
@Override protected void onStart() { handler.sendEmptyMessageDelayed(10, 3000); super.onStart(); }
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { vp.setCurrentItem(vp.getCurrentItem() + 1); handler.sendEmptyMessageDelayed(10, 3000); super.handleMessage(msg); } };
标签:type idt relative 声明 false port bool fse empty
原文地址:https://www.cnblogs.com/nangongyibin/p/10324893.html