时间:2023-09-06 08:12:02 | 来源:网站运营
时间:2023-09-06 08:12:02 来源:网站运营
直播视频网站源码,使用ViewPager实现基本的翻页效果:直播视频网站源码,使用ViewPager实现基本的翻页效果实现的相关代码```handlebars<?xml version="1.0" encoding="utf-8"?><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" tools:context=".ViewPagerActivity"> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>```
1.1.viewpager1.xml```handlebars<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#CF1717" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个layout" android:textSize="20sp" /></LinearLayout>```
另外两个xml同理```handlebarspublic class ViewPagerActivity extends AppCompatActivity { private ViewPager view_pager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); view_pager = findViewById(R.id.view_pager); View inflate1 = LayoutInflater.from(this).inflate(R.layout.viewpager1, null); View inflate2 = LayoutInflater.from(this).inflate(R.layout.viewpager2, null); View inflate3 = LayoutInflater.from(this).inflate(R.layout.viewpager3, null); List<View> viewList = new ArrayList<>(); viewList.add(inflate1); viewList.add(inflate2); viewList.add(inflate3); MyAdapter myAdapter = new MyAdapter(viewList); view_pager.setAdapter(myAdapter); }} ```
2.1.适配器MyAdapter代码如下:```handlebarspublic class MyAdapter extends PagerAdapter { private List<View> viewList; public MyAdapter(List<View> viewList) { this.viewList = viewList; } //获取多少View @Override public int getCount() { return viewList.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { container.addView(viewList.get(position), 0); return viewList.get(position); } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView(viewList.get(position)); }}```
关键词:基本,实现,效果,使用,视频,直播