我很难理解如何将值从一个片段传递到另一个片段。我在回收商项目中创建了一个切换按钮,在检查时应将卡的内容传递给另一个实现recyclerVIew的片段(收藏夹)。
这是我的recyclerView适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ClipboardManager myClipboard;
private ClipData myClip;
private Context context;
public List<CardItemModel> cardItems;
public RecyclerAdapter(List<CardItemModel> cardItems){
this.cardItems = cardItems;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView copyButton;
ImageView shareButton;
ToggleButton favButton;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
this.title = (TextView)itemView.findViewById(R.id.card_title);
this.content = (TextView)itemView.findViewById(R.id.card_content);
this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
favButton.setChecked(false);
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_launcher));
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(cardItems.get(position).title);
holder.content.setText(cardItems.get(position).content);
holder.copyButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
myClip = ClipData.newPlainText("label", holder.content.getText().toString());
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();
}
});
holder.shareButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
}
});
holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton favButton, boolean isChecked){
if (isChecked)
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher));
else
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart));
}
});
}
@Override
public int getItemCount() {
return cardItems.size();
}
}
这是我最喜欢的&#39;我希望传递值的片段。
public class FavouriteFragment extends Fragment {
private List<CardItemModel> cardItems = new ArrayList<>(20);
private MainActivity mainActivity;
private Toolbar toolbar;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
public FavouriteFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity)activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favourite, container, false);
toolbar = (Toolbar)view.findViewById(R.id.fab_toolbar);
setupToolbar();
recyclerView = (RecyclerView)view.findViewById(R.id.fab_recycler_view);
setupRecyclerView();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainActivity.setupNavigationDrawer(toolbar);
}
private void setupToolbar(){
toolbar.setTitle(getString(R.string.fav_fragment_title));
mainActivity.setSupportActionBar(toolbar);
}
private void setupRecyclerView(){
recyclerView.setLayoutManager(new LinearLayoutManager(mainActivity));
recyclerView.setHasFixedSize(true);
initializeCardItemList();
recyclerAdapter = new RecyclerAdapter(cardItems);
recyclerView.setAdapter(recyclerAdapter);
}
private void initializeCardItemList(){
CardItemModel cardItemModel;
String[] cardTitles = getResources().getStringArray(R.array.fav_cards);
String[] cardContents = getResources().getStringArray(R.array.fav_cards_content);
final int length = cardTitles.length;
for(int i=0;i<length;i++){
cardItemModel = new CardItemModel(cardTitles[i],cardContents[i]);
cardItems.add(cardItemModel);
}
}
}
答案 0 :(得分:1)
您可以使用界面。让我们看一个您希望将值从FragmentA传递到FragmentB
的示例 class FragmentA extends Fragment{
//Your fragment a functionalities goes here
//now define an interface inside this FragmentA
//you get reference from main activity
MainActivity activity;
@Override
void onCreate(){
activity = getActivity();
}
//after your functionalities you can call passValue function of main activity from the variable 'activity' like
activity.passValue(your integer value from fragmentA to main activity)
interface OnValuePassedListener{
void passValue(int value)//if you want to pass integer value
}
}
//this is the activity where FragmentA is located
public class MainActivity extends Activity implements FragmentA.OnValuePassedListener{
//Inside this activity, you write code to open the fragment. When you do that pass this activity's reference to the FragmentA
@Override
void passValue(int value){
//you get the value from FragmentA inside this function
}
}
您知道如何将值从活动传递到片段。从passValue()函数内部活动,您可以调用另一个片段并将值传递给片段。
通过这种方式,您可以使用Activity作为中间人将值从FragmentA传递到另一个片段。 您可以在此处https://www.youtube.com/watch?v=VyyGP_d0Ia8&index=118&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa
了解有关此内容的更多信息答案 1 :(得分:0)
片段之间的通信可以通过EventBus完成。 脚步 : 1.创建事件类 2.注册要更改片段数据的事件。 3.从片段发布事件(onClick on toggle按钮)。
https://github.com/greenrobot/EventBus
http://greenrobot.org/eventbus/
第1步:
import java.io.Serializable;
public class ToggleButtonClickEvent implements Serializable {
private CardItemModel cardItemModel;
public ToggleButtonClickEvent (CardItemModel ardItemModel){
this.cardItemModel= cardItemModel;
}
public CardItemModel getCardItemModel() {
return cardItemModel;
}
}
第2步: 在片段中注册和取消注册要更改数据的事件
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
第3步:
单击切换按钮时发布事件
EventBus.getDefault().post(new ToggleButtonClickEvent(cardItemModel));
答案 2 :(得分:0)
在Bundle中添加您的值
Fragment fragment = new Table_Selection_QRCODE();
Bundle bun = new Bundle();
bun.putString("table","20");
fragment.setArguments(bun);
getFragmentManager().beginTransaction().replace(R.id.frag, fragment).commit();
在其他片段中接收像这样的值(OnCreate)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b=getArguments();
if(b!=null){
rslt=b.getString("table","");
}
}