Android 在cardview inside recycler中制作滑动图像的最佳方法是什么?
我有一个Android 在cardview inside recycler中制作滑动图像的最佳方法是什么?,android,android-viewpager,android-recyclerview,android-cardview,Android,Android Viewpager,Android Recyclerview,Android Cardview,我有一个RecyclerView和cardwiews。我想在这个CardView中滑动图像,就像在OLX应用程序中一样。最好的方法是什么? 我想把viewpager放在cardview里面。可以吗,或者我应该试试别的 我用ViewPager做的,但是看起来太慢了。这是viewpager适配器的一部分 @Override public Object instantiateItem(ViewGroup collection, int position) { LayoutInflater
RecyclerView
和cardwiew
s。我想在这个CardView中滑动图像,就像在OLX应用程序中一样。最好的方法是什么?
我想把viewpager放在cardview里面。可以吗,或者我应该试试别的
我用ViewPager
做的,但是看起来太慢了。这是viewpager适配器的一部分
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.viewpager_custom, collection, false);
collection.addView(layout);
ImageView image = (ImageView) layout.findViewById(R.id.viewPagerImageView);
image.setImageResource(mPics[position]);
return layout;
}
我将在上使用以下库来实现此更改。使用它,您可以在完成该项目后,从
列表视图中横向滑动该项目
请使用以下示例来帮助您:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mItems = new ArrayList<>(30);
for (int i = 0; i < 30; i++) {
mItems.add(String.format("Card number %2d", i));
}
mAdapter = new CardViewAdapter(mItems);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(mRecyclerView,
new SwipeableRecyclerViewTouchListener.SwipeListener() {
@Override
public boolean canSwipe(int position) {
return true;
}
@Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
});
mRecyclerView.addOnItemTouchListener(swipeTouchListener);
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mItems=新阵列列表(30);
对于(int i=0;i<30;i++){
添加(String.format(“卡号%2d”,i));
}
mAdapter=新的CardViewAdapter(mItems);
mRecyclerView=(RecyclerView)findViewById(R.id.recycler\u视图);
mRecyclerView.setLayoutManager(新的LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
SwipeableRecycleServiceWithTouchListener swipeTouchListener=
新的SwipeableRecyclerViewWithTouchListener(mRecyclerView,
新的SwipeableRecycleServiceWtouchListener.SwipeListener(){
@凌驾
公共布尔扫描(整数位置){
返回true;
}
@凌驾
公共无效onDismissedBySwipeLeft(RecyclerView RecyclerView,int[]reverseSortedPositions){
用于(内部位置:反向启动位置){
斜接。移除(位置);
mAdapter.notifyItemRemoved(位置);
}
mAdapter.notifyDataSetChanged();
}
@凌驾
公共无效onDismissedBySwipeRight(RecyclerView RecyclerView,int[]reverseSortedPositions){
用于(内部位置:反向启动位置){
斜接。移除(位置);
mAdapter.notifyItemRemoved(位置);
}
mAdapter.notifyDataSetChanged();
}
});
mRecyclerView.addonimtouchlistener(swipeTouchListener);
希望这有帮助:)你走的路是对的
只需做一件事加载压缩位图而不是未压缩位图。
您正在将位图资源直接设置到imageview。请使用类似毕加索的库
或者使用谷歌的官方资源高效加载大型位图
首先,在活动中复制此方法:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
然后使用此方法解码位图:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
然后按如下方式加载位图:
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.viewpager_custom, collection, false);
collection.addView(layout);
ImageView image = (ImageView) layout.findViewById(R.id.viewPagerImageView);
image.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, reqwidth, reqheight));
return layout;
}
方向是什么?垂直还是水平?您希望滑动是用户交互的(即用户拖动幻灯片)还是自动的?@abforce,是的,应该是用户交互的,或者在ImageView上使用GestureDetector.SimpleGestureListener更快?要更改图像,我建议您尝试此方法