我有一個MainActivity
舉辦了fragment
稱爲FragmentOne
還有另一個Activity
命名DetailPagerActivity
託管在其他fragment
稱爲DetailFragment
調用方法
我想在FragmentOne
使用method
從DetailFragment
..我該怎麼辦呢? 我想等使用FragmentManager
和,但未能
我想打電話給getDetailReport()
從DeatilFragment
在onSwiped
>FragmentOne
這裏是我的FragmentOne
:
public class FragmentOne extends Fragment {
private RecyclerView mDetailRecyclerView;
private DetailAdapter mAdapter;
private boolean mNumberVisible;
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); }
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_one_layout,
container, false);
mDetailRecyclerView = (RecyclerView)
view.findViewById(R.id.detail_recycler_view);
..
..
return view;
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_NUMBER_VISIBLE, mNumberVisible);
}
private class DetailHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
private TextView mTitleTextView;
private Detail mDetail;
private RatingBar mRatingBar;
public DetailHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_detail,
parent, false));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.detail_title);
mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
}
public void bind(Detail detail) {
mDetail = detail;
mTitleTextView.setText(mDetail.getTitle());
mRatingBar.setRating(mDetail.getRate()); }
@Override
public void onClick(View view) {
Intent intent = DetailPagerActivity.newIntent(getActivity(),
mDetail.getId());
startActivity(intent); }
return true;
}
}
private class DetailAdapter extends RecyclerView.Adapter<DetailHolder> {
private List<Detail> mDetails;
public DetailAdapter(List<Detail> details) {
mDetails = details;
}
@Override
public DetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(getActivity());
return new DetailHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(DetailHolder holder, int position) {
Detail detail = mDetails.get(position);
holder.bind(detail);
}
@Override
public int getItemCount() {
return mDetails.size();
}
public void setDetails(List<Detail> details) {
mDetails = details;
////////////////////////////////////////////////////////////////////////////////////////////////////////
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
private Detail mDetail;
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition(); //get position which is swipe
if (direction == ItemTouchHelper.LEFT) { //if swipe left
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); //alert for confirm to delete
builder.setMessage("Are you sure to delete?"); //set message
builder.setPositiveButton("REMOVE", new DialogInterface.OnClickListener() { //when click on DELETE
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, getDetailReport());
i.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.detail_report_subject));
i = Intent.createChooser(i, getString(R.string.send_report));
startActivity(i);
//////////////////////////////////////////////////////
// mAdapter.notifyItemRemoved(position); //item removed from recylcerview
// DetailLab.get(getActivity()).deleteDetail(mDetail);
// updateUI();
// updateNumbers();
dialog.dismiss();
// return;
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { //not removing items if cancel is done
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show(); //show alert dialog
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(mDetailRecyclerView); //set swipe to recylcerview
}
}
}
,這是我DetailFargment
:
public class DetailFragment extends Fragment {
private static final String ARG_DETAIL_ID = "detail_id";
private static final String DIALOG_DATE = "DialogDate";
private static final String DIALOG_PHOTO = "DialogPhoto";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_PHOTO = 2;
..
..
public static DetailFragment newInstance (UUID detailId) {
Bundle args = new Bundle();
args.putSerializable(ARG_DETAIL_ID, detailId);
DetailFragment fragment = new DetailFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
UUID detailId = (UUID) getArguments().getSerializable(ARG_DETAIL_ID);
mDetail = DetailLab.get(getActivity()).getDetail(detailId);
mPhotoFile = DetailLab.get(getActivity()).getPhotoFile(mDetail);
}
@Override
public void onPause() {
super.onPause();
DetailLab.get(getActivity()).updateDetail(mDetail);
}
@Override
public View onCreateView (LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_detail,
container, false);
..
..
/////////////////////////// Report Button
mReportButton = (Button) v.findViewById(R.id.detail_report);
mReportButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, getDetailReport());
i.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.detail_report_subject));
i = Intent.createChooser(i, getString(R.string.send_report));
startActivity(i);
}
});
..
..
return v;
}
@Override
public void onResume() {
super.onResume();
}
//////////////////////////////////// REPORT
private String getDetailReport() {
String dateFormat = "EEE, MMM dd";
String dateString = DateFormat.format(
dateFormat, mDetail.getDate()).toString();
String rateString = null;
String report = getString(R.string.detail_report,
mDetail.getTitle(), dateString,
rateString, rateString
);
return report;
}
..
..}
一個活動不應該依賴於任何其他活動實例(或其片段)的存在。 –
@KevinKrumwiede是的,我現在知道這一點,謝謝 –