具有多列的RecyclerView

时间:2019-01-14 14:03:59

标签: android android-recyclerview

我正在使用FirebaseUi填充recyclerview。这是我的数据库:

field1

1:“ a”  2:“ ab”  3:“ abc”

field2

1:“ b”  2:“ bc”  3:“ bcd”

field3

1:“ c”  2:“ cd”  3:“ cde”

通过GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false),我以这种方式在recyclerview中获得3列:

a b c

ab bc cd

abc bcd cde

我该如何检索具有这种结构的数据?

一个Ab abc

b bc bcd

c cd cde

谢谢您的配合。

1 个答案:

答案 0 :(得分:0)

编写此代码

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private FirebaseFirestore mDb;
private List<String> mList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.reccyclerView);
    mDb = FirebaseFirestore.getInstance();
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 3, GridLayout.VERTICAL, false));

    final NewAdapter adapter = new NewAdapter(mList);

    recyclerView.setAdapter(adapter);

    mDb.collection("eg")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    for (QueryDocumentSnapshot ds: task.getResult()) {
                        mList.add(ds.getString("1"));
                        mList.add(ds.getString("2"));
                        mList.add(ds.getString("3"));
                        adapter.notifyDataSetChanged();
                    }
                }
            });





}

}

自定义适配器

public class NewAdapter  extends  RecyclerView.Adapter<NewAdapter.Holder >{

List<String> mList;

public NewAdapter(List<String> mList) {
    this.mList = mList;
}

@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
    View view = inflater.inflate(R.layout.layout, viewGroup, false);
    return  new Holder(view);
}

@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {

    holder.text.setText(mList.get(i));

}

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

protected class Holder extends RecyclerView.ViewHolder {

    TextView text;
    public Holder(@NonNull View itemView) {
        super(itemView);

        text = itemView.findViewById(R.id.textView);
    }
}

}

布局单行

<RelativeLayout
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">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_centerHorizontal="true"
    tools:layout_editor_absoluteX="111dp"
    tools:layout_editor_absoluteY="63dp" />

mainLAyout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/reccyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我正在获得

a b c b bc bcd c cd cde