我不知道我做错了什么。好吧,我有一个包含 listView 和 Fragment 的 Activity,当我从我的 listView 中按下一个项目时,它会用另一个片段替换该片段。所以问题是当我向我的片段添加一些视图并用另一个片段替换它时,如果我返回到我的第一个片段,我没有发现我的视图被添加。有一些代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View addReport = inflater
.inflate(R.layout.add_report, container, false);
ListView listView = (ListView) addReport.findViewById(R.id.list);
// listView.addHeaderView(padding);
listView.setDivider(new GradientDrawable(Orientation.LEFT_RIGHT, colors));
listView.setDividerHeight(1);
remplirListOfReport();
ReportAdaptor reportSchedule = new ReportAdaptor(getActivity(),
R.layout.report_item_format, listOfReports);
listView.setAdapter(reportSchedule);
// Inflate the root layout
myInflater = (LayoutInflater) addReport.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
uploadFiles = (LinearLayout) addReport.findViewById(R.id.upload_layout);
picture = (Button) addReport.findViewById(R.id.picture);
video = (Button) addReport.findViewById(R.id.video);
vocale = (Button) addReport.findViewById(R.id.vocal);
report = (EditText) addReport.findViewById(R.id.report_text);
picture.setOnClickListener(buttonOnClickListener);
video.setOnClickListener(buttonOnClickListener);
vocale.setOnClickListener(buttonOnClickListener);
return addReport;
}
Button.OnClickListener buttonOnClickListener = new Button.OnClickListener() {
private String fullPathAudio = "";
@Override
public void onClick(View v) {
if (v == picture) {
if(null != getGPSCoordonnee(getActivity())){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, MEDIA_TYPE_IMAGE);
}
} else if (v == video) {
if(null != getGPSCoordonnee(getActivity())){
Intent intent1 = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
intent1.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set
//the video image quality to high
startActivityForResult(intent1, MEDIA_TYPE_VIDEO);
}
} else if (v == vocale) {
if(null != getGPSCoordonnee(getActivity())){
getOutputMediaFile(MEDIA_TYPE_AUDIO);
fullPathAudio = URL_START_WITH + File.separator + AUDIO_NAME;
vocale.setText("Vocal");
View audioView = myInflater.inflate(R.layout.picture_format,
uploadFiles, false);
description = (TextView) audioView
.findViewById(R.id.picture_text);
imageView = (ImageView) audioView
.findViewById(R.id.picture_image);
edit = (Button) audioView.findViewById(R.id.picture_edit);
date = (TextView) audioView.findViewById(R.id.picture_date);
date.setText(TIME);
description.setText("Audio");
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(getActivity(),
ShowVideo.class);
intent2.putExtra("videoName", fullPathAudio);
startActivity(intent2);
}
});
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
editPictureNames(description);
}
});
uploadFiles.addView(audioView);
Intent myIntent = new Intent(getActivity(),
AudioRecordingActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra("path", fullPathAudio);
getActivity().startActivity(myIntent);
}
}
}
};
我的活动中有一种方法可以替换片段:
@Override
public void addReport(long id) {
LEADERBOARD_FRAG_TAG = "REPORT";
int show = 0;
fragment = getSupportFragmentManager().findFragmentByTag(LEADERBOARD_FRAG_TAG);
if(null == fragment){
fragment = new AddReportFragment();
show = 0;
}else
show = 1;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.myFragment, fragment, LEADERBOARD_FRAG_TAG);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}