[英]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? 我正在尝试通过单击recyclerview项来打开片段,单击该项后得到的都是片段强度,在单击我的recyclerview后,我看到了要去的片段中的图像。有人知道我在做什么错吗?
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: 很高兴看到您布局xml,但是从我可以看到:
Your recycler view is not inside a fragment - it is inside the MainActivity . 您的回收者视图不在片段中 ,而是在MainActivity中 。 You set it with
setContentView(R.layout.activity_main);
您可以使用
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. 您将新片段放入
fragment_container
视图组中。 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. 简单易用 -为您的片段设置纯色背景(即
android:background="@android:color/black"
),并将android:clickable="true"
到其根布局。 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();
更正确的方法 -将您的回收站视图放在一个片段中,然后使用
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, recyclerFragment).commit();
将该fragment_container
放入fragment_container 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();
然后,当您调用
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FragA()).commit();
时,该项目将替换为单击项时的新片段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 为了能够返回到回收站视图,您还需要调用addToBackStack方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.