我在当前项目中使用FirebaseRecyclerAdapter
多个ViewType,它显示黑屏!!?
任何人都可以帮我解决这个问题吗?
在代码或布局中出现问题?或其他一些东西
FirebaseRecyclerAdapter的问题我们发现的是它的方式 写的永远不会支持多种视图类型。这是真的吗?
我的java代码:
private void displayChatMessages() {
RecyclerView listOfMessages = (RecyclerView)findViewById(R.id.myRv);
mAdapter = new FirebaseRecyclerAdapter<ChatMessage, RecyclerView.ViewHolder>(
ChatMessage.class, R.layout.list_item_chat, RecyclerView.ViewHolder.class, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateViewHolder(final RecyclerView.ViewHolder viewHolder, final ChatMessage user,
final int position) {
switch (user.getMessageUser()) {
case "test@test.com":
HolderMe vh1 = (HolderMe)viewHolder;
populateType1(vh1, user, position);
toaa1("cbn");
break;
case "test2@test2.com":
populateType2((HolderYou) viewHolder, user, position);
break;
case "3":
populateType3((HolderDate) viewHolder, user, position);
break;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_holder_me, parent, false);
toaa1("cbn2");
return new HolderMe(userType1);
case 2:
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_holder_you, parent, false);
return new HolderYou(userType2);
case 3:
View userType3 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_holder_date, parent, false);
return new HolderDate(userType3);
}
return super.onCreateViewHolder(parent, viewType);
}
@Override
public int getItemViewType(int position) {
ChatMessage user = getItem(position);
switch (user.getMessageUser()) {
case "test@test.com":
toaa1("cbn3");
return 1;
case "test2@test2.com":
return 2;
case "3":
return 3;
}
return super.getItemViewType(position);
}
private void populateType1(HolderMe v, ChatMessage model, int position) {
v.getTimeView().setText(DateFormat.format("HH:mm",model.getMessageTime()));
v.getTextv().setText(model.getMessageText());
}
private void toaa1( String text) {
Toast.makeText(ChatActivity.this,text,Toast.LENGTH_LONG).show();
}
private void populateType2(HolderYou vv, ChatMessage model, int position) {
vv.getTextv().setText(DateFormat.format("HH:mm",model.getMessageTime()));
vv.getTimeView().setText(model.getMessageText());
}
private void populateType3(HolderDate vcv, ChatMessage model, int position) {
vcv.getDate().setText(DateFormat.format("HH:mm",model.getMessageTime()));
}
};
listOfMessages.setAdapter(mAdapter);
}
我的xml:
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.marxhack.ma153.ActivityChatNew"
tools:showIn="@layout/app_bar_activity_chat_new">
<EditText
android:id="@+id/msginput"
android:layout_width="310dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/myRv"
android:layout_width="358dp"
android:layout_height="450dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
编辑:问题已解决
.
.
.
RecyclerView recycler = (RecyclerView)findViewById(R.id.myRv);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
.
.
.