简体   繁体   中英

open a Fragment by clicking on a recyclerview item and move to this fragment

i'm trying to open a Fragment by clicking on a recyclerview item, what i'm getting after clicking on item is both fragment toghter, i see the image from the fragment that i want to go to after the clicking on my recyclerview..Does anyone have any idea what i'm doing wrong?

public class MainActivity extends AppCompatActivity {

    private FragmentManager manger;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ArrayList<Chat> chatList = new ArrayList<>();
        chatList.add(new Chat("send post card", " ", R.drawable.sentpostcard));
        chatList.add(new Chat("send greeting card", "happy holiday", R.drawable.greetingcard));
        chatList.add(new Chat("special designs", "choose a card", R.drawable.special));


        ChatAdapter adapter = new ChatAdapter(chatList);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.chatList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

    }



    // 1
    public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ChatViewHolder>{

        // 7
        private ArrayList<Chat> chats;

        // 8
        public ChatAdapter(ArrayList<Chat> chats) {
            this.chats = chats;
        }

        // 10
        @Override
        public ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = getLayoutInflater();
            View v = inflater.inflate(R.layout.chat_list_item, parent, false); // false == do not attach to root
            return new ChatViewHolder(v);
        }

        // 11
        @Override
        public void onBindViewHolder(ChatViewHolder holder, int position) {
            holder.bind( chats.get(position) );
        }

        // 9
        @Override
        public int getItemCount() {
            return chats.size();
        }

        // 2
        public class ChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

            //4
            private TextView textName, textChat;
            private ImageView imageChat;

            //we need to remember a chat for each view holder and we bind the chat object using the bind function
            //this will be useful later in the onClick Listener
            private Chat c;

            // view is the layout view ==> it contains all of the view in the layout file
            // 3
            public ChatViewHolder(View itemView) {
                super(itemView);
                // 5
                textName = (TextView) itemView.findViewById(R.id.textName);
                textChat = (TextView) itemView.findViewById(R.id.textChat);
                imageChat = (ImageView) itemView.findViewById(R.id.imageChat);



                // 12
                itemView.setOnClickListener(this);
            }

            // 6
            public void bind(Chat chat){
                c = chat;
                textName.setText(chat.getName());
                textChat.setText(chat.getText());
                imageChat.setImageResource(chat.getImage());
            }

            @Override
            public void onClick(View v) {

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FragA()).commit();


            }

            }
        }
    }

Would be nice to see you layout xml, but from what I can see:

Your recycler view is not inside a fragment - it is inside the MainActivity . You set it with setContentView(R.layout.activity_main);

When you do

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FragA()).commit();

you put your new fragment in the fragment_container view group. But this does not affect the activity at all. So, you end up with both views visible .

There are two ways to fix this:

The easy and hacky one - set solid background (ie android:background="@android:color/black" ) to your fragment and add android:clickable="true" to it's root layout. Then you won't be able to see or interact with the activity that will be beneath your fragment.

More proper way - put your recycler view in a fragment, put that fragment in the fragment_container with getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, recyclerFragment).commit(); . Then it will be replaced with your new fragment on item click when you call getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FragA()).commit();

To be able to go back to the recycler view you will also need to call addToBackStack method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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