如何在Android RecyclerView中将onBindViewHolder与多个项目一起使用

时间:2019-08-16 14:18:25

标签: java android firebase android-recyclerview google-cloud-firestore

我正在用CardView的内部创建一个RecyclerView,每个卡都有标题,描述和图像。使用onBindViewHolder仅允许我添加标题,而不能添加其他标题。这是我尝试的方法。

这是我的适配器

public class userDressAdapter extends RecyclerView.Adapter<userDressAdapter.ViewHolder> {

    private LayoutInflater layoutInflater;
    private List<String> data;
    private Object ViewGroup;

    userDressAdapter(Context context, List<String> data) {
        this.layoutInflater = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.dress_recycler_view, (android.view.ViewGroup) ViewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        String title = data.get(position);
        ViewHolder.dressTitle.setText(title);
        ViewHolder.dressDescription.setText(title);


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        static TextView dressTitle;
        static TextView dressDescription;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            dressTitle = itemView.findViewById(R.id.userDressTitleText);
            dressDescription = itemView.findViewById(R.id.userDressDescriptionText);
        }
    }
}

我将其用作视图的主要适配器,这是我从主要Java类中调用它的方式:

public class UserDressView extends AppCompatActivity {
    private static final String TAG = "UserDressView";
    RecyclerView recyclerView;
    com.innoventiq.fostania.userDressAdapter userDressAdapter;
    ArrayList<String> items;
    ArrayList<String> description;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_dress_view);
        items = new ArrayList<>();
        description = new ArrayList<>();
        ReadDressData();
    }

    public void AddDressToList(String theData) {
        items.add(theData);
        recyclerView = findViewById(R.id.userDressRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userDressAdapter = new userDressAdapter(this, items);
        recyclerView.setAdapter(userDressAdapter);
    }

    public void AddDressDescriptionToList(String theData) {
        description.add(theData);
        recyclerView = findViewById(R.id.userDressRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userDressAdapter = new userDressAdapter(this, description);
        recyclerView.setAdapter(userDressAdapter);
    }

    public void ReadDressData() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("dress")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                AddDressToList(String.valueOf(document.getData().get("title")));
                                AddDressDescriptionToList(String.valueOf(document.getData().get("description")));
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                    }
                });
    }
}

由于我是android studio和JAVA的新手,所以没有找到相关指南。我添加了2个函数,这些函数将不同的列表添加到Adapter,但结果是一团糟。文本随处添加,并且项目重复。

1 个答案:

答案 0 :(得分:3)

将项目添加到 @override Widget build(BuildContext context) { return GestureDetector( onTap: () => _buttonCallBack(), child: ClipOval( child: FractionallySizedBox( heightFactor : 0.4, widthFactor : 0.6, child : Container( color: Colors.lightGreen, child : Center( child: Text( "Start", style: new TextStyle(fontSize: 36.0), ) ) ) ), ), ); ,然后仅将ArrayList<Dress>设置一次。

通过DressAdapter字段,可以对卡视图进行数据绑定。

@Bindable

添加项目:

class Dress extends BaseObservable {

    private String title;

    private String desc;

    /** Constructor */
    public Dress() {}

    /** Constructor, new instance from Firestore {@link QueryDocumentSnapshot} */
    public Dress(@NonNull QueryDocumentSnapshot snapshot) {
        this.fromSnapshot(snapshot);
    }

    public void setTitle(@NonNull String value) {
        boolean changed = !TextUtils.equals(this.title, value);
        if(changed) {
            this.title = value;
            notifyPropertyChanged(BR.title);
        }
    }

    public void setDesc(@NonNull String value) {
        boolean changed = !TextUtils.equals(this.desc, value);
        if(changed) {
            this.desc = value;
            notifyPropertyChanged(BR.desc);
        }
    }

    @Bindable
    public String getTitle() {
        return this.title;
    }

    @Bindable
    public String getDesc() {
        return this.desc;
    }

    public void fromSnapshot(@NonNull QueryDocumentSnapshot snapshot) {
        this.setTitle(String.valueOf(snapshot.getData().get("title")));
        this.setDesc(String.valueOf(snapshot.getData().get("description")));
    }
}
每个项目都被调用

ArrayList<Dress> mItems = new ArrayList<>(); ... if (task.isSuccessful()) { for (QueryDocumentSnapshot snapshot : Objects.requireNonNull(task.getResult())) { mItems.add(new Dress(snapshot)); } recyclerView.setAdapter(new dressAdapter(mItems)); }

onBindViewHolder()