所以我试图基于相同的布局文件创建一个包含26种不同布局的viewpager,并且我试图通过一个包传递文本和图像。我把文本部分放下了,但我对如何将图像放入包中感到困惑。
public class ScreenSlidePageFragment extends Fragment {
//Resources mResources = getResources();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.alphabet, container, false);
TextView tv = (TextView)v.findViewById(R.id.alphabet);
// ImageView iv = (ImageView)v.findViewById(R.id.letter);
//Drawable drawable = mResources.getDrawable(R.drawable.aletter);
//iv.setImageDrawable(drawable);
tv.setText(getArguments().getString("msg"));
return v;
}
public static ScreenSlidePageFragment newInstance(String text, Drawable drawable) {
ScreenSlidePageFragment f = new ScreenSlidePageFragment();
Bundle b = new Bundle();
b.putString("msg", text);
// b.putInt();
f.setArguments(b);
return f;
}
}
public Fragment getItem(int position) {
switch(position) {
case 0: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 1");
case 1: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 2");
case 2: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 3");
case 3: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 4");
case 4: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 5");
case 5: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 6");
case 6: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 7");
case 7: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 8");
case 8: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 9");
case 9: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 10");
case 10: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 11");
case 11: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 12");
case 12: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 13");
case 13: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 14");
case 14: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 15");
case 15: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 16");
case 16: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 17");
case 17: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 18");
case 18: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 19");
case 19: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 20");
case 20: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 21");
case 21: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 22");
case 22: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 23");
case 23: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 24");
case 24: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 25");
case 25: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 26");
default: return ScreenSlidePageFragment.newInstance("FirstFragment, Instance 1");
}
}
答案 0 :(得分:0)
您应该在字母表布局中声明一个ImageView。
... some layout container ...
<ImageView android:id="@+id/letter" android:width="wrap_content" android:height="wrap_content" />
... some other layouts...
然后在您的onCreateView中,您可以找到此ImageView,您可以从该包中获取该图像的资源。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.alphabet, container, false);
TextView tv = (TextView)v.findViewById(R.id.alphabet);
ImageView iv = (ImageView)v.findViewById(R.id.letter);
iv.setImageResource(getArguments().getInt("letter")); // maybe check first if the argument is set :)
tv.setText(getArguments().getString("msg"));
return v;
}
在newInstance方法中,您可以将正确的资源设置为片段参数。
public static ScreenSlidePageFragment newInstance(String text, @DrawableRes int drawable) {
ScreenSlidePageFragment f = new ScreenSlidePageFragment();
Bundle b = new Bundle();
b.putString("msg", text);
b.putInt("letter", drawable); // this is the image in your drawable folder that you can access with R.drawable.letter_a (if the file is called letter_a.png
f.setArguments(b);
return f;
}
答案 1 :(得分:0)
如果要添加更多字段,使用Parcelable
会更灵活。
首先,创建自己的对象,例如Screen.java
包含字段消息和图片(可绘制)
public class Screen implements Parcelable {
public String message;
public Drawable picture;
private static final long serialVersionUID = 1L;
public Screen(String message, Drawable picture){
this.message = message;
this.picture = picture;
}
/**
* constructor to rebuild object from the Parcel
* @param source, a parcel data
*/
public Screen(Parcel source) {
this.message = source.readString();
this.picture = new BitmapDrawable(
( (Bitmap) source.readValue(Bitmap.class.getClassLoader()) )
);
}
/**
* describe the kind of special object
*/
@Override
public int describeContents() {
return hashCode();
}
/**
* write this object in to a Parcel
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(message);
dest.writeValue(((BitmapDrawable)picture).getBitmap());
}
public static final Parcelable.Creator<Screen> CREATOR = new Parcelable.Creator<Screen>() {
public Screen createFromParcel(Parcel source) {
return new Screen(source);
}
public Screen[] newArray(int size) {
return new Screen[size];
}
};
}
接下来,您可以开始在片段中传递和接收Parcelable
。
public class ScreenSlidePageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.alphabet, container, false);
TextView tv = (TextView)v.findViewById(R.id.alphabet);
ImageView iv = (ImageView)v.findViewById(R.id.letter);
final Screen screen = getArguments().getParcelable("screen");
iv.setImageDrawable(screen.picture);
tv.setText(screen.message);
return v;
}
public static ScreenSlidePageFragment newInstance(String text, Drawable drawable) {
ScreenSlidePageFragment f = new ScreenSlidePageFragment();
Screen screen = new Screen(text, drawable);
Bundle b = new Bundle();
b.putParcelable("screen", screen);
f.setArguments(b);
return f;
}
希望这会对你有帮助!