我将ViewPager与PagerAdapter一起用于图片。代码在这里
PagerAdapter
public class MediaPagerAdapter extends PagerAdapter {
private final LayoutInflater mLayoutInflater;
private List<String> mediaItems = new ArrayList<>();
private Context context;
public MediaPagerAdapter(List<String> mediaItems,
Context context) {
this.mediaItems = mediaItems;
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mediaItems != null ? mediaItems.size() : 0;
}
@Override
public View instantiateItem(ViewGroup container, final int position) {
View view = mLayoutInflater.inflate(R.layout.item, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
Picasso.with(context).load(mediaItems.get(position)).placeholder(R.drawable
.placeholder2).into(imageView);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
它在寻呼机中成功加载了2个项目。但是当我滚动到第三个项目时它变得空白。没有内容显示在那里,当我回去时,之前的项目也被破坏了。 我研究了很多,但没有找到任何解决方案。如果有人也遇到这种问题并解决了,请在此处提出解决方案。 谢谢,
编辑:2016年5月18日
我使用Swipe-Desk Library在卡片上工作,并在每张卡片中为ViewPager充气照片。代码在这里
主xml文件
<?xml version="1.0" encoding="utf-8"?>
<com.daprlabs.cardstack.SwipeFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipedeck="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.daprlabs.cardstack.SwipeDeck
android:id="@+id/swipe_deck"
android:layout_width="match_parent"
android:layout_height="480dp"
android:padding="20dp"
swipedeck:card_spacing="10dp"
swipedeck:max_visible="3"
swipedeck:render_above="true"
swipedeck:rotation_degrees="15" />
</com.daprlabs.cardstack.SwipeFrameLayout>
SwipeDeckAdapter
public class SwipeDeckAdapter extends BaseArrayAdapter {
private Context context;
public SwipeDeckAdapter(List<String> data, Context context) {
super(context, R.layout.card_layout, data);
this.context = context;
}
@Override
public View newView(Context context, int position, ViewGroup parent) {
View v = getLayoutInflater().inflate(getLayoutResId(), parent, false);
ViewHolder viewHolder = new ViewHolder(v, listener);
v.setTag(viewHolder);
return v;
}
@Override
public void bindView(final View view, Context context, String item, int position) {
final ViewHolder viewHolder = (ViewHolder) view.getTag();
final List<String> list = new ArrayList<>();
list.add("http://i.huffpost.com/gen/1279675/images/o-CAREER-facebook.jpg");
list.add("http://clickker.in/wp-content/uploads/2015/10/ambitious.jpg");
list.add("http://clickker.in/wp-content/uploads/2015/10/ambitious.jpg");
MediaPagerAdapter mediaPagerAdapter = new MediaPagerAdapter(list, context);
viewHolder.viewPager.setAdapter(mediaPagerAdapter);
}
public class ViewHolder {
public ViewPager viewPager;
public ViewHolder(View view) {
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
}
}
}
主要活动
public class MainActivity extends ToolBarActivity
{
private SwipeDeckAdapter mAdapter;
private SwipeDesk cardStack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cardStack = (SwipeDesk) findViewById(R.id.swipe_deck);
testData = new ArrayList<>();
testData.add("0");
testData.add("1");
testData.add("2");
testData.add("3");
testData.add("4");
testData.add("6");
testData.add("7");
mAdapter = new SwipeDeckAdapter(testData, this);
cardStack.setAdapter(mAdapter);
}
}
你们都知道我在编辑之前解释的剩余故事。
谢谢,