如果我的recycler-view的适配器绑定了基本类型Arraylist
(String
),我的ViewHolders
会正确填充,但如果我使用ArrayList
的{{1}}引用类型(Person
)它仅绑定ArrayList
中的最后一项(仅填充一个视图)。
查看持有人
public class PasswordViewHolder extends RecyclerView.ViewHolder {
TextView header;
TextView content;
public PasswordViewHolder(View itemView) {
super(itemView);
header = (TextView) itemView.findViewById(R.id.Header);
content = (TextView) itemView.findViewById(R.id.content);
}
public void Bind(String title) {
//Password currentPassword = Passwords.get(position);
content.setText("xxxxxxx");
header.setText(title);
}
}
适配器
public class PasswordAdapter extends RecyclerView.Adapter<PasswordAdapter.PasswordViewHolder> {
private static final String TAG = PasswordAdapter.class.getSimpleName();
private int mNumberItems;
private ArrayList<Password> Passwords = new ArrayList<>();
// private ArrayList<String> Strings = new ArrayList<>();
public PasswordAdapter(ArrayList<Password> passwords) {
// mNumberItems = Strings.size();
//Strings = passwords;
Passwords = passwords;
mNumberItems = getPasswords().size();
}
@Override
public PasswordViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
boolean shouldAttachToParentImmediately = false;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.password_item, viewGroup, shouldAttachToParentImmediately);
PasswordViewHolder viewHolder = new PasswordViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(PasswordViewHolder holder, int position) {
Log.d(TAG, "#" + position);
// Password p = getPasswords().get(position);
// String text = p.get_title();
//holder.Bind(text);
Password password = Passwords.get(position) ;
holder.header.setText(password.get_title());
holder.content.setText(password.get_category());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return mNumberItems;
}
public List<Password> getPasswords() {
return Passwords;
}
public void setPasswords(ArrayList<Password> passwords) {
Passwords = passwords;
//Strings = passwords;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
// ArrayList<String> message = new ArrayList<String>();
//message.add("FaceBook");
// message.add("Tweeter");
ArrayList<Password> Passwords = SetTestPasswords();
passwordListRV = (RecyclerView) findViewById(R.id.passwordListView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
passwordListRV.setHasFixedSize(true);
passwordAdapter = new PasswordAdapter(Passwords);
passwordListRV.setLayoutManager(layoutManager);
passwordListRV.setAdapter(passwordAdapter);
SetFLoatingButton();
}
Password_Item.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="wrap_content"
android:padding="5sp">
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorTertiary"
android:textStyle="bold"
android:text="@string/password_header"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:fontFamily="monospace"
android:textSize="20sp"
android:text="@string/password_description"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
答案 0 :(得分:0)
尝试更改
@Override
public int getItemCount() {
return mNumberItems;
}
到
@Override
public int getItemCount() {
return Passwords.size();
}
也删除
passwordListRV.setHasFixedSize(true);