我有以下问题..在我的应用程序中,我有一个活动应该显示用户的每个ProfilePictures +一个额外的片段来添加新图片。
什么工作:我有Fragment和StatePagerAdapter的Fragment活动;
碎片: 公共类ScreenSlidePageFragment扩展Fragment {
final static int RESULT_LOAD_IMAGE = 2;
ArrayList<String> test = new ArrayList<String>();
Bitmap profilePic;
ImageView imgView;
UserProfilePub_viewpager actRef;
Bitmap bitmap;
int pos;
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
public ScreenSlidePageFragment newInstance(Bitmap profilePic, ArrayList<Bitmap> bitmaps, int pos){
this.bitmaps = bitmaps;
this.profilePic = profilePic;
this.pos = pos;
ScreenSlidePageFragment f = new ScreenSlidePageFragment();
Bundle bdl = new Bundle();
bdl.putParcelableArrayList("profilePics", bitmaps);
bdl.putInt("pos", pos);
bdl.putParcelable("profilePic", profilePic);
f.setArguments(bdl);
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
imgView = (ImageView) rootView.findViewById(R.id.up_pub_iv_viewpager);
if(getArguments().getInt("pos") == 0){
imgView.setImageBitmap((Bitmap) getArguments().getParcelable("profilePic"));
}else{
int position = getArguments().getInt("pos");
bitmaps = getArguments().getParcelableArrayList("profilePics");
if(bitmaps.get(position) != null)
imgView.setImageBitmap((Bitmap) getArgumentsbitmaps.get(position);
imgView.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View arg0) {
loadPic(imgView);
return false;
}
});
//}
}
return rootView;
}
public void loadPic(ImageView imgView){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
}
所以,现在,主要的UserProfilePic,在开始活动之前下载,只是显示在第一个片段中,另外一个UserProfilePics应该显示在其余的片段中......如果带有图片的数组是空的,最后一个Fragment应该添加一个选项来添加一个新的图片...但是这对于现在来说并不重要...所以,现在我使用主用户图片和一个可以添加新图片的片段的片段。 ..通过OnLongClick手机上的图库被调用,可以设置新的图片。
FragmentActivity + Adapter:
public class UserProfilePub_viewpager extends FragmentActivity {
ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
UserProfilePub_viewpager actRef;
String url="***************************";
Bitmap profilePic;
int pos;
private static final int NUM_PAGES = 2;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout_activity);
Intent intent = getIntent();
String USID = intent.getExtras().getString("USID");
profilePic = intent.getExtras().getParcelable("profilePic");
pics = intent.getParcelableArrayListExtra("profilePics");
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("USID", USID));
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), profilePic, pics);
mPager.setAdapter(mPagerAdapter);
}
public void onBackPressed(){
if(mPager.getCurrentItem() == 0){
super.onBackPressed();
}else{
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
public static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
Bitmap profilePic;
public ScreenSlidePagerAdapter(FragmentManager fm, Bitmap profilePic, ArrayList<Bitmap> pics) {
super(fm);
this.pics = pics;
this.profilePic = profilePic;
}
@Override
public Fragment getItem(int pos){
return new ScreenSlidePageFragment().newInstance(profilePic, pics, pos);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUM_PAGES;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_profile_pub_viewpager, menu);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
int RESULT_LOAD_IMAGE = 2;
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
int vID = data.getExtras().getInt("vID");
imgView = (ImageView) findViewById(vID);
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
decodeBitmap decBit = new decodeBitmap(picturePath, imgView);
decBit.execute();
}
}
public void savePic(Bitmap bitmap){
pics.add(bitmap);
}
decodeBitmap是一个asyncTask,其中来自Gallery的所选图片被scalled ..在onPostExecute方法中,他调用“savePic”...并且在这最后一个方法中我想要为现有的片段添加一个新的片段在最后一个位置,应显示所选图像。 但我不知道,如何添加一个新的Fragment..i尝试使用FragmentTransaction,但这不起作用..我认为因为我使用Adapter..can任何人都可以帮助我吗?至少只是一个我应该思考的方向的东西...... =(
------我自己的回答------
我已经尝试通过FragmentTransaction更新viewpager ..但它不起作用...我想我必须通过适配器来做...而且我已经尝试过这样做...而且它也有效..但遗憾的是根本没有......当我想要获得图片时,在图库意图中选择了图片,图片由asynctask解码,并且这从Fragment活动调用一个方法来添加一个新的片段。问题是......当我发送意图并从意图库中获取图片时......片段活动被破坏并重新创建......所以asynctask指的是活动的旧/错实例......我该如何解决这个问题?
答案 0 :(得分:0)
将主布局设置为线性布局。它应该包含你的ViewPager和一个FrameLayout,它将包含你添加的新图片片段。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<FrameLayout android:orientation="horizontal"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
然后当你想要添加你的片段时:
getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();