我想在我的recyclerview适配器中打开一个片段表单onClick并传递数据。我知道如何打开一个活动(检查下面的代码),但我怎么能以相同的方式打开片段。请查看下面的代码以便更好地理解。我是android的新手,并试图从免费资源中学习。对不起,如果有什么不对。
CODE
@Override
public void onClick(View v) {
int position = getAdapterPosition();
PlaylistDetailsItem playlistDetailsItems = this.playlistDetailsItems.get(position);
Intent intent = new Intent(this.context, VideoActivity.class);
intent.putExtra("videoHeading", playlistDetailsItems.getPlaylistDetailsSnippet()
.getPlaylistDetailsTitle());
intent.putExtra("videoDesc", playlistDetailsItems.getPlaylistDetailsSnippet()
.getPlaylistDetailsDescription());
intent.putExtra("videoID", playlistDetailsItems.getPlaylistDetailsSnippet()
.getPlaylistDetailsResourceId().getVideoId());
this.context.startActivity(intent);
}
}
答案 0 :(得分:0)
在onClick中添加以下代码
FirstFragment firstFrag = new FirstFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.layout_container, firstFrag, TAG_FRAGMENT)
.addToBackStack(null)
.commit();
答案 1 :(得分:0)
Bundle b = new Bundle();
replaceFragmentInternal(new fragment,b,true,false);
//将数据放入bundle中并在方法中传递。在顶级活动中使用此方法,这样您就不必一次又一次地编写相同的代码。
replaceFragmentInternal(Fragment fragment, Bundle bundle, boolean addToBackStack, boolean anim) {
if (bundle != null)
fragment.setArguments(bundle);
tag = fragment.getClass().getSimpleName();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.container_internal, fragment, tag);
fragment.setRetainInstance(true);
if (addToBackStack)
ft.addToBackStack(tag);
try {
ft.commit();
} catch (Exception ex) {
ex.printStackTrace();
ft.commitAllowingStateLoss();
}
}
答案 2 :(得分:0)
首先,您必须创建一个像这样的Fragment类,并记住导入android.support.v4.app.Fragment; (支持v4片段)
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_xml_file, container, false);
return view;
}
}
并调用像
这样的片段 MyFragment myfragment = new Fragment();
//pass data
Bundle bundle = new Bundle();
bundle.putString("KEY","DATA");
myfragment.setArguments(bundle);
FragmentTransaction fragmentManager = activity.getSupportFragmentManager().beginTransaction();
fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentManager.addToBackStack(null);
fragmentManager.replace(R.id.content, mFragment, TAG).commit();
答案 3 :(得分:0)
如果没有,您是否创建了Viewholder,然后在关闭适配器之前在底部的回收器视图适配器下方粘贴(在底部“}”之前)
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIdView = (TextView) itemView.findViewById(R.id.id);
mContentView = (TextView) itemView.findViewById(R.id.content);
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = getAdapterPosition();
if (null != listener) {
listener.onListFragmentInteraction(mValues.get(position));
}
}
});
}
另请阅读本教程以发送数据: https://developer.android.com/training/basics/fragments/communicating.html
所有这些看起来很难,但这是为了学习而且看起来很专业