您好我正在尝试在我的代码中制作文本滑块和图像滑块,文本滑块工作正常但图像滑块不显示,我不知道为什么会发生
这是我的代码
简介片段
public class Intro extends Fragment {
ViewPager imagePager, textPager;
MammalAdapter mammalAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mammals_intro, container, false);
forTextSlider(R.id.mammalIntroTextPager, view);
forImageSlider(R.id.mammalIntroimagePager, view);
return view;
}
private void forImageSlider(int pager, View view) {
imagePager = (ViewPager) view.findViewById(pager);
int [] imageArray = {R.drawable.img1, R.drawable.img2,R.drawable.img3, R.drawable.img4,R.drawable.img5,
R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10};
mammalAdapter = new MammalAdapter(getActivity(), "img", imageArray);
imagePager.setAdapter(mammalAdapter);
}
private void forTextSlider(int pager, View view) {
textPager = (ViewPager) view.findViewById(pager);
String [] mammalArray = getResources().getStringArray(R.array.mammal_info);
mammalAdapter = new MammalAdapter(getActivity(), "text", mammalArray);
textPager.setAdapter(mammalAdapter);
}
}
这是我的适配器类
public class MammalAdapter extends PagerAdapter {
Context context;
int img[]; String text[];
String type;
public MammalAdapter(Context context, String type, String[] text) {
this.context = context;
this.text = text;
this.type = type;
}
public MammalAdapter(Context context, String type, int[] img) {
this.context = context;
this.img = img;
this.type = type;
}
LayoutInflater layoutInflater;
@Override
public int getCount() {
if(type.equals("image"))
{
return img.length;
}
else if(type.equals("text")){
return text.length;
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
if(type.equals("img"))
{
view = layoutInflater.inflate(R.layout.mammal_img_item, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.mammalImageItem);
imageView.setImageResource(img[position]);
container.addView(view);
return view;
}
else if(type.equals("text")){
view = layoutInflater.inflate(R.layout.mammal_text_item, container, false);
TextView textView = (TextView) view.findViewById(R.id.mammalTextItem);
textView.setText(text[position]);
container.addView(view);
return view;
}
return null;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
我的Xml布局
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:id="@+id/mammalIntroTextPager"/>
<me.relex.circleindicator.CircleIndicator
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:id="@+id/indicator"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:id="@+id/mammalIntroimagePager"/>
image item xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/purple"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mammalImageItem"/>
</LinearLayout>
text item xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/whiteGrey"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mammalTextItem"/>
</LinearLayout>
答案 0 :(得分:1)
检查&#34; img&#34;而不是&#34;图像&#34;在getCount()中如下:
@Override
public int getCount() {
if(type.equals("img"))
{
return img.length;
}
else if(type.equals("text")){
return text.length;
}
return 0;
}