【问题标题】:Fragment not showing up after click button from adapter从适配器单击按钮后片段未显示
【发布时间】:2019-12-07 03:58:38
【问题描述】:

我正在尝试在回收站视图的末尾添加一个按钮,并使该添加按钮显示为片段。以前它有效。但我不知道为什么它现在不起作用。根据谷歌和一些 StackOverflow 问题,我已将其更改为框架布局,但仍未显示。当我在 onClick 函数中放一个 toast 时,toast 出现了。有点奇怪。是不是其他地方有问题?

MessageAdapter.java

@Override
    protected void onBindViewHolder(final MessagesHolder holder, int position, Messages model) {
        holder.textViewMessages.setText(model.getMessages());
        holder.textViewUser.setText(model.getUser());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd yyyy, hh:mm a");
        String testtimeStamp = (model.getTimePosted().toDate().toString());
        String testtimeStamp2 = simpleDateFormat.format(new Date(testtimeStamp));
        holder.timeStamp.setText(testtimeStamp2);
        holder.bAdd.setVisibility(View.GONE);
        holder.container.setVisibility(View.GONE);

        //Compare size and add button at buttom of view
        if(position==getItemCount()-1){
            holder.bAdd.setVisibility(View.VISIBLE);
        }
        holder.bAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.container.setVisibility(View.VISIBLE);
                AppCompatActivity activity = (AppCompatActivity)v.getContext();
                Fragment replyFrag = new AddReplyFragment();
                Bundle extras = new Bundle();
                extras.putString("USER","Test_User");
                replyFrag.setArguments(extras);
                replyFrag.setArguments(extrasFromTitle);
                activity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.replyFragment,replyFrag).commit();
            }
        });
    }

    @NonNull
    @Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforuminterface,parent,false);
        EmojiCompat.init(new BundledEmojiCompatConfig(view.getContext()));
        return new MessagesHolder(view);
    }

    class MessagesHolder extends RecyclerView.ViewHolder{
        EmojiTextView textViewMessages;
        EmojiTextView textViewUser;
        TextView timeStamp;
        Button bAdd;
        FrameLayout container;
        public MessagesHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessages = itemView.findViewById(R.id.tvDescription_reply);
            textViewUser = itemView.findViewById(R.id.tvUsername);
            timeStamp = itemView.findViewById(R.id.tvTimestampMessages);
            bAdd = itemView.findViewById(R.id.bAdd);
            container = itemView.findViewById(R.id.replyFragment);
        }
    }

AddReplyFragment.java

public class AddReplyFragment extends Fragment {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    Button bAddReply;
    EditText reply;
    Timestamp now = Timestamp.now();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_add_reply_fragment,container,false);
        final String username = getArguments().getString("USER");
        final String forum_title = getArguments().getString("TITLE");
        final String forum_type = getArguments().getString("FORUM_TYPE");
        final String forum_id = getArguments().getString("FORUM_ID");

        reply = (EditText)view.findViewById(R.id.etReply);
        bAddReply = (Button)view.findViewById(R.id.bAddReply);
        String userReply = reply.getText().toString();
        final Map<String, Object> reply = new HashMap<>();
        reply.put("messages",userReply);
        reply.put("timePosted",now);
        reply.put("user",username);

        bAddReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.collection(forum_type).document(forum_id).collection("messages").add(reply);
            }
        });

        return view;
    }
}

cardviewforuminterface.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="4">

    <LinearLayout
        android:id="@+id/reply_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <androidx.emoji.widget.EmojiTextView
            android:id="@+id/tvDescription_reply"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="description"
            android:textSize="15sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/users_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="3">

        <androidx.emoji.widget.EmojiTextView
            android:id="@+id/tvUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="name"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvTimestampMessages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="timestamp"
            android:textIsSelectable="false"
            android:textSize="15sp" />

    </LinearLayout>

    <Button
        android:id="@+id/bAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Add Reply" />

    <FrameLayout
        android:id="@+id/replyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>

