我正在尝试使用Java在Android上制作自己的滑块。 在问这个问题之前,我做了很多研究,但找不到任何解决方案。
在API 21上滑动时,我的ViewPager速度太慢,但在API 23或更高版本上效果很好。
这些,但我没有成功:
活动:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_timeline);
//Slider Process
ViewPager slider = findViewById(R.id.vpSlider);
ViewPagerIndicator pagerIndicator = findViewById(R.id.vpiTimeline);
SliderAdapter sliderAdapter = new SliderAdapter(slider.getContext(),listSliderUrl());
slider.setAdapter(sliderAdapter);
slider.setOffscreenPageLimit(10);
pagerIndicator.setupWithViewPager(slider);
ViewPager适配器:
public class SliderAdapter extends PagerAdapter {
private Context context;
private List<Integer> urls;
private LayoutInflater inflater;
public SliderAdapter(Context context, List<Integer> urls) {
this.context = context;
this.urls = urls;
}
@Override
public int getCount() {
return urls.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return (View)object == view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
inflater =
(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
// View view = LayoutInflater.from(context).inflate(R.layout.item_slider,container,false);
View view = inflater.inflate(R.layout.item_slider,container,false);
ImageView iv = view.findViewById(R.id.ivSlider);
// GlideApp.with(context).load(urls.get(position)).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);
iv.setImageResource(urls.get(position));
container.addView(view);
return view;
}
}
Recycler View适配器:
public class RecyclerViewSliderAdapter extends RecyclerView.Adapter {Context context; List<Integer> urls; public RecyclerViewSliderAdapter(Context context, List<Integer> urls) { this.context = context; this.urls = urls; } @NonNull @Override public ViewHolderSlider onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_slider,parent,false); ViewHolderSlider viewHolderSlider = new ViewHolderSlider(layoutView); return viewHolderSlider; } @Override public void onBindViewHolder(@NonNull ViewHolderSlider holder, int position) { holder.bindData(urls.get(position)); } @Override public int getItemCount() { return urls.size(); }
}
class ViewHolderSlider extends RecyclerView.ViewHolder{ImageView ivSlider; public ViewHolderSlider(View itemView) { super(itemView); ivSlider = (ImageView)itemView.findViewById(R.id.ivSlider); } public void bindData(final Integer url){ ivSlider.setImageResource(url); }}
//Here is my item_slider.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <ImageView android:id="@+id/ivSlider" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" android:scaleType="centerCrop" app:layout_constraintBottom_toBottomOf="parent" android:layout_margin="0dp"/> <ImageView android:id="@+id/ivGradient" android:layout_width="match_parent" android:layout_height="111dp" android:layout_margin="0dp" android:src="@mipmap/gradient" android:scaleType="fitXY" app:layout_constraintBottom_toBottomOf="@id/ivSlider" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/tvSliderHeader" android:layout_width="match_parent" android:fontFamily="@font/opensans_light" android:textSize="11sp" android:textColor="@color/yellow" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toTopOf="@id/tvSliderTitle" android:layout_marginBottom="7dp" android:layout_marginStart="32dp" android:text="@string/dunya" /> <TextView android:id="@+id/tvSliderTitle" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="@id/ivSlider" android:layout_marginBottom="26dp" android:layout_marginStart="31dp" android:textColor="@color/white" android:textStyle="bold" android:text="TÜRKİYE'YE KARŞI\nHİSLERİM DEĞİŞTİ" /> </android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
这实际上是内存泄漏,我在ConstraintLayout内使用ViewPager,将ConstraintLayout更改为LinearLayout,问题已解决!如果您遇到这样的麻烦,请尝试更改外部布局。