如何在 RecyclerView 的一项中找到两个文本块?
Posted
技术标签:
【中文标题】如何在 RecyclerView 的一项中找到两个文本块?【英文标题】:How can I locate two text blocks in one item in RecyclerView? 【发布时间】:2019-10-30 21:57:45 【问题描述】:我已经创建了带有视图持有者和一个列表的 RecyclerView。但我需要一个 textView 开始,第二个结束。我该怎么做?
我用两个 TextViews 创建了布局,那里已经有位置了。但我应该为第一个 textView animalNames 和第二个 - peopleNames 设置。 这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> animalNames = new ArrayList<>();
animalNames.add("Horse");
animalNames.add("Cow");
animalNames.add("Camel");
animalNames.add("Sheep");
animalNames.add("Goat");
ArrayList<String> peopleNames = new ArrayList<>();
animalNames.add("Harry");
animalNames.add("John");
animalNames.add("Sergey");
animalNames.add("Steve");
animalNames.add("Woody");
RecyclerView recyclerView = findViewById(R.id.rvAnimals);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, animalNames);
recyclerView.setAdapter(adapter);
adapter.setClickListener(new MyRecyclerViewAdapter.ItemClickListener()
@Override
public void onItemClick(View view, int position)
Toast.makeText(getApplicationContext(), "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
);
MyRecyclerViewAdapter.java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
MyRecyclerViewAdapter(Context context, List<String> data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = mInflater.inflate(R.layout.custom_view, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(ViewHolder holder, int position)
String animal = mData.get(position);
holder.myTextView.setText(animal);
@Override
public int getItemCount()
return mData.size();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
TextView myTextView;
TextView myTextView2;
ViewHolder(View itemView)
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
myTextView2 = itemView.findViewById(R.id.tvAnimalName2);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
String getItem(int id)
return mData.get(id);
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
public interface ItemClickListener
void onItemClick(View view, int position);
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/tvAnimalName"
android:layout_
android:layout_
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:textSize="20sp" />
<TextView
android:id="@+id/tvPersonName"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:layout_marginEnd="8dp"
android:textSize="20sp" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvAnimals"
android:layout_
android:layout_/>
</RelativeLayout>
【问题讨论】:
【参考方案1】: Pass the person array list also in the constructor of the recycler view
//like in activity
RecyclerView recyclerView = findViewById(R.id.rvAnimals);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, animalNames,personNames);
//in adapter class
create a list for person also
private List<String> mData;
private List<String> mpersonData;
// adapter constructor
MyRecyclerViewAdapter(Context context, List<String> data,List<String> mpersonData)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mpersonData = mpersonData;
//onbind view holder
@Override
public void onBindViewHolder(ViewHolder holder, int position)
String animal = mData.get(position);
String person = mData.get(position);
holder.myTextView.setText(animal);
holder.myTextView.setText(person);
//change the tv id name to person
ViewHolder(View itemView)
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
// just change this to person name
myTextView2 = itemView.findViewById(R.id.tvPersonalName);
itemView.setOnClickListener(this);
Now you will set person name also in the recycler view text field
Paste this code instead of relative layout use linear layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/tvAnimalName"
android:layout_
android:layout_
android:weight="1"
android:gravity="start"
android:layout_marginStart="8dp"
android:textSize="20sp" />
<TextView
android:id="@+id/tvPersonName"
android:layout_
android:layout_
android:weight="1"
android:gravity="end"
android:layout_marginEnd="8dp"
android:textSize="20sp" />
</LinearLayout>
and you can have two columns as mentioned in the question
【讨论】:
非常感谢。但是人和动物的名字在同一列。我想动物会在第一列,人会在第二列。 您的意思是您正在为视图持有者询问布局。如果是这样,你应该粘贴布局 我该怎么做? 你可以粘贴适配器的 xml 布局,以便我可以看到并告诉如何做到这一点@Sergey 我编辑了我的答案@Sergey。请看一看。让我知道它的工作与否以上是关于如何在 RecyclerView 的一项中找到两个文本块?的主要内容,如果未能解决你的问题,请参考以下文章
在 android 的 RecyclerView 中可点击的一项