android - 如何动态更改Modal Bottomsheet android中的内容

标签 android android-layout android-recyclerview bottom-sheet

我有一个包含两个元素的 Bottomsheet xml,FollowCopy link.. 当我点击 Follow 时,它应该被改变动态地取消关注。我尝试使用 setText,但没有用。

请指导我如何更改底部对话框的文本。

这是我的 Bottomsheet xml 文件

<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:id="@+id/bottom_sheet_follow"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    android:foreground="?android:attr/selectableItemBackground"
    android:padding="16dp">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_mode_follow_grey_24dp"
        />
    <TextView
        android:id="@+id/followTView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:text="Follow"/>
</LinearLayout>
<LinearLayout
    android:id="@+id/bottom_sheet_copy"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    android:foreground="?android:attr/selectableItemBackground"
    android:padding="16dp">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_copy_grey_24dp"
        />
    <TextView
        android:id="@+id/copyLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:text="Copy link"/>
</LinearLayout>

Bottomsheet Java 代码

    View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);

            mDialog = new BottomSheetDialog(mContext);
            mDialog.setContentView(bottomSheetView);
            mDialog.show();

followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);

followBtnLayout .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    followTView.setText("Unfollow");
}
});

但是,Follow TextView (我的意思是 followTView)不会更改为Unfollow。 请告诉我哪里出错了...

提前致谢...

最佳答案

你可以用 bool 值来做,我已经用我的 imageview 做了。 可能对你有帮助

 //Initialize boolean as true by default
   public boolean showingFirst = true;

  holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showingFirst){
                    Toast.makeText(mActivity, "Item added to favorite", Toast.LENGTH_SHORT).show();
                    holder.ivFavorite.setImageResource(R.drawable.filled_heart);
                    showingFirst = false;
                }else {
                    holder.ivFavorite.setImageResource(R.drawable.ic_favorite);
                    Toast.makeText(mActivity, "Item removed from favorite", Toast.LENGTH_SHORT).show();
                    showingFirst = true;
                }

            }
        });

关于android - 如何动态更改Modal Bottomsheet android中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364133/

相关文章:

java - 从字符串中提取两行数字

java - 使用 volley 发出请求时未得到响应

android - 带有上下文操作栏的自定义 ListView 上的选定项目

android - 为什么在我从设备上卸载我的应用程序时显示此错误?

未显示 Android Studio 预览

java - 如何使用 layout_weight 获取 LinearLayout 的像素高度

android - 不推荐使用 getAdapterPosition()

android - 如何在使用 Livedata 时使用 Filter SearchView for RecyclerView?

java - 在 recyclerView 中滚动时如何保持复选框状态?

android - 如何在android动画中设置特定的帧