将数据发送到单击按钮(位于第一个选项卡上)的第二个选项卡

时间:2016-12-09 19:37:44

标签: android tabs fragment android-scrollable-tabs

我有两个标签作为Tab1和Tab2。在按钮上单击我想发送列表行数据发送选项卡,以下是我的代码。

PagerAdapter.java

public class PagerAdapter extends FragmentStatePagerAdapter {

  int mNumOfTabs = 2;

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


@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            FragmentOne tab1 = new FragmentOne();
            return tab1;

        case 1:
            FragmentTwo tab2 = new FragmentTwo();
            return tab2;

        default:
            return null;
    }
}

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

选项卡1:

enter image description here

TAB 2: enter image description here

FragmentOne.java

  public class FragmentOne extends Fragment implements   
 FirstAdapter.MyClickListner {

private RecyclerView rvMovieList;
MovieModel movieModel;
private FirstAdapter firstAdapter;
private List<MovieModel> movieList = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);


    rvMovieList = (RecyclerView) view.findViewById(R.id.rvMovieList);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    rvMovieList.setLayoutManager(mLayoutManager);
    rvMovieList.setItemAnimator(new DefaultItemAnimator());


    new asyncGetMovie().execute();


    return view;
}

@Override
public void onButtonClick(int position) {

    Toast.makeText(getActivity(), "Clicked Item "+position, Toast.LENGTH_SHORT).show();
}

class asyncGetMovie extends AsyncTask<String, String, String> {
    String jsonString;
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... strings) {
        try {

        // here we get movieList


        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();

        firstAdapter = new FirstAdapter(movieList , FragmentOne.this);
        rvMovieList.setAdapter(firstAdapter);

    }
}

}

FragmentTwo.java     公共类FragmentTwo扩展了Fragment {

private TextView tvTitle;

public FragmentTwo() {
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_two, container, false);

    MovieModel movieModel = null;

    tvTitle = (TextView) view.findViewById(R.id.tvTitle);


    if (movieModel != null){

        movieModel = getArguments().getParcelable("data");

        System.out.println(" movieModel title "+movieModel.getTitle());
        tvTitle.setText(movieModel.getTitle());
    }

    return view;
}

}

FirstAdapter.java

 public class FirstAdapter extends    
    RecyclerView.Adapter<FirstAdapter.MyViewHolder> {

private  MyClickListner clickListner;
private List<MovieModel> moviesList;
FragmentOne fragament ;


public class MyViewHolder extends RecyclerView.ViewHolder {
   TextView tvTitle;
   Button btnDetails;
   MyClickListner clickListner;



   public MyViewHolder(View view) {
    super(view);
      tvTitle = (TextView) view.findViewById(R.id.tvTitle);
      btnDetails = (Button)view.findViewById(R.id.btnDetails);
   }
 }


public FirstAdapter(List<MovieModel> moviesList, FragmentOne fragmentOne) {
    this.moviesList = moviesList;
    this.fragament = fragmentOne;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_movie_list, parent, false);


    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    MovieModel movie = moviesList.get(position);
    holder.tvTitle.setText(movie.getTitle());


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

            fragament.onButtonClick(position);

        }
    });
}

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

public interface MyClickListner{
    public void onButtonClick(int position);
}

}

1 个答案:

答案 0 :(得分:0)

改变你的getItem()

@Override
public Fragment getItem(int position) {
switch (position) {
    case 0:
        FragmentOne tab1 = new FragmentOne();
        return tab1;

    case 1:
        FragmentTwo tab2 = new FragmentTwo();
        Bundle bundle = new Bundle();
        bundle.putExtra("KEY_OF_WHATEVER_YOU_WANT_TO_SEND","VALUE");
        tab2.setArguments(bundle);
        return tab2;

    default:
        return null;
}
}