我有两个选项卡作为 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:
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);
}
}