【发布时间】:2020-06-13 22:20:56
【问题描述】:
我有一个线性布局视图,当您单击该视图时,它会弹出一个底部对话框片段,该对话框包含一个回收器视图(包含来自数据库的数据),并且当您单击任何回收器视图项时底部的工作表对话框,它应该将其设置为选中并将线性布局更新为选定的回收器项目
这可能听起来令人困惑,所以我添加了我正在努力实现的目标的图片,以及我到目前为止编写的代码
从下图中线性布局被标记 (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;
}
}
问题
是否可以通过选择底部工作表项来更新线性布局?如果是,我该如何实现?
【问题讨论】:
-
嗯,您已经完成了为什么需要将其更改为 spiner。底片适合材料设计
-
我不打算更改为微调器本身,我只是想在选择底部工作表项时更新线性布局视图
-
在BottomSheetDialog中单击时,您应该将标题更改为如何更新项目