其他分享
首页 > 其他分享> > 第六天

第六天

作者:互联网

ViewPage

viewpage的使用

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private List<String> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //添加数据
        for (int i = 1; i <= 3; i++) {
            datas.add("第" + i + "条数据");
        }
        initView();
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        MyAdapter myAdapter = new MyAdapter(MainActivity.this, datas);
        //设置适配器
        vp.setAdapter(myAdapter);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //设置当前viewpager显示那个页面
                        vp.setCurrentItem(index);
                    }
                });
                index++;
                if (index == datas.size()) {
                    index = 0;
                }

            }
        }, 0, 1000);
        //添加滑动的监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == datas.size() - 1) {
                    timer.cancel();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class MyAdapter extends PagerAdapter {
        private Context context;
        private List<String> data;

        public MyAdapter(Context context, List<String> data) {
            this.context = context;
            this.data = data;
        }

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

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.vp_item, null);
            TextView textView = inflate.findViewById(R.id.text);
            textView.setText(data.get(position));
            container.addView(inflate);
            return inflate;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            // super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }

自动翻页

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FragmentActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/f_ff"
        android:layout_width="wrap_content"
        android:layout_height="500dp"></androidx.viewpager.widget.ViewPager>
</LinearLayout>

activity.java

public class FragmentActivity extends AppCompatActivity {

    private ViewPager fFf;
    private List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        fFf = (ViewPager) findViewById(R.id.f_ff);
        for (int i = 1; i <= 3; i++) {
            WelFragment welFragment = new WelFragment();
            Bundle bundle = new Bundle();
            bundle.putString("name", "第" + i + "条");
            welFragment.setArguments(bundle);
            list.add(welFragment);
        }

        fFf.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

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

fragemnt布局添加id
fragment.java

public class WelFragment extends Fragment {

    private TextView fText;
    private String name;

    public WelFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_wel, container, false);
        fText = (TextView) inflate.findViewById(R.id.f_text);
        //取值
        Bundle arguments = getArguments();
        String name = arguments.getString("name");
        fText.setText(name);
        return inflate;
    }

}

关于FragmentStatePagerAdapter和FragmentPagerAdapter

FragmentStatePagerAdapter适合fragment较多的情况

FragmentPagerAdapter适合fragment较少的情况

weixin_46361828 发布了6 篇原创文章 · 获赞 0 · 访问量 88 私信 关注

标签:container,fragment,private,第六天,Override,inflate,public
来源: https://blog.csdn.net/weixin_46361828/article/details/104522899