如何将值从一个片段的回收器视图项传递到另一个片段
Posted
技术标签:
【中文标题】如何将值从一个片段的回收器视图项传递到另一个片段【英文标题】:how to pass values from recycler view item of one fragment to another fragment 【发布时间】:2017-10-13 06:58:06 【问题描述】:我很难理解如何将值从一个片段传递到另一个片段。 I've created a toggle button in recycler item which when checked should pass the content of the card to another fragment(Favourites) implementing 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();
这是我想要传递值的“收藏夹”片段。
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);
【问题讨论】:
【参考方案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
您知道如何将值从活动传递到片段。通过activity内部的passValue()函数,你可以调用另一个fragment并将值传递给fragment。
通过这种方式,您可以使用 Activity 作为中间人将值从 FragmentA 传递到另一个 Fragment。 您可以从这里了解更多信息https://www.youtube.com/watch?v=VyyGP_d0Ia8&index=118&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa
【讨论】:
【参考方案2】:Fragment之间的通信可以通过EventBus来完成。 脚步 : 1.创建事件类 2.注册要更改片段数据的事件。 3. 从片段发布事件(点击切换按钮)。
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));
【讨论】:
【参考方案3】:在 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","");
【讨论】:
以上是关于如何将值从一个片段的回收器视图项传递到另一个片段的主要内容,如果未能解决你的问题,请参考以下文章