我正在使用Android Studio在Android应用程序上工作,正在使用Viewpager类来显示Data。我想要做的是,当我单击包含回收者列表的第二个视图寻呼机中的项目时,其数据已加载到thirdViewpager类中。
为此,我创建了Viewpager
类,还为第三类创建了回收站适配器。
我已经显示了从firstviewpager类到第三个View-pager的单个数据项,在主Activity中创建了一个包含三个Viewpager类的方法;
public void selectTab(int position, String fname) {
initialize();
this.fname = fname;
viewPager.setCurrentItem(position);
}
public String getMyData() {
return fname;
}
像这样在第二个Viewpager适配器类中获取第一个方法,而在第二个Viewpager类中获取上述第二个方法。
MainFragment activity = (MainFragment) getActivity();
String name = activity.getMyData();
EdtName.setText(name);
现在我想要相同的东西,但是当我在第二个Viewpager类中单击“回收者列表”时,我想要的是,其数据已加载到第三个Viewpager回收者列表中。
我已经为第三个Viewpager实现了回收站适配器。
public class InvoicesRecyclerAdapter extends RecyclerView.Adapter<InvoicesRecyclerAdapter.ClientViewHolder> {
private List<GetSetProductsDetail> listProduct;
Context mContext;
RecyclerView mRecyclerView;
View itemView;
public InvoicesRecyclerAdapter(List<GetSetProductsDetail> listProduct, RecyclerView recyclerView) {
this.listProduct = listProduct;
mRecyclerView = recyclerView;
}
@Override
public ClientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.items_invoice, parent, false);
return new ClientViewHolder(itemView);
}
public class ClientViewHolder extends RecyclerView.ViewHolder {
public TextView lblPName;
public TextView lblRetail;
RelativeLayout layout;
public ClientViewHolder(View view) {
super(view);
lblPName = view.findViewById(R.id.lblInvoicePName);
lblRetail = view.findViewById(R.id.lblInvoiceRetail);
layout = view.findViewById(R.id.listinvoice);
}
}
@Override
public void onBindViewHolder(ClientViewHolder holder, final int position) {
MainFragment activityprodinvoice = (MainFragment) mContext;
String namebltp = activityprodinvoice.getMyProdData();
holder.lblPName.setText(namebltp);
//holder.lblPName.setText(listProduct.get(position).getNAME());
//holder.lblRetail.setText(listProduct.get(position).getRETAIL());
holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// displayingAlertDialog(position);
return false;
}
});
}
@Override
public int getItemCount() {
Log.v(InvoicesRecyclerAdapter.class.getSimpleName(), "" + listProduct.size());
return listProduct.size();
}
}
这是我的第三个Viewpager类
public class Invoices extends Fragment {
TextView EdtName;
EditText EditCurrDate;
EditText EdtCurrDate2;
RecyclerView recyclerViewProdInvoice;
InvoicesRecyclerAdapter invoicesRecyclerAdapter;
List<GetSetProductsDetail> listproductsInvoices;
TextView EdtNameProdInvoiuce;
DatabaseHelper databaseHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.invoice, container, false);
listproductsInvoices = new ArrayList<>();
recyclerViewProdInvoice=view.findViewById(R.id.recyclerviewinvoicesprod);
invoicesRecyclerAdapter = new InvoicesRecyclerAdapter(listproductsInvoices,recyclerViewProdInvoice);
recyclerViewProdInvoice.setItemAnimator(new DefaultItemAnimator());
recyclerViewProdInvoice.setItemAnimator(new DefaultItemAnimator());
recyclerViewProdInvoice.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewProdInvoice.setHasFixedSize(true);
recyclerViewProdInvoice.setAdapter(invoicesRecyclerAdapter);
databaseHelper = new DatabaseHelper(getActivity());
String date_n = new SimpleDateFormat("MMM / dd / yyyy", Locale.getDefault()).format(new Date());
EditCurrDate = view.findViewById(R.id.currdate);
EdtCurrDate2 = view.findViewById(R.id.currdate1);
EdtName = view.findViewById(R.id.txtclientname);
EdtNameProdInvoiuce = view.findViewById(R.id.edtprodinvoice);
MainFragment activity = (MainFragment) getActivity();
String name = activity.getMyData();
EdtName.setText(name);
MainFragment activityprodinvoice = (MainFragment) getActivity();
String namebltp = activityprodinvoice.getMyProdData();
EdtNameProdInvoiuce.setText(namebltp);
EdtNameProdInvoiuce.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Choose Option");
builder.setMessage("Are You Sure You Want To Add New Product?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainFragment main = (MainFragment) getActivity();
main.fromThirdToSecond(1);
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
return false;
}
});
EdtName.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Choose Option");
builder.setMessage("Are You Sure You Want To Add New Client?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainFragment main = (MainFragment) getActivity();
main.fromThirdToSecond(0);
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
return false;
}
});
EditCurrDate.setText(date_n);
EdtCurrDate2.setText(date_n);
EditCurrDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int year = mcurrentDate.get(Calendar.YEAR);
int month = mcurrentDate.get(Calendar.MONTH);
int day = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
// TODO Auto-generated method stub
Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
EditCurrDate.setText((selectedMonth + 1) + "/" + selectedDay + "/" + selectedYear);
}
}, year, month, day);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
EdtCurrDate2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int year1 = mcurrentDate.get(Calendar.YEAR);
int month1 = mcurrentDate.get(Calendar.MONTH);
int day1 = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
// TODO Auto-generated method stub
Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
EdtCurrDate2.setText((selectedMonth + 1) + "/" + selectedDay + "/" + selectedYear);
}
}, year1, month1, day1);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
return view;
}
}
这是我的主要活动
public class MainFragment extends AppCompatActivity {
DatabaseHelper databaseHelper;
ViewPager viewPager;
String fname;
String namebltp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
initialize();
databaseHelper = new DatabaseHelper(this);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Dynamic ERPMini");
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Clients"));
tabLayout.addTab(tabLayout.newTab().setText("Products"));
tabLayout.addTab(tabLayout.newTab().setText("Invoices"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final PagerAdapter adapter = new PagersAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void initialize() {
viewPager = findViewById(R.id.pager);
}
@Override
public void onResume() {
super.onResume();
viewPager = findViewById(R.id.pager);
}
public void fromThirdToSecond(int position) {
initialize();
viewPager.setCurrentItem(position);
}
public void selectTab(int position, String fname) {
initialize();
this.fname = fname;
viewPager.setCurrentItem(position);
}
public String getMyData() {
return fname;
}
public void selectTabProd(int position, String namebltp) {
initialize();
this.namebltp = namebltp;
viewPager.setCurrentItem(position);
}
public String getMyProdData() {
return namebltp;
}
}
我可以共享代码以更好地理解。