我正在努力实现类似于this的效果。我环顾四周,找到了与此类似的代码
ViewPagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private ArrayList<SomeObject> someObjectList;
public ViewPagerAdapter(Context context, ArrayList<SomeObject> someObjectList) {
mContext = context;
this.someObjectList = someObjectList;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return someObjectList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View itemvView = mLayoutInflater.inflate(R.layout.content_main, container, false);
SomeObject object = someObjectList.get(position);
TextView tvTitle = itemvView.findViewById(R.id.textView);
tvTitle.setText(object.getTitle());
ImageView image = itemvView.findViewById(R.id.imageView);
image.setImageResource(object.getImageResource());
container.addView(itemvView);
return itemvView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
public void addItems(List<SomeObject> someObject) {
// TODO : Add objects dynamically without using notifyDataSetChanged()
}
}
我还修改了自定义View Pager组件以满足我的需求
自定义视图寻呼机
class VerticalViewPager : ViewPager {
private val MIN_SCALE = 0.75f
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
private fun init() {
// The majority of the magic happens here
setPageTransformer(true, VerticalPageTransformer())
// The easiest way to get rid of the overscroll drawing that happens on the left and right
overScrollMode = View.OVER_SCROLL_NEVER
}
private inner class VerticalPageTransformer : ViewPager.PageTransformer {
override fun transformPage(view: View, position: Float) {
when {
position < -1 -> // [-Infinity,-1)
// This page is way off-screen to the left.
view.alpha = 0f
position <= 0 -> { // [-1,0]
// Use the default slide transition when moving to the left page
view.alpha = 1f
// Counteract the default slide transition
view.translationX = view.width * -position
//set Y position to swipe in from top
val yPosition = position * view.height
view.translationY = yPosition
view.scaleX = 1f
view.scaleY = 1f
}
position <= 1 -> { // [0,1]
view.alpha = 1f
// Counteract the default slide transition
view.translationX = view.width * -position
// Scale the page down (between MIN_SCALE and 1)
val scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position))
view.scaleX = scaleFactor
view.scaleY = scaleFactor
}
else -> // (1,+Infinity]
// This page is way off-screen to the right.
view.alpha = 0f
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private fun swapXY(ev: MotionEvent): MotionEvent {
val width = width.toFloat()
val height = height.toFloat()
val newX = ev.y / height * width
val newY = ev.x / width * height
ev.setLocation(newX, newY)
return ev
}
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
val intercepted = super.onInterceptTouchEvent(swapXY(ev))
swapXY(ev) // return touch coordinates to original reference frame for any child views
return intercepted
}
override fun onTouchEvent(ev: MotionEvent): Boolean {
return super.onTouchEvent(swapXY(ev))
}
}
我在main_activity.xml中插入了自定义ViewPager视图,并在MainActivity.java中设置了适配器..
我需要动态无限地添加卡片项目。即数据将在用户滚动卡片堆栈时加载,并显示无限卡片。
因此,当卡片向下滚动时,我需要动态地向视图寻呼机添加对象。
正如我用Google搜索,向视图寻呼机添加更多数据的唯一方法是使用notifyDataSetChanged方法。但我不希望这样,因为随着数据量的增加它变得非常低效。&#39;
如何动态地向视图寻呼机添加更多数据?
我找到了通知活动何时加载的方法。我只需要弄清楚如何在不使用notifyDataSetChanged()的情况下设置数据。