</LinearLayout>

activity_add_reply_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical"
    tools:context=".Fragment.AddReplyFragment">

    <TextView
        android:id="@+id/tvReply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:padding="10dp"
        android:text="Your Reply"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/etReply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="Type your reply here"
        android:inputType="textPersonName"
        android:padding="10dp" />

    <Button
        android:id="@+id/bAddReply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="Submit Reply" />
</FrameLayout>

【问题讨论】:

  • 你真的需要这个回复块作为一个片段吗?您如何看待将其用作 include 布局并更改按钮单击时的可见性?我认为这会更容易。
  • @GuilhermeLimaPereira 好主意,我现在就尝试实现它
  • @GuilhermeLimaPereira 它似乎有其他问题,我认为我坚持这个
  • @MUHAMADNURFIKRYBINMARJAMAL,您正在尝试在 RecyclerView 项目中添加片段。这样对吗?你能提供一张你想要达到的图像吗?
  • 你遇到过什么样的问题,@MUHAMADNURFIKRYBINMARJAMAL?

标签: android android-fragments android-recyclerview


【解决方案1】:

我在适配器中添加的代码。

holder.container.setId(newContainerId);
final int newContainerId = getUniqueId();

public int getUniqueId(){
        return (int) SystemClock.currentThreadTimeMillis();
    }

这是我最后的代码

public class MessagesAdapter extends FirestoreRecyclerAdapter<Messages, MessagesAdapter.MessagesHolder> {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    MessagesAdapter context;
    Bundle extrasFromInterface;

    public MessagesAdapter(@NonNull FirestoreRecyclerOptions<Messages> options,Bundle bundle) {
        super(options);
        extrasFromInterface = bundle;
    }

    @Override
    protected void onBindViewHolder(final MessagesHolder holder, int position, Messages model) {
        final int newContainerId = getUniqueId();

        holder.textViewMessages.setText(model.getMessages());
        holder.textViewUser.setText(model.getUser());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd yyyy, hh:mm a");
        String uncovertedTimeStamp = (model.getTimePosted().toDate().toString());
        String ConvertedTimeStamp = simpleDateFormat.format(new Date(uncovertedTimeStamp));
        holder.timeStamp.setText(ConvertedTimeStamp);
        holder.bAdd.setVisibility(View.GONE);
        holder.container.setVisibility(View.GONE);



        //Compare size and add button at buttom of view
        if(position==getItemCount()-1){
            holder.bAdd.setVisibility(View.VISIBLE);
        }
        holder.bAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.container.setId(newContainerId);
                holder.container.setVisibility(View.VISIBLE);
                AppCompatActivity activity = (AppCompatActivity)v.getContext();
                Fragment replyFrag = new AddReplyFragment();
                Bundle extras = new Bundle();
                extras.putString("USER","Test_User");
                replyFrag.setArguments(extras);
                replyFrag.setArguments(extrasFromInterface);
                activity.getSupportFragmentManager().beginTransaction()
                        .replace(newContainerId,replyFrag).commit();
            }
        });
    }

    public int getUniqueId(){
        return (int) SystemClock.currentThreadTimeMillis();
    }

    @NonNull
    @Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforuminterface,parent,false);
        EmojiCompat.init(new BundledEmojiCompatConfig(view.getContext()));
        context = this;
        return new MessagesHolder(view);
    }

    class MessagesHolder extends RecyclerView.ViewHolder{
        EmojiTextView textViewMessages;
        EmojiTextView textViewUser;
        TextView timeStamp;
        Button bAdd;

        FrameLayout container;
        public MessagesHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessages = itemView.findViewById(R.id.tvDescription_reply);
            textViewUser = itemView.findViewById(R.id.tvUsername);
            timeStamp = itemView.findViewById(R.id.tvTimestampMessages);
            bAdd = itemView.findViewById(R.id.bAdd);
            container = itemView.findViewById(R.id.replyFragment);
        }
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多