如何在单击 RecyclerView 项目时从一个片段移动到另一个片段
Posted
技术标签:
【中文标题】如何在单击 RecyclerView 项目时从一个片段移动到另一个片段【英文标题】:How to move from one fragment to another fragment on click of a RecyclerView item 【发布时间】:2018-05-17 05:12:10 【问题描述】:我想单击 RecyclerView 上的一个项目(在列表片段上),然后转到包含该项目的一些数据的详细信息片段。
我已经知道如何在活动中使用意图,但我找不到与 RecyclerView 项目有关的示例。
//In first Activity
Intent intent = new Intent();
intent.setClass(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
intent.putExtras(bundle);
startActivity(intent);
//In second Activity
Bundle bundle1=this.getIntent().getExtras();
String sex=bundle1.getString("sex");
double height=bundle1.getDouble("height");
我卡在适配器中。谁能一步步给我解释一下?
谢谢!
我的适配器代码
public class GPAAdapter extends RecyclerView.Adapter<GPAAdapter.ViewHolder>
private List<GPAModel> mGPAList;
class ViewHolder extends RecyclerView.ViewHolder
TextView lessonNameText;
TextView lessonCodeText;
TextView teacherNameText;
TextView semesterNameText;
View listView;
public ViewHolder(View gpaItemView)
super(gpaItemView);
lessonNameText = (TextView) gpaItemView.findViewById(R.id.gpa_lessonName);
lessonCodeText = (TextView) gpaItemView.findViewById(R.id.gpa_lessonCode);
teacherNameText = (TextView) gpaItemView.findViewById(R.id.gpa_teacherName);
semesterNameText = (TextView) gpaItemView.findViewById(R.id.gpa_semesterName);
listView = gpaItemView;
public GPAAdapter(List<GPAModel> dataList)
mGPAList = dataList;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gpa_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
return holder;
public void setData(List<GPAModel> viewData)
mGPAList.clear();
mGPAList.addAll(viewData);
notifyDataSetChanged();
@Override
public void onBindViewHolder(ViewHolder holder, int position)
GPAModel data = mGPAList.get(position);
holder.setIsRecyclable(true);
holder.lessonNameText.setText(data.getLessonName());
holder.lessonCodeText.setText(data.getLessonCode());
holder.teacherNameText.setText(data.getTeacherName());
holder.semesterNameText.setText(data.getSemesterName());
//save position in itemView with TAG
holder.itemView.setTag(position);
@Override
public int getItemCount()
return mGPAList.size();
//define interface
public static interface OnItemClickListener
void onItemClick(View view , int position);
private OnItemClickListener mOnItemClickListener = null;
//Alert: method does not override method from its superclass
@Override
public void onClick(View v)
if (mOnItemClickListener != null)
//getTag of position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
public void setOnItemClickListener(OnItemClickListener listener)
this.mOnItemClickListener = listener;
我的列表片段
private FragmentManager manager;
private FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View gpaView = inflater.inflate(R.layout.fragment_gpa, container, false);
RecyclerView gpaRecyclerView = (RecyclerView) gpaView.findViewById(R.id.gpa_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
gpaRecyclerView.setLayoutManager(layoutManager);
GPAAdapter adapter = new GPAAdapter(getData());
gpaRecyclerView.setAdapter(adapter);
return gpaView;
//how use .setArguments(bundle) in fragment?
super.onCreate(savedInstanceState);
manager = getFragmentManager();
GPAAdapter.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(View view , int position)
Toast.makeText(MainActivity.this, data[position], 600).show();
);
private List<GPAModel> getData()
【问题讨论】:
【参考方案1】:您可以在 List Fragment 中使用 onItemClick
中的 FragmentTransaction
替换片段
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
【讨论】:
【参考方案2】:在 Recycler.Adapter 上调用 getFragmentManager 对我来说有点困难。所以我在这里选择发送点击消息到一个新的活动(详细展示)。
希望我的代码可以帮助到别人。
我的适配器代码
public class GPAAdapter extends RecyclerView.Adapter<GPAAdapter.ViewHolder>
private ArrayList<GPAModel> mGPAList;
Context context;
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
TextView lessonNameText;
TextView lessonCodeText;
TextView teacherNameText;
TextView semesterNameText;
View listView;
ArrayList<GPAModel> list = new ArrayList<GPAModel>();
Context ctx;
public ViewHolder(View gpaItemView, Context myCtx, ArrayList<GPAModel> myList)
super(gpaItemView);
gpaItemView.setOnClickListener(this);
this.list = myList;
this.ctx = myCtx;
lessonNameText = (TextView) gpaItemView.findViewById(R.id.gpa_lessonName);
lessonCodeText = (TextView) gpaItemView.findViewById(R.id.gpa_lessonCode);
teacherNameText = (TextView) gpaItemView.findViewById(R.id.gpa_teacherName);
semesterNameText = (TextView) gpaItemView.findViewById(R.id.gpa_semesterName);
listView = gpaItemView;
@Override
public void onClick(View view)
int position = getAdapterPosition();
GPAModel sendGPA = this.list.get(position);
Intent intent = new Intent(this.ctx, GPADetail.class);
intent.putExtra("lessonName", sendGPA.getLessonName());
intent.putExtra("lessonCode", sendGPA.getLessonCode());
intent.putExtra("creditPoint", sendGPA.getCreditPoint());
intent.putExtra("teacherName", sendGPA.getTeacherName());
intent.putExtra("semesterName", sendGPA.getSemesterName());
intent.putExtra("totalStudentNumber", sendGPA.getTotalStudentNumber());
//
ArrayList<String> scoreValue = new ArrayList<String>();
scoreValue = sendGPA.getScoreValue();
intent.putExtra("scoreValue", scoreValue);
//
ArrayList<Float> studentCount = new ArrayList<Float>();
studentCount = sendGPA.getStudentCount();
intent.putExtra("studentCount",studentCount);
this.ctx.startActivity(intent);
public GPAAdapter(ArrayList<GPAModel> myList, Context myContext)
this.mGPAList = myList;
this.context = myContext;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gpa_item, parent, false);
final ViewHolder holder = new ViewHolder(view, context, mGPAList);
return holder;
public void setData(List<GPAModel> viewData)
mGPAList.clear();
mGPAList.addAll(viewData);
notifyDataSetChanged();
@Override
public void onBindViewHolder(ViewHolder holder, int position)
GPAModel data = mGPAList.get(position);
holder.setIsRecyclable(true);
holder.lessonNameText.setText(data.getLessonName());
holder.lessonCodeText.setText(data.getLessonCode());
holder.teacherNameText.setText(data.getTeacherName());
holder.semesterNameText.setText(data.getSemesterName());
@Override
public int getItemCount()
return mGPAList.size();
我的列表片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View gpaView = inflater.inflate(R.layout.fragment_gpa, container, false);
RecyclerView gpaRecyclerView = (RecyclerView) gpaView.findViewById(R.id.gpa_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
gpaRecyclerView.setLayoutManager(layoutManager);
GPAAdapter adapter = new GPAAdapter(getData());
gpaRecyclerView.setAdapter(adapter);
return gpaView;
private List<GPAModel> getData()
【讨论】:
【参考方案3】:然后像这样将上下文向上转换到 AppCompatActivity 并获取 getSupportFragmentManager
FragmentTransaction transaction =((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, new CartLayout()); // Add your fragment class
transaction.addToBackStack(null);
transaction.commit();
【讨论】:
以上是关于如何在单击 RecyclerView 项目时从一个片段移动到另一个片段的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 NSFetchedResultsController 在另一个表中单击同一项目时从一个表中删除项目
在Android recyclerVIew中单击项目时如何保存状态
如何在单击android中的listview项目时从数据库中获取id