I have a Linear Layout view, when you click the view,it pops up a bottom dialog fragment, the dialog contains a recycler view (with data from a DB), and when you click on any of the recycler view item in the bottom sheet dialog, it should set it as selected and update the Linear Layout to the selected recycler item
This might sound confusing so I have added pictures of what i'm trying to achieve, as well as what I've coded so far
From the image below the Linear Layout is Labelled (1)
Clicking the Linear Layout (1) opens a bottom sheet fragment with a recycler view
Clicking on any recycler view item should update the Linear Layout (1) with the selected item
AccountPickerDialog.java
public class AccountPickerDialog extends BottomSheetDialogFragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<Wallet> userWalletList;
boolean mIsRequest = false;
private ShimmerFrameLayout mShimmerViewContainer;
public AccountPickerDialog() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.account_picker, container, false);
mShimmerViewContainer = view.findViewById(R.id.shimmer_view_container);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
// mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
// DividerItemDecoration.VERTICAL));
//initializing the walletlist
userWalletList = new ArrayList<>();
loadUserWallets();
return view;
}
private void loadUserWallets() {
if(mIsRequest)
return;
mIsRequest= true;
userWalletList.clear();
User user = SharedPrefManager.getInstance(getContext()).getUser();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.URL_USER_WALLET_LIST + user.getId(),
response -> {
mIsRequest = false;
try {
//converting the string to json object
JSONObject object=new JSONObject(response);
//getting data array from json response object
JSONArray array=object.getJSONArray("data");
for(int i=0;i<array.length();i++) {
//getting wallet object from json array
JSONObject userWallets=array.getJSONObject(i);
//adding the wallet to wallet list
userWalletList.add(new Wallet(
userWallets.getInt("id"),
userWallets.getInt("user_id"),
userWallets.getString("wallet_name"),
userWallets.getInt("wallet_id"),
userWallets.getInt("wallet_type"),
userWallets.getDouble("balance")
));
}
//creating adapter object and setting it to recyclerview
if (!isAdded()) return;
AccountPickerAdapter adapter = new AccountPickerAdapter(getActivity(),getChildFragmentManager(), userWalletList);
mRecyclerView.setAdapter(adapter);
// stop animating Shimmer and hide the layout
mShimmerViewContainer.stopShimmerAnimation();
mShimmerViewContainer.setVisibility(GONE);
// progressDialog.dismiss();
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> {
mIsRequest = false;
// progressDialog.dismiss();
// swipeRefreshLayout.setRefreshing(false);
Toast.makeText(getContext(),"No Internet Connection", Toast.LENGTH_SHORT).show();
});
//adding our stringrequest to queue
Volley.newRequestQueue(getContext()).add(stringRequest);
}
@Override
public void onResume() {
super.onResume();
mShimmerViewContainer.startShimmerAnimation();
}
@Override
public void onPause() {
mShimmerViewContainer.stopShimmerAnimation();
super.onPause();
}
}
AccountPickerAdapter.java
public class AccountPickerAdapter extends RecyclerView.Adapter<AccountPickerAdapter.DataObjectHolder> {
private static String TAG = AccountPickerAdapter.class.getSimpleName();
private Context mCtx;
private FragmentManager fragmentManager;
private ArrayList<Wallet> userWalletList;
private static AccountPickerAdapter.MyClickListener myClickListener;
public AccountPickerAdapter(Context mCtx, FragmentManager fragmentManager, ArrayList<Wallet> userWalletList) {
this.mCtx = mCtx;
this.fragmentManager = fragmentManager;
this.userWalletList = userWalletList;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder {
TextView walletname;
TextView walletbalance;
TextView walletid, destinationAccount;
public DataObjectHolder(View itemView) {
super(itemView);
walletname = (TextView) itemView.findViewById(R.id.account_picker_name);
walletbalance = (TextView) itemView.findViewById(R.id.account_picker_balance);
walletid = (TextView) itemView.findViewById(R.id.wallet_id);
destinationAccount = (TextView) itemView.findViewById(R.id.destination_account);
// Log.i(LOG_TAG, "Adding Listener");
// itemView.setOnClickListener(this);
}
// @Override
// public void onClick(View v) {
// myClickListener.onItemClick(getAdapterPosition(), v);
// }
}
// public void setOnItemClickListener(MyClickListener myClickListener) {
// this.myClickListener = myClickListener;
// }
@Override
public AccountPickerAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.account_picker_item, parent, false);
final AccountPickerAdapter.DataObjectHolder dataObjectHolder = new AccountPickerAdapter.DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(AccountPickerAdapter.DataObjectHolder holder, final int position) {
DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.##");
formatter.setDecimalSeparatorAlwaysShown(true);
formatter.setMinimumFractionDigits(2);
Double doubleBalance = userWalletList.get(position).getBalance();
String numberFormatBalance = formatter.format(doubleBalance);
holder.walletname.setText(userWalletList.get(position).getWalletName());
holder.walletbalance.setText(String.valueOf(numberFormatBalance));
holder.walletid.setText(String.valueOf(userWalletList.get(position).getId()));
holder.destinationAccount.setText(String.valueOf(userWalletList.get(position).getDestinationAccount()));
}
@Override
public int getItemCount() {
return userWalletList.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
Fragment Containing the Linear Layout to be updated
public class ReceivePageDialog extends BottomSheetDialogFragment {
View selectAccount; //The layout view to be updated
public ReceivePageDialog() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.receive_page, container, false);
selectAccount = view.findViewById(R.id.source_account_spinner); //Linear Layout view to be updated
selectAccount.setOnClickListener(v -> {
AccountPickerDialog accountPickerDialog = new AccountPickerDialog(); //This launches the wallet picker bottom sheet
accountPickerDialog.show(getChildFragmentManager(), accountPickerDialog.getTag());
});
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
bottomSheetDialog.setOnShowListener(dialog -> {
BottomSheetDialog dialogc = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = dialogc.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(Resources.getSystem().getDisplayMetrics().heightPixels);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
});
return bottomSheetDialog;
}
}
Question
Is it possible to update the linear layout by selecting a bottom sheet item? If yes, how can I achieve it?
You should init Listener
to ViewHolder
public class AccountPickerAdapter extends RecyclerView.Adapter<AccountPickerAdapter.DataObjectHolder> {
private static String TAG = AccountPickerAdapter.class.getSimpleName();
private Context mCtx;
private FragmentManager fragmentManager;
private ArrayList<Wallet> userWalletList;
private AccountPickerAdapter.MyClickListener myClickListener;
public AccountPickerAdapter(Context mCtx, FragmentManager fragmentManager, ArrayList<Wallet> userWalletList) {
this.mCtx = mCtx;
this.fragmentManager = fragmentManager;
this.userWalletList = userWalletList;
}
public Wallet getItemByPos(int pos) {
return userWalletList.get(pos);
}
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView walletname;
TextView walletbalance;
TextView walletid, destinationAccount;
private AccountPickerAdapter.MyClickListener myClickListener;
public DataObjectHolder(View itemView, AccountPickerAdapter.MyClickListener listener;) {
super(itemView);
walletname = (TextView) itemView.findViewById(R.id.account_picker_name);
walletbalance = (TextView) itemView.findViewById(R.id.account_picker_balance);
walletid = (TextView) itemView.findViewById(R.id.wallet_id);
destinationAccount = (TextView) itemView.findViewById(R.id.destination_account);
itemView.setOnClickListener(this);
myClickListener = listener;
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
@Override
public AccountPickerAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.account_picker_item, parent, false);
final AccountPickerAdapter.DataObjectHolder dataObjectHolder = new AccountPickerAdapter.DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(AccountPickerAdapter.DataObjectHolder holder, final int position) {
DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.##");
formatter.setDecimalSeparatorAlwaysShown(true);
formatter.setMinimumFractionDigits(2);
Double doubleBalance = userWalletList.get(position).getBalance();
String numberFormatBalance = formatter.format(doubleBalance);
holder.walletname.setText(userWalletList.get(position).getWalletName());
holder.walletbalance.setText(String.valueOf(numberFormatBalance));
holder.walletid.setText(String.valueOf(userWalletList.get(position).getId()));
holder.destinationAccount.setText(String.valueOf(userWalletList.get(position).getDestinationAccount()));
}
@Override
public int getItemCount() {
return userWalletList.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
public class AccountPickerDialog extends BottomSheetDialogFragment implements AccountPickerAdapter.MyClickListener {
@Override
public void onItemClick(int position, View v) {
Wallet wallet = mAdapter.getItemByPos(position)
View view = ((ReceivePageDialog)getParentFragmen()). selectAccount;
// find child view and update here
}
}
Init mAdapter
when you get response
mAdapter = new AccountPickerAdapter(getActivity(),getChildFragmentManager(), userWalletList);
mRecyclerView.setAdapter(adapter);
mAdapter.setOnItemClickListener(AccountPickerDialog.this)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.