android - 我想将来自 recyclerview 适配器的数据从片段发送到片段打开新片段

android - I want to send data from recyclerview adapter from fragment to fragment opening new fragment

我想将数据从 RecyclerView 适配器发送到新的 Fragment。成功了,但是为什么前面片段的碰撞结果是?

至少你的关注对我来说是很大很有意义的:)

如下图:

我要片段开辟新的片段如WhatsAppInstagram

RecyclerViewAdapter.java

package com.mimdudin.carekkerje.Adapter;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mimdudin.carekkerje.Activities.HomeFragment;
import com.mimdudin.carekkerje.Activities.HomeFragmentDetail;
import com.mimdudin.carekkerje.Model.Job;
import com.mimdudin.carekkerje.R;

import java.util.List;


public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private List<Job> jobList;

    public HomeRecyclerViewAdapter(Context context, List<Job> jobs){
        this.context = context;
        jobList = jobs;
    }

    @Override
    public HomeRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_row, parent, false);

        return new ViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder(HomeRecyclerViewAdapter.ViewHolder holder, int position) {
    Job job = jobList.get(position);

    holder.title.setText(job.getTitle());
    holder.namaPerusahaan.setText(job.getNamaPerusahaan());
    holder.kriteria_1.setText(job.getKriteria_1());
    holder.kriteria_2.setText(job.getKriteria_2());
    holder.kriteria_3.setText(job.getKriteria_3());
    holder.gaji.setText(job.getGaji());
    holder.lokasi.setText(job.getLokasi());
    holder.tanggal.setText(job.getTanggal());

    String img_logoLink = job.getImg_logo();
        Glide.with(context)
                .load(img_logoLink) // URL
                .apply(new RequestOptions().placeholder(android.R.drawable.ic_dialog_info)
                                .error(android.R.drawable.ic_dialog_alert).centerCrop().dontAnimate()
//                         .centerCrop(), .crossFade(), .thumbnail(), .dontAnimate(), .dontTransform() BitmapTransformation(.circleCrop())
                )
                .into(holder.img_logo); //TARGET GAMBAR YANG NAK DIUBAH

    }

    @Override
    public int getItemCount() {
        return jobList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

         TextView title;
         TextView namaPerusahaan;
         TextView lokasi;
         TextView kriteria_1;
         TextView kriteria_2;
         TextView kriteria_3;
         TextView gaji;
         ImageView img_logo;
         TextView tanggal;


        public ViewHolder(final View itemView, final Context ctx) {
            super(itemView);
            context = ctx;
            title = itemView.findViewById(R.id.titleID);
            namaPerusahaan = itemView.findViewById(R.id.namaPerusahaanID);
            kriteria_1 = itemView.findViewById(R.id.satuTv);
            kriteria_2 = itemView.findViewById(R.id.duaTv);
            kriteria_3 = itemView.findViewById(R.id.tigaTv);
            gaji = itemView.findViewById(R.id.gajiID);
            lokasi = itemView.findViewById(R.id.lokasiID);
            img_logo = itemView.findViewById(R.id.img_logoID);
            tanggal = itemView.findViewById(R.id.tanggalID);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HomeFragmentDetail fragmentDetail = new HomeFragmentDetail();
                    Bundle bundle = new Bundle();
                    Job job = jobList.get(getAdapterPosition());
                    bundle.putSerializable("job", job);
                    fragmentDetail.setArguments(bundle);

                    AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
                    android.support.v4.app.FragmentManager fragmentManager = activity.getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//          fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragmentTransaction.replace(R.id.frame_fragmenthome, fragmentDetail);
                    fragmentTransaction.commit();

////                    Intent intent = new Intent(context, HomeDetailActivity.class);
////                    intent.putExtra("job", job);
////                    ctx.startActivity(intent);

                }
            });
        }
    }

HomeFragmentDetail.java

package com.mimdudin.carekkerje.Activities;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mimdudin.carekkerje.Model.Job;
import com.mimdudin.carekkerje.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 */
public class HomeFragmentDetail extends Fragment {

    private TextView tvDeskripsi;
    private TextView tvIndustri;

    public HomeFragmentDetail() {
        // Required empty public constructor
    }

    public static HomeFragmentDetail newInstance(){
        HomeFragmentDetail homeFragmentDetail = new HomeFragmentDetail();
        return homeFragmentDetail;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home_fragment_detail, container, false);
        tvDeskripsi = view.findViewById(R.id.tvDeskripsi);
        tvIndustri = view.findViewById(R.id.tvIndustri);

        Bundle bundle = getArguments();
        Job job = (Job) bundle.getSerializable("job");

        String PostID = job.getId_post();
        String titleID = job.getTitle();

        tvDeskripsi.setText(PostID);
        tvIndustri.setText(titleID);

        return view;


//    DataHolder dataholder=new DataHolder("1","TestName");
//    Bundle bundle=new Bundle();
//    bundle.putSerializable("obj",dataholder);

//    Fragment fragment=new FragmentB();
//    fragment.setArguments(bundle);
//    fragmentManager = getActivity(). getSupportFragmentManager();
//    fragmentTransaction = fragmentManager .beginTransaction();
//    fragmentTransaction.add(R.id.container, fragment);
//    fragmentTransaction.commit();

        //Job job = (Job) getIntent.getSerializableExtra("job");

//        Bundle bundle = getArguments();
//        Job job = (Job) bundle.getSerializable("job");
//        String jobID = job.getId_post();
//        String titleID = job.getTitle();



    }

}

}

您只需要在回收站查看项目点击时打开一个新片段。我说得对吗?

解决方案 1:

在替换 HomeDetailFragment 的代码中,像这样调用 addToBackStack()

fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_fragmenthome, fragmentDetail);
fragmentTransaction.addToBackStack(null);    // add this line to your code
fragmentTransaction.commit();

这是 android 开发者文档中对 Providing Proper Back Navigation 的简短解释。

When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).

解决方案 2:

如果没有硬性要求使用片段,那么我建议使用 HomeDetailActivity 而不是 HomeDetailFragment。

除了调用 Activity(通过意图)而不是 Fragment 之外,上面的大部分代码将保持不变。

这有一些优点,比如您不必处理 fragmentManager 或 fragment backStack。在你的情况下,它很容易实现。