什么是ViewFlipper?
ViewFlipper是Android中一个非常实用的控件,它可以让用户在多个视图之间进行切换,ViewFlipper主要用于实现页面之间的滑动切换,例如轮播图、选项卡等场景,ViewFlipper是Android开源项目lib-flip库的一部分,使用这个库可以方便地实现ViewFlipper的功能。
如何创建一个ViewFlipper?
1、在项目的build.gradle文件中添加依赖:
dependencies { implementation 'com.android.support:recyclerview-v7:28.0.0' }
2、在布局文件中添加ViewFlipper控件:
<android.support.v7.widget.ViewFlipper android:id="@+id/view_flipper" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.ViewFlipper>
3、在Activity或Fragment中初始化ViewFlipper,并设置适配器:
import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mData = {"A", "B", "C", "D", "E"}; private int mSelected = 0; public static class ViewHolder extends RecyclerView.ViewHolder { public CardView cardView; public ImageView imageView; public TextView textView; public ViewHolder(View itemView) { super(itemView); cardView = itemView.findViewById(R.id.card_view); imageView = itemView.findViewById(R.id.image_view); textView = itemView.findViewById(R.id.text_view); } } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { if (position == mSelected) { holder.cardView.setBackgroundColor(Color.parseColor("FF4081")); } else { holder.cardView.setBackgroundColor(Color.TRANSPARENT); } holder.imageView.setImageResource(mData[position]); holder.textView.setText(mData[position]); } @Override public int getItemCount() { return mData.length; } public void setSelectedPosition(int position) { mSelected = position; notifyDataSetChanged(); } }
4、在Activity或Fragment中设置ViewFlipper的适配器和监听器:
MyAdapter adapter = new MyAdapter(); adapter.setSelectedPosition(0); // 默认选中第一个视图 ViewFlipper viewFlipper = findViewById(R.id.view_flipper); viewFlipper.setAdapter(adapter); // 为ViewFlipper设置适配器 viewFlipper.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { // 为ViewFlipper添加监听器,当页面切换时更新背景颜色和文字颜色等样式变化效果,如果不需要样式变化效果,可以不添加监听器。@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) {} @Override public void onPageScrollStateChanged(int state) {} }); // 如果需要在页面选中时更新UI,可以在onPageSelected方法中调用adapter的setSelectedPosition方法更新选中位置,adapter.setSelectedPosition(position); // 注意:这里的位置参数是从0开始的,即第一个视图对应位置为0,第二个视图对应位置为1,依此类推,private boolean isFirstPage = true; private int currentPosition = 0; private void updateUi() { if (isFirstPage) { // 如果是第一次进入该页面,将当前视图设为选中状态 isFirstPage = false; adapter.setSelectedPosition(currentPosition); currentPosition++; if (currentPosition >= adapter.getItemCount()) currentPosition = adapter.getItemCount() 1; viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.animator)); viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.animator)); viewFlipper.showNext(); return; } viewFlipper.showPrevious(); currentPosition--; if (currentPosition <= 0) currentPosition = 1; adapter.setSelectedPosition(currentPosition); viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.animator)); viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.animator)); viewFlipper.showNext(); // 注意:在每次页面切换后都需要调用showPrevious()和showNext()方法来更新显示的视图,如果只需要切换到指定的两个视图,而不需要平滑过渡效果,可以将这两个方法移除。// private void switchToTwoPages() { adapter = new MyAdapter(); adapter.setSelectedPosition(0); viewFlipper = findViewById(R.id.view_flipper); viewFlipper.setAdapter(adapter); viewFlipper
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/189286.html