繁体   English   中英

如何在Modal Bottomsheet Android中动态更改内容

[英]How to dynamically change content inside Modal Bottomsheet android

我有一个Bottomsheet xml,其中包含两个元素,“ 关注”和“ 复制”链接 ..当我单击“ 关注”时 ,应将其动态更改为“ 取消关注” 我尝试使用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>

底表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");
}
});

但是,“ 跟随 Textview”(我的意思是followTView)并未更改为Unfollow 请告诉我我哪里出问题了...

提前致谢...

你可以用布尔值做到这一点,我已经用我的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;
                }

            }
        });

只是一个猜测。 您是否尝试重新排列代码? 也许点击监听器应该在调用“ mDialog.show()”之前首先加载。

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

        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");
            }
        });

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM