我正在尝试使用nested fragments
,ViewPager
但有问题。我有一个ViewPager
and 在我想添加的第一页 3 fragments
,负责拍照和预览它。这是我的完整代码:
活动主.xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
PlaceHolderFragment 布局,fragment_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:orientation="vertical">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/frm1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></FrameLayout>
<FrameLayout
android:id="@+id/frm2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></FrameLayout>
<FrameLayout
android:id="@+id/frm3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></FrameLayout>
</LinearLayout>
主要活动:
这里包括SectionPagerAdapter
和Page Fragment
( PlaceHolderFragment
)。
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
private ArrayList <Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragments();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), list);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(mSectionsPagerAdapter.getCount());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
private static final String ARG_SECTION_NUMBER = "section_number";
private void initFragments(){
list = new ArrayList<Fragment>();
Fragment f = null;
for(int i = 0; i < 5; i++) {
f = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, i);
f.setArguments(args);
list.add(f);
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> list;
public SectionsPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return list.get(position);
}
@Override
public int getCount() {
// Show 3 total pages.
return this.list.size();
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
int pagePosition = getArguments().getInt(ARG_SECTION_NUMBER);
if(pagePosition == 0){
FragmentTransaction trans = getChildFragmentManager().beginTransaction();
TestImgFragment frg1 = new TestImgFragment();
trans.add(R.id.frm1, frg1, "test1");
TestImgFragment frg2 = new TestImgFragment();
trans.add(R.id.frm2, frg2, "test2");
TestImgFragment frg3 = new TestImgFragment();
trans.add(R.id.frm3, frg3, "test3");
trans.commit();
}
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment f = null;
f = getChildFragmentManager().findFragmentByTag("test1");
f.onActivityResult(requestCode, resultCode, data);
f = getChildFragmentManager().findFragmentByTag("test2");
f.onActivityResult(requestCode, resultCode, data);
f = getChildFragmentManager().findFragmentByTag("test3");
f.onActivityResult(requestCode, resultCode, data);
}
}
}
这是Nested Fragment class
和layout
:
test_img_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/image_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="@dimen/textLabel_weight"
android:textSize="@dimen/view_text_size_normal" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="@dimen/inputView_weight"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/imageview_width"
android:layout_height="@dimen/imageview_height"
android:scaleType="fitCenter"
android:layout_marginRight="@dimen/customview_marginright"
android:src="@drawable/no_image" />
</LinearLayout>
</LinearLayout>
测试ImgFragment:
public class TestImgFragment extends Fragment {
public static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int PICK_IMAGE_REQUEST_CODE = 200;
private static final String IMAGE_DIRECTORY_NAME = "Loan Images";
private Bitmap bitmap;
private ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_img_fragment, container, false);
imageView = (ImageView) rootView.findViewById(R.id.image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
return rootView;
}
private void openDialog(){
final Dialog dialog = new Dialog(this.getActivity());
dialog.setContentView(R.layout.dialog_image_setting_type);
dialog.setTitle("Image Type");
Button captureImage = (Button) dialog.findViewById(R.id.capture);
Button pickImage = (Button) dialog.findViewById(R.id.pick);
captureImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
captureImage();
dialog.dismiss();
}
});
pickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickImage();
dialog.dismiss();
}
});
dialog.show();
}
private void captureImage(){
Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
private void pickImage(){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
}
private void previewImage(Bitmap image){
Bitmap bitmap = image;
imageView.setImageBitmap(bitmap);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
switch(requestCode) {
case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
if(resultCode == getActivity().RESULT_OK) {
Uri photoUri = data.getData();
InputStream imageStream = null;
try {
imageStream = getActivity().getContentResolver().openInputStream(photoUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(imageStream, null, options);
previewImage(bitmap);
} else if(resultCode == getActivity().RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(), "User Cancelled Image Capture", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), "Sorry! Failed to capture Image", Toast.LENGTH_LONG).show();
}
break;
case PICK_IMAGE_REQUEST_CODE:
if(resultCode == getActivity().RESULT_OK) {
Uri selectedImage = data.getData();
InputStream imageStream = null;
try {
imageStream = getActivity().getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(imageStream, null, options);
previewImage(bitmap);
}else if(resultCode == getActivity().RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(), "User Cancelled Image Capture", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), "Sorry! Failed to capture Image", Toast.LENGTH_LONG).show();
}
break;
}
}
}
我想要做的是,当用户clicks
,ImageView
他/她应该capture
或pick image
应该预览。
但是问题是,当我单击第一个ImageView
(First Nested Fragment
)时,一切正常,但是当我单击第二个和第三个ImageViews
(Nested Fragments
)时,PlaceHolderFragmet
-sonActivityResult
Nested Fragments
不可见,当我调用getChildFragmentManager().findFragmentByTag("test1");
或任何其他tag
时,它返回null
。
我不知道我错过了什么。