如何在适配器的底部 sheet 片段中设置 TextView 占位符
How to set a TextView placeholder in Bottom sheet fragment from an adapter
我有一个像这样的 BottomSheetDialogFragmentclass
重命名钱包对话框
public class RenameWalletDialog extends com.google.android.material.bottomsheet.BottomSheetDialogFragment {
EditText editTextWalletName;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.rename_wallet_sheet, container, false);
editTextWalletName = (EditText) view.findViewById(R.id.custom_input_et);
view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Confirm button clicked", Toast.LENGTH_SHORT).show();
}
});
view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
view.findViewById(R.id.clear_field).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editTextWalletName.getText().clear();
}
});
return view;
}
}
我在我的适配器中创建了一个函数 class 就像这样
钱包适配器
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.wallet_item, parent, false);
final DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
dataObjectHolder.walletoptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(v.getContext(), "Option Click " + dataObjectHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
// Log.d(TAG, "position = " + dataObjectHolder.getAdapterPosition());
Context wrapper = new ContextThemeWrapper(mCtx, R.style.popupMenuStyle);
final PopupMenu popup = new PopupMenu(wrapper, v, Gravity.END);
popup.inflate(R.menu.poupup_menu);
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
showFragmentRenameWallet();
break;
case R.id.two:
//Do operation if menu_item_two
Toast.makeText(mCtx,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
});
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
DecimalFormat formatter = new DecimalFormat("#,###,###");
Double doubleBalance = (double) 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).getWalletid()));
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
renameWalletFragment.show(((FragmentActivity)mCtx).getSupportFragmentManager(), renameWalletFragment.getTag());
Bundle bundle = new Bundle();
String walletN = userWalletList.get(position).getWalletName();
bundle.putString("walletname", walletN);
renameWalletFragment.setArguments(bundle);
}
@Override
public int getItemCount() {
return userWalletList.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
private void showFragmentRenameWallet(){
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
renameWalletFragment.show(((FragmentActivity)mCtx).getSupportFragmentManager(), renameWalletFragment.getTag());
}
并像这样使用函数
showFragmentRenameWallet();
我想现在每个人都对我的代码逻辑工作原理有了基本的了解
我正在努力实现;
- 我想将底部 sheet 的编辑文本视图设置为用户点击
重命名选项的钱包名称
但我没有看到任何通过适配器执行此操作的可行方法,因为它实际上并不是适配器视图的一部分
有人知道如何做到这一点吗?
注意:在我的适配器的绑定视图持有者中,我得到这样的钱包名称
holder.walletname.setText(userWalletList.get(position).getWalletName());
如果您想通过 bundle 传递数据,请执行此操作
在适配器中:
Bundle bundle = new Bundle()
bundle.putString(SomeKey, value you want to send)
walletFragmentObject.setArguments(bundle)
然后在片段
String text = getArguments.getString(SomeKey)
更新:
如下所示更改您的方法:
private void showFragmentRenameWallet(ViewHolder dataObjectHolder) {
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
Bundle bundle = new Bundle();
String walletN = userWalletList
.get(dataObjectHolder.getAdapterPosition())
.getWalletName();
bundle.putString("walletname", walletN);
renameWalletFragment.show(
((FragmentActivity) mCtx).getSupportFragmentManager(),
renameWalletFragment.getTag()
);
}
从 onBindViewHolder 中删除与 bundle 和 RenameWalletDialog 相关的代码。
我有一个像这样的 BottomSheetDialogFragmentclass
重命名钱包对话框
public class RenameWalletDialog extends com.google.android.material.bottomsheet.BottomSheetDialogFragment {
EditText editTextWalletName;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.rename_wallet_sheet, container, false);
editTextWalletName = (EditText) view.findViewById(R.id.custom_input_et);
view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Confirm button clicked", Toast.LENGTH_SHORT).show();
}
});
view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
view.findViewById(R.id.clear_field).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editTextWalletName.getText().clear();
}
});
return view;
}
}
我在我的适配器中创建了一个函数 class 就像这样
钱包适配器
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.wallet_item, parent, false);
final DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
dataObjectHolder.walletoptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(v.getContext(), "Option Click " + dataObjectHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
// Log.d(TAG, "position = " + dataObjectHolder.getAdapterPosition());
Context wrapper = new ContextThemeWrapper(mCtx, R.style.popupMenuStyle);
final PopupMenu popup = new PopupMenu(wrapper, v, Gravity.END);
popup.inflate(R.menu.poupup_menu);
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
showFragmentRenameWallet();
break;
case R.id.two:
//Do operation if menu_item_two
Toast.makeText(mCtx,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
});
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
DecimalFormat formatter = new DecimalFormat("#,###,###");
Double doubleBalance = (double) 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).getWalletid()));
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
renameWalletFragment.show(((FragmentActivity)mCtx).getSupportFragmentManager(), renameWalletFragment.getTag());
Bundle bundle = new Bundle();
String walletN = userWalletList.get(position).getWalletName();
bundle.putString("walletname", walletN);
renameWalletFragment.setArguments(bundle);
}
@Override
public int getItemCount() {
return userWalletList.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
private void showFragmentRenameWallet(){
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
renameWalletFragment.show(((FragmentActivity)mCtx).getSupportFragmentManager(), renameWalletFragment.getTag());
}
并像这样使用函数
showFragmentRenameWallet();
我想现在每个人都对我的代码逻辑工作原理有了基本的了解
我正在努力实现;
- 我想将底部 sheet 的编辑文本视图设置为用户点击 重命名选项的钱包名称
但我没有看到任何通过适配器执行此操作的可行方法,因为它实际上并不是适配器视图的一部分
有人知道如何做到这一点吗?
注意:在我的适配器的绑定视图持有者中,我得到这样的钱包名称
holder.walletname.setText(userWalletList.get(position).getWalletName());
如果您想通过 bundle 传递数据,请执行此操作
在适配器中:
Bundle bundle = new Bundle()
bundle.putString(SomeKey, value you want to send)
walletFragmentObject.setArguments(bundle)
然后在片段
String text = getArguments.getString(SomeKey)
更新:
如下所示更改您的方法:
private void showFragmentRenameWallet(ViewHolder dataObjectHolder) {
RenameWalletDialog renameWalletFragment = new RenameWalletDialog();
Bundle bundle = new Bundle();
String walletN = userWalletList
.get(dataObjectHolder.getAdapterPosition())
.getWalletName();
bundle.putString("walletname", walletN);
renameWalletFragment.show(
((FragmentActivity) mCtx).getSupportFragmentManager(),
renameWalletFragment.getTag()
);
}
从 onBindViewHolder 中删除与 bundle 和 RenameWalletDialog 相关的代码。