[英]How to update LinearLayout when clicking recycler view item in a bottom sheet fragment?
我有一个线性布局视图,当您单击该视图时,它会弹出一个底部对话框片段,该对话框包含一个回收器视图(包含来自数据库的数据),并且当您单击底部表中的任何回收器视图项时对话框,它应该将其设置为选中并将线性布局更新为选定的回收器项目
这可能听起来令人困惑,所以我添加了我想要实现的目标的图片,以及我迄今为止编码的内容
从下面的图像中,线性布局被标记 (1)
单击线性布局 (1) 会打开带有回收器视图的底部工作表片段
单击任何回收站视图项目应使用所选项目更新线性布局 (1)
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);
}
}
包含要更新的线性布局的片段
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;
}
}
问题
是否可以通过选择底部工作表项来更新线性布局? 如果是,我该如何实现?
您应该将Listener
初始化为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
}
}
收到响应时初始化mAdapter
mAdapter = new AccountPickerAdapter(getActivity(),getChildFragmentManager(), userWalletList);
mRecyclerView.setAdapter(adapter);
mAdapter.setOnItemClickListener(AccountPickerDialog.this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.