我是位机器人新手,当用户单击父片段的列表视图时,我尝试实现details子片段。但是,我不知道该如何实现。父片段在viewpager中。请帮忙。
这是下面的viewpager代码内的父片段:
public class OpenTradesFragment extends Fragment implements OTClickListener {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String OPEN_TRADES_REQUEST_URL = "https://google.com;
String BUYING_DATE="BD :";
String TRADE_LOCKED="Trade Locked";
String UNLOCK="UNLOCK";
String BUYING_PRICE ="Buying Price";
public static final String FREETRADE = "freeTrade";
ListView openTradesListView;
ProgressBar otProgressBar;
OpenTradesAdapter openTradesAdapter;
ArrayList<OpenTrades> OpenTradesList = new ArrayList<>();
static int paise;
SharedPreferences sharedPref;
private TextView mEmptyStateTextView;
public OpenTradesFragment() {
}
OTClickListener mCallback;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_open_trades, container, false);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
sharedPref = this.getActivity().getSharedPreferences(uid, Context.MODE_PRIVATE);
openTradesListView = rootView.findViewById(R.id.open_trades_listview);
otProgressBar = rootView.findViewById(R.id.ot_progressbar);
otProgressBar.setVisibility(View.VISIBLE);
mEmptyStateTextView = rootView.findViewById(R.id.empty_view);
openTradesListView.setEmptyView(mEmptyStateTextView);
new OpenTradesAsyncTask().execute();
return rootView;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onOpenTradeClicked(int position) {
OTDetailsFragment detailsFragment = new OTDetailsFragment();
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction()
.replace(R.id.otdetails_container, detailsFragment)
.addToBackStack(null)
.commit();
}
这是下面的适配器代码:
public class OpenTradesAdapter extends ArrayAdapter<OpenTrades> {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private ArrayList<OpenTrades> openTradesArrayList;
public static final int TYPE_LOCKED = 1;
public static final int TYPE_UNLOCKED = 0;
public static String tradeNumber;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
OTClickListener mListener;
public OpenTradesAdapter(@NonNull Context context, @NonNull ArrayList<OpenTrades> openTrades, OTClickListener otClickListener) {
super(context, 0, openTrades);
openTradesArrayList = openTrades;
mListener = otClickListener;
}
@Override
public int getItemViewType(int position) {
OpenTrades currentOpenTrade = openTradesArrayList.get(position);
if (currentOpenTrade.isLocked) {
return TYPE_LOCKED;
} else {
return TYPE_UNLOCKED;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final OpenTrades currentOpenTrade = openTradesArrayList.get(position);
if (getItemViewType(position) == TYPE_LOCKED) {
ViewHolderLocked holderLocked;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.open_trades_list_item_locked, parent, false);
holderLocked = new ViewHolderLocked();
holderLocked.t1 = convertView.findViewById(R.id.stock_name_locked);
holderLocked.t2 = convertView.findViewById(R.id.bought_on_locked);
holderLocked.t3 = convertView.findViewById(R.id.buying_date_locked);
holderLocked.i1 = convertView.findViewById(R.id.open_trades_icon_locked);
holderLocked.b1 = convertView.findViewById(R.id.unlock_now_button);
holderLocked.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tradeNumber = currentOpenTrade.getTradeNumber();
startPayment();
}
});
convertView.setTag(holderLocked);
} else {
holderLocked = (ViewHolderLocked) convertView.getTag();
}
holderLocked.i1.setImageResource(currentOpenTrade.getOpenImage());
holderLocked.t1.setText(currentOpenTrade.getOpenStock());
holderLocked.t2.setText(currentOpenTrade.getOpenBuyingDate());
holderLocked.t3.setText(currentOpenTrade.getOpenBD());
holderLocked.b1.setText(currentOpenTrade.getOpenBuyingPrice());
holderLocked.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tradeNumber = currentOpenTrade.getTradeNumber();
startPayment();
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tradeNumber = currentOpenTrade.getTradeNumber();
startPayment();
}
});
return convertView;
}
else {
ViewHolderUnlocked holderUnLocked;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.open_trades_list_item_unlocked, parent, false);
holderUnLocked = new ViewHolderUnlocked();
holderUnLocked.t1 = (TextView) convertView.findViewById(R.id.stock_name);
holderUnLocked.t2 = (TextView) convertView.findViewById(R.id.bought_on);
holderUnLocked.t3 = (TextView) convertView.findViewById(R.id.buying_date);
holderUnLocked.t4 = (TextView) convertView.findViewById(R.id.open_trade_buying_price);
holderUnLocked.t5 = (TextView) convertView.findViewById(R.id.open_trade_bp);
holderUnLocked.i1 = (ImageView) convertView.findViewById(R.id.open_trades_icon);
convertView.setTag(holderUnLocked);
} else {
holderUnLocked = (ViewHolderUnlocked) convertView.getTag();
}
holderUnLocked.i1.setImageResource(currentOpenTrade.getOpenImage());
holderUnLocked.t1.setText(currentOpenTrade.getOpenStock());
holderUnLocked.t2.setText(currentOpenTrade.getOpenBuyingDate());
holderUnLocked.t3.setText(currentOpenTrade.getOpenBD());
holderUnLocked.t4.setText(currentOpenTrade.getOpenBuyingPrice());
holderUnLocked.t5.setText(currentOpenTrade.getOpenBP());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onOpenTradeClicked(position);
}
});
return convertView;
}
}
public static class ViewHolderLocked {
TextView t1, t2, t3;
ImageView i1;
Button b1;
}
public static class ViewHolderUnlocked {
TextView t1, t2, t3, t4, t5;
ImageView i1;
}