如何在单击RecyclerView项目时从一个片段移动到另一个片段

时间:2017-12-03 18:02:59

标签: android android-fragments android-intent bundle

我想点击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() {
}

3 个答案:

答案 0 :(得分:0)

您可以使用列表片段中的FragmentTransaction中的onItemClick替换片段

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
ft.commit(); 

答案 1 :(得分:0)

在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() {
}

答案 2 :(得分:0)

然后像这样向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();