我正在使用Firebase在我的应用程序上实现一些逻辑。
我需要使用循环视图和自定义适配器为我的用户设置一些信息,每次我做一个prepareData(),只显示1个元素而不是我的所有数组,我认为它可以有一些东西与我的布局有关,因为在适配器上我收到了所有信息。
所以在我的主要活动中我有这个:
package com.esmad.pdm.friendlymanager;
import android.content.Intent;
import android.icu.text.MessagePattern;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.esmad.pdm.friendlymanager.model.Event;
import com.esmad.pdm.friendlymanager.model.EventViewModel;
import com.esmad.pdm.friendlymanager.model.Participant;
import com.esmad.pdm.friendlymanager.other.EventGameAdapter;
import com.esmad.pdm.friendlymanager.other.Participants;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MatchInvitation extends AppCompatActivity {
FirebaseDatabase database;
DatabaseReference myRef;
DatabaseReference user;
private FirebaseAuth auth;
String id;
String username;
RecyclerView ps;
Participants participantAdapter;
TextView title1;
ArrayList<Participant> participants;
boolean exists;
String matchId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_invitation);
participants = new ArrayList<>();
title1 = (TextView)findViewById(R.id.textView3);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Matches");
user = database.getReference("Users");
auth = FirebaseAuth.getInstance();
id = auth.getCurrentUser().getUid();
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null) {
if (extras.containsKey("matchId")) {
matchId = getIntent().getStringExtra("matchId");
}
}
ps = (RecyclerView)findViewById(R.id.participants);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
ps = (RecyclerView)findViewById(R.id.participants);
ps.setHasFixedSize(true);
ps.setLayoutManager(linearLayoutManager);
participantAdapter = new Participants(getApplicationContext(), participants);
ps.setAdapter(participantAdapter);
loadParticipants();
//loadDataTeamA();
//loadDataTeamB();
}
public void remove(View view){
myRef.child(matchId).child("Participants")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(id)){
for(Participant p: participants){
if(p.getUserId().equals(id)){
participants.remove(p);
}
}
snapshot.getRef().setValue(null);
}
}
participantAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}
});
}
public void add(View view){
getUser();
}
public void loadParticipants(){
myRef.child(matchId).child("Participants")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Participant x = new Participant(snapshot.getKey(),snapshot.getValue().toString());
participants.add(x);
}
participantAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}
});
}
public void participantExist(final String id){
myRef.child("Participants").child("matchId")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(id)){
exists = true;
}
}
participantAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}
});
}
public void getUser() {
user.child(id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getKey().equals(id)) {
username = dataSnapshot.child("username").getValue().toString();
myRef.child(matchId).child("Participants").child(id).setValue(username);
Participant x = new Participant(id,username);
participants.add(x);
Log.d("arrqwe",String.valueOf(participants.size()));
}
if(participants.size() > 0){
title1.setVisibility(View.VISIBLE);
}
participantAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}
});
}
}
然后在我的customAdapter上我有:
public class Participants extends RecyclerView.Adapter<Participants.ViewHolder> {
private ArrayList<Participant> participants;
private Context context;
private View cv;
public Participants(Context context, ArrayList<Participant> participants) {
this.participants = participants;
this.context = context;
}
@Override
public Participants.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.username_list, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final Participants.ViewHolder viewHolder, final int i) {
Log.d("participante",participants.get(i).getUsername());
viewHolder.name.setText(participants.get(i).getUsername());
}
@Override
public int getItemCount() {
if (participants != null) {
return participants.size();
} else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView name;
public ViewHolder(View view) {
super(view);
cv = view;
name = (TextView)view.findViewById(R.id.username);
}
}
}
我的单项行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
最后我的活动布局
<?xml version="1.0" encoding="utf-8"?>
<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="com.esmad.pdm.friendlymanager.MatchInvitation">
<android.support.v7.widget.RecyclerView
android:id="@+id/teamA"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toLeftOf="@+id/teamB"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="7dp"
app:layout_constraintBottom_toBottomOf="@+id/teamB"
android:layout_marginEnd="13dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/teamB">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/teamB"
android:layout_width="174dp"
android:layout_height="0dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="@+id/textView4"
android:layout_marginBottom="15dp">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/participants"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="34dp"
android:layout_marginEnd="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="34dp"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button6"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="participants"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="6dp"
app:layout_constraintTop_toBottomOf="@+id/button5"
app:layout_constraintLeft_toLeftOf="@+id/button5" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:text="TeamB"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="65dp"
app:layout_constraintRight_toRightOf="@+id/participants"
android:layout_marginTop="17dp"
app:layout_constraintTop_toBottomOf="@+id/participants"
app:layout_constraintLeft_toLeftOf="@+id/teamB" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="61dp"
android:layout_marginStart="58dp"
android:text="TeamA"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toRightOf="@+id/teamA"
android:layout_marginTop="288dp"
app:layout_constraintLeft_toLeftOf="@+id/participants"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.487" />
<ImageButton
android:id="@+id/button5"
android:layout_width="44dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:onClick="add"
android:src="@drawable/ic_check_black_24dp"
android:tint="@color/emerald"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toLeftOf="@+id/button6"
tools:layout_constraintRight_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/button6"
android:layout_marginEnd="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/button6" />
<ImageButton
android:id="@+id/button6"
style="@android:style/Widget.DeviceDefault.ImageButton"
android:layout_width="44dp"
android:layout_height="0dp"
android:layout_marginEnd="75dp"
android:layout_marginStart="75dp"
android:onClick="remove"
android:src="@drawable/ic_clear_black_24dp"
android:tint="@color/red"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toLeftOf="@+id/teamB"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginEnd="137dp"
android:layout_marginStart="137dp"
android:layout_marginTop="32dp"
android:text="best teams"
android:visibility="invisible"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button8"
android:layout_width="88dp"
android:layout_height="48dp"
android:text="random"
android:visibility="invisible"
app:layout_constraintBaseline_toBaselineOf="@+id/button7"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
android:layout_marginStart="11dp"
app:layout_constraintLeft_toRightOf="@+id/button7" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:5)
在单个项目行中,将LinearLayout android:layout_height
设置为wrap_content
。 android:layout_height="match_parent"
导致每个项目占据整个屏幕。
答案 1 :(得分:1)
更新你的mysingleitemrow.xml
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />