将活动-1的recyclerview适配器中的数据传递给activity-2的viewpager片段

时间:2018-04-07 06:05:48

标签: android android-fragments android-recyclerview android-viewpager

我的recyclerView适配器 - 活动1

  

这里我使用 setArguments()

向ViewPager Fragmnet发送参数
   @Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Bundle bundle = new Bundle();
            bundle.putString("projectName",  jobCardLists.get(position).getProject());
            // set MyFragment Arguments
            DetailsFragment detailsFragment = new DetailsFragment();
            detailsFragment.setArguments(bundle);
          Intent intent = new Intent(context, JobCardViewActivity.class);
          context.startActivity(intent);

    });
}

活动2 ,其中包含ViewPager - 标签布局

public class JobCardViewActivity extends AppCompatActivity {


private ViewPager mViewPager;
private Toolbar mToolbar;
private JobCardViewVPAdapter mViewPagerAdapter;
private TabLayout mTabLayout;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job_card_view);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    //set TabLayout
    setViewPager();


}

private void setViewPager() {
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPagerAdapter = new JobCardViewVPAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mViewPagerAdapter);

    mTabLayout = (TabLayout) findViewById(R.id.tab);
    mTabLayout.setupWithViewPager(mViewPager);
    mTabLayout.getTabAt(0).setIcon(R.drawable.ic_home_white_24dp);
}

}

我的viewPager适配器

公共类JobCardViewVPAdapter扩展了FragmentStatePagerAdapter {

private static int TAB_COUNT = 6;

public JobCardViewVPAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return DetailsFragment.newInstance();
        case 1:
            return WorkActivitiesFragment.newInstance();
        case 2:
            return DocumentsFragment.newInstance();
        case 3:
            return TasksFragment.newInstance();
        case 4:
            return WorkImagesFragment.newInstance();
        case 5:
            return NotesFragment.newInstance();
    }
    return null;
}

@Override
public int getCount() {
    return TAB_COUNT;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 1:
            return WorkActivitiesFragment.TITLE;
        case 2:
            return DocumentsFragment.TITLE;
        case 3:
            return TasksFragment.TITLE;
        case 4:
            return WorkImagesFragment.TITLE;
        case 5:
            return NotesFragment.TITLE;
    }
    return super.getPageTitle(position);
}

}

我的ViewPager片段类

  

这里我从 getArguments()

获得 NULL
public class DetailsFragment extends Fragment {

private View view;
private Activity activity;

public static final String TITLE = "Details";
private String projectName;

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


public static DetailsFragment newInstance() {

    return new DetailsFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();
    if (getArguments() != null) {
        Bundle bundle = getArguments();
        projectName = bundle.getString("projectName");
    } else {
        Toast.makeText(activity, "NULL", Toast.LENGTH_SHORT).show();

    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_details, container, false);
    TextView tvProjectName, startDate, endDate, assignedBy, workCategory, status;

    tvProjectName = view.findViewById(R.id.tv_project_name);
    tvProjectName.setText(projectName);
    return view;
}

1 个答案:

答案 0 :(得分:1)

1.您可以为您的片段使用单个对象(单身)。

在你的片段使用中,

private static DetailsFragment ourInstance = new DetailsFragment();

public static DetailsFragment newInstance() {

    return ourInstance;
}

onBindViewHolder()中,使用

 DetailsFragment detailsFragment =  DetailsFragment.newInstance();

 detailsFragment.setArguments(bundle);

2.或者,在活动之间传递数据

onBindViewHolder()中,使用

 Intent intent = new Intent(context, JobCardViewActivity.class);
 intent.putString("project",  jobCardLists.get(position).getProject());
 context.startActivity(intent);

并且做,

1.in JobCardViewActivity,获取字符串并保存

myString=getIntent().getString("project"); //public String myString;

2.in片段,使用保存的字符串

tvProjectName.setText(((JobCardViewActivity)getActivity()).myString);