从不同活动中的另一个片段调用片段的方法

时间:2017-08-24 16:56:51

标签: android android-fragments android-activity fragment android-fragmentactivity

我有MainActivityfragment托管FragmentOne 还有一个名为Activity的{​​{1}}托管另一个DetailPagerActivity fragment

我想在DetailFragment中使用method中的DetailFragment .. 我怎样才能做到这一点? 我试图使用FragmentOne等等,但失败了

我想从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

1 个答案:

答案 0 :(得分:0)

您应该将getDetailsReport()移出片段​​并进入某种数据/实用程序类 - 然后您可以从两个地方调用它。数据应该与UI分开,特别是如果您需要在多个活动/片段中使用它。

如果要在不相关的活动/片段之间进行通信,最简单的方法是使用Broadcast / BroadcastReceiver。这通常用于通知其他活动/片段需要更新的内容 - 而不是尝试查询其他活动/片段的数据。通常,片段/活动在接收广播时会更新,如果从头开始初始化则会处于良好状态。

当使活动/片段依赖于其他活动/片段时,您将遇到的问题是可以清除未显示的那个 - 因此您不能依赖实际发生的操作。