[英]How can I change the info below a RecyclerView depending of the item selected?
I'm making a recycler view with a linear layout horizontal, it looks like this: HorizontalRecyclerView我正在制作一个水平线性布局的回收器视图,它看起来像这样: HorizontalRecyclerView
As you can see under it there are two text view that I want to fill with the item info when the user press an item.正如你在它下面看到的,当用户按下一个项目时,我想用项目信息填充两个文本视图。 I've do two Adapters to show a menu before, this way when you make a long click the item change its layout, but it doesn´t change the information below.
我之前做过两个适配器来显示一个菜单,这样当你长按项目时会改变它的布局,但它不会改变下面的信息。 The way of manage a selected item usually is using the adapter like this:
管理选定项的方式通常是使用这样的适配器:
if (holder instanceof AdaptadorProyectosViewHolder) {
((AdaptadorProyectosViewHolder) holder).tv_nombreProyecto.setText(nombreProyecto.toString());
((AdaptadorProyectosViewHolder) holder).tv_nombreClienteProyecto.setText("UN CLIENTE");
//When it's a long click the menu is shown in the place of the item
((AdaptadorProyectosViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mostrarMenu(position); //showMenu(position)
return true;
}
});
What this do is that the layout of an item change (displaying more information, options,etc), but i want the info to be below the Recycler View, not inside it into an item.这样做是项目的布局发生变化(显示更多信息、选项等),但我希望信息位于回收站视图下方,而不是在项目内部。 There's any way to do it?
有什么办法吗?
Here's the xml of the fragment that contains the Recycler View:这是包含 Recycler 视图的片段的 xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Vistas.Proyectos">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_proyectos"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
Relying on little details from your question, here's a quick solution.依靠您问题中的小细节,这是一个快速的解决方案。 Add an onClickListener to your adapter:
将 onClickListener 添加到您的适配器:
public interface OnItemClickListener {
void onItemClick(int position);
}
Declare the listener's variable and add it to the adapter's constructor:声明监听器的变量并将其添加到适配器的构造函数中:
private final OnItemClickListener listener;
public MyAdapter ( List<Item> items , Context context , OnItemClickListener listener ) {
mItems = items;
mContext = context;
this.listener = listener;
}
Inside ViewHolder, add OnClickListener:在 ViewHolder 中,添加 OnClickListener:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView txt;
public ViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt = itemView.findViewById(R.id.tvItemTitle);
}
@Override
public void onClick(View v)
{
listener.onItemClick(getAdapterPosition());
}
}
After adding these to the adapter, now move to the activity or fragment hosting the recyclerview and implement the onClickListener you just created.将这些添加到适配器后,现在移动到托管 recyclerview 的活动或片段并实现您刚刚创建的 onClickListener。 Here, I used a fragment:
在这里,我使用了一个片段:
public class LibraryFragment extends MainActivityFragment implements MyAdapter.OnItemClickListener
Add the following to the newly implemented method:将以下内容添加到新实现的方法中:
@Override
public void onItemClick(int position)
{
mtext.setText(mItems.get(position).getName());
}
All done.全部完成。 Notice that
mItems
is your list of items, and mtext
is the TextView
hosting the results, Also, note the getName()
method is a method I created.请注意,
mItems
是您的项目列表, mtext
是托管结果的TextView
,另外,请注意getName()
方法是我创建的方法。 Replace mItems, mtext and getName with your own codes: Extra note: you need to add the listener to the adapter in the fragment:将 mItems、mtext 和 getName 替换为您自己的代码: 额外说明:您需要将监听器添加到片段中的适配器:
adapter = new MyAdapter( mItems , getActivity() , this );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.