gpt4 book ai didi

android - ViewPager 不显示内容

转载 作者:行者123 更新时间:2023-11-30 02:51:49 25 4
gpt4 key购买 nike

我正在尝试创建一个显示 ImageView 列表的 ViewPager。我能够正确地实现寻呼机和适配器,并且它在滑动时按预期工作。但是,它没有正确显示 ImageView 的内容,即图像本身。就好像图像在那里,但它们是透明的。整个批处理中只有一张图片可见。

这是我的代码-

ImagePagerAdapter.java

public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
List<ImageView> views = new ArrayList<>();
boolean[] done = {false,false,false,false,false};
ItemDetailFragment idf;

public ImagePagerAdapter(ItemDetailFragment idf,
LayoutInflater inflater) {
this.idf = idf;
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(idf.getActivity());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}

public ImagePagerAdapter(LayoutInflater inflater) {
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(inflater.getContext());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
if (done[position]) {
return views.get(position);
}
ImageView v = views.get(position);
views.set(position, v);
((ViewPager) container).addView(v);
done[position] = true;
return v;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}

@Override
public int getCount() {
return 5;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return true;
}

}

ItemDetailFragment.java:这是我设置适配器的地方。

public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
pager = (ViewPager) rootView.findViewById(R.id.pager);
adapter = new ImagePagerAdaper(this, inflater);
pager.setAdapter(adapter);
}

fragment_item_detail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.trial.piclist.ItemDetailFragment" >

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >

</android.support.v4.view.ViewPager>

<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/item_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textIsSelectable="true" />

<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />

<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/controls_layout" />
</TableRow>

<ScrollView
android:id="@+id/descScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
</ScrollView>

</LinearLayout>

请帮助。谢谢。

最佳答案

使用:

public class ImagePagerAdapter extends PagerAdapter {

LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
act = a;
count=c;
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return count;
}

@Override
public Object instantiateItem(View collection, int position) {
View v1 = Inflater.inflate(R.layout.element_image, null);
ImageView image = (ImageView) v1
.findViewById(R.id.about_image);
image.setImageResource(R.drawable.light_grey_background);
((ViewPager) collection).addView(v1, 0);

return v1;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
return null;
}
}

制作布局element_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

替换

adapter = new ImagePagerAdaper(this, inflater);

adapter = new ImagePagerAdaper(getActivity(), count_of_images);

关于android - ViewPager 不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com