RecyclerView不会在第一次启动时加载项目 [英] RecyclerView doesn't load items on first start of activity
问题描述
我产卵的项目:
<?xml version =1.0encoding =utf-8?>
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:app =http://schemas.android.com/apk/res -auto
xmlns:card_view =http://schemas.android.com/tools
android:id =@ + id / show_chat_single_item_layout
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =horizontal>
android.support.v7.widget.CardView
android:layout_width =match_parent
android:layout_height =70dp
app :cardBackgroundColor =#dcdada
app:cardCornerRadius =0dp
app:contentPadding =3dp
card_view:cardCornerRadius =5dp
card_view:cardElevation =2dp
card_view:cardUseCompatPadding =true>
< android.support.constraint.ConstraintLayout
android:layout_width =match_parent
android:layout_height =wrap_content>
$ {
$ android
$ android $ android $ layout bash $ $ b android:layout_marginBottom =8dp
android:layout_marginLeft =8dp
android:layout_marginTop =8dp
app:layout_constraintBottom_toBottomOf =parent
app:layout_constraintLeft_toLeftOf = parent
app:layout_constraintTop_toTopOf =parent
app:srcCompat =@ mipmap / ic_launcher/>
TextView
android:id =@ + id / chat_persion_name
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =10dp
android:layout_marginTop =0dp
android:text =Name
android:textSize =18sp$ b $ android:textStyle = bold
app:layout_constraintLeft_toRightOf =@ + id / chat_persion_image
app:layout_constraintTop_toTopOf =@ + id / chat_persion_image/>
TextView
android:id =@ + id / chat_persion_email
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =0dp
android:layout_marginTop =8dp
android:text =Email
app:layout_constraintLeft_toLeftOf =@ + id / chat_persion_name
app:layout_constraintTop_toBottomOf =@ + id / chat_persion_name/>
< /android.support.v7.widget.CardView>
< / LinearLayout>
我的对话版面:
<?xml version =1.0encoding =utf-8?>
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:app =http://schemas.android.com/apk/res -auto
android:id =@ + id / msgs_content
android:layout_width =match_parent
android:layout_height =match_parent
android:orientation =vertical
app:theme =@ style / AppTheme2
>
< android.support.v7.widget.RecyclerView
android:id =@ + id / active_chats
android:layout_width = match_parent
android:layout_height =510dp
android:layout_alignParentBottom =true
android:layout_alignParentTop =true
android:layout_centerHorizontal =true
android:layout_marginLeft =2dp
android:layout_marginRight =2dp
android:layout_marginTop =57dp>
< /android.support.v7.widget.RecyclerView>
$ b $ include
android:id =@ + id / include2
layout =@ layout / toolbar
android:layout_width = match_parent
android:layout_height =wrap_content
/>
< / RelativeLayout>
我的适配器:
public class ActiveChatConvo extends RecyclerView.Adapter< ActiveChatConvo.ViewHolder>
{
private ArrayList< User> mUsers;
private Context mContext;
public ActiveChatConvo(ArrayList< User> photos,Context dick){
mUsers = photos;
mContext = dick;
}
private Context getContext(){
return mContext;
$ b @Override
public ActiveChatConvo.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
//膨胀自定义布局
查看contactView = inflater.inflate(R.layout.chat_single_item,parent,false);
//返回一个新的持有者实例
ViewHolder viewHolder = new ViewHolder(contactView);
返回viewHolder;
$ b @Override
public void onBindViewHolder(ViewHolder holder,int position){
//获取基于位置的数据模型
User user = mUsers.get(位置);
//根据您的视图和数据模型设置项目视图
TextView name = holder.mItemName;
name.setText(user.getName());
TextView description = holder.mItemDescription;
description.setText(user.getEmail());
ImageView pic = holder.mItemImage; (pic.getContext())。load(Uri.parse(user.getPic()))。into(pic);
Picasso.with(pic.getContext()
$ b @Override
public int getItemCount(){
return mUsers.size();
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
$ b $ private ImageView mItemImage;
私人TextView mItemName;
私人TextView mItemDescription;
私有的LinearLayout布局;
final LinearLayout.LayoutParams params;
public ViewHolder(View v){
super(v);
mItemImage =(ImageView)v.findViewById(R.id.chat_persion_image);
mItemName =(TextView)v.findViewById(R.id.chat_persion_name);
mItemDescription =(TextView)v.findViewById(R.id.chat_persion_email);
layout =(LinearLayout)itemView.findViewById(R.id.show_chat_single_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
v.setOnClickListener(this);
@Override
public void onClick(View v){
Context context = itemView.getContext();
Intent showChatIntent = new Intent(context,ChatConversationActivity.class);
//showPhotoIntent.putExtra(PHOTO_KEY,mPhoto);
context.startActivity(showChatIntent);
我的主类 public class ConnectionsActivity extends AppCompatActivity {
private Toolbar toolbar;
私人ViewPager mViewPager;
私人TextView person_name,person_email;
私人RecyclerView recyclerView;
private DatabaseReference mRef;
私有LinearLayoutManager mLinearLayoutManager;
private ArrayList< User>用户;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connections);
//数据库
mRef = FirebaseDatabase.getInstance()。getReference(Users);
mRef.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
users = new ArrayList<>();
$ b mRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists() )
{
for(DataSnapshot postSnapshot:dataSnapshot.getChildren()){
String name = postSnapshot.child(Name)。getValue(String.class); $ $ b $ String String = postSnapshot.child(Email)。 b $ b users.add(new User(name,email,pic));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
// Recycler View
recyclerView =(RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo适配器=新的ActiveChatConvo(用户,这);
recyclerView.setAdapter(adapter);
mLinearLayoutManager =新的LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
// VIEWS
toolbar =(工具栏)findViewById(R.id.tToolbar);
if(toolbar!= null)
setSupportActionBar(toolbar);
ImageView profileActivity =(ImageView)toolbar.findViewById(R.id.action_profile);
ImageView homeActivity =(ImageView)toolbar.findViewById(R.id.action_home);
profileActivity.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent profileActivity = new Intent (ConnectionsActivity.this,ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
}
});
homeActivity.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent home = new Intent (ConnectionsActivity.this,HomeActivity.class);
startActivity(home);
finish();
overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
}
});
}
$ b}
因此,如果recyclerView Activity是我访问的第一个活动,那么它将不会创建这些项目。 Firebase是同步的,所以它不会阻塞你的应用程序的主线程,这意味着你的应用程序继续执行,你需要通过在之后使用 adapter.notifiyDatasetChanged()
来完成他的工作之后通知适配器循环
Everytime we open the activity that bares the recycler adapter it fails to load on the first try. Exiting the activity and re-entering fixes the problem. Here is a gif for example. https://gyazo.com/32dc664dd427ef1129704a09861a3708
The Item i am spawning in:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/show_chat_single_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="70dp"
app:cardBackgroundColor="#dcdada"
app:cardCornerRadius="0dp"
app:contentPadding="3dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/chat_persion_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/chat_persion_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:text="Name"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@+id/chat_persion_image"
app:layout_constraintTop_toTopOf="@+id/chat_persion_image" />
<TextView
android:id="@+id/chat_persion_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="8dp"
android:text="Email"
app:layout_constraintLeft_toLeftOf="@+id/chat_persion_name"
app:layout_constraintTop_toBottomOf="@+id/chat_persion_name" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
My conversation layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/msgs_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:theme="@style/AppTheme2"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/active_chats"
android:layout_width="match_parent"
android:layout_height="510dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="57dp">
</android.support.v7.widget.RecyclerView>
<include
android:id="@+id/include2"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
My Adapter:
public class ActiveChatConvo extends RecyclerView.Adapter<ActiveChatConvo.ViewHolder>
{
private ArrayList<User> mUsers;
private Context mContext;
public ActiveChatConvo(ArrayList<User> photos,Context dick) {
mUsers = photos;
mContext = dick;
}
private Context getContext() {
return mContext;
}
@Override
public ActiveChatConvo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.chat_single_item, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Get the data model based on position
User user = mUsers.get(position);
// Set item views based on your views and data model
TextView name = holder.mItemName;
name.setText(user.getName());
TextView description = holder.mItemDescription;
description.setText(user.getEmail());
ImageView pic = holder.mItemImage;
Picasso.with(pic.getContext()).load(Uri.parse(user.getPic())).into(pic);
}
@Override
public int getItemCount() {
return mUsers.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mItemImage;
private TextView mItemName;
private TextView mItemDescription;
private LinearLayout layout;
final LinearLayout.LayoutParams params;
public ViewHolder(View v) {
super(v);
mItemImage = (ImageView) v.findViewById(R.id.chat_persion_image);
mItemName = (TextView) v.findViewById(R.id.chat_persion_name);
mItemDescription = (TextView) v.findViewById(R.id.chat_persion_email);
layout = (LinearLayout) itemView.findViewById(R.id.show_chat_single_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Context context = itemView.getContext();
Intent showChatIntent = new Intent(context, ChatConversationActivity.class);
//showPhotoIntent.putExtra(PHOTO_KEY, mPhoto);
context.startActivity(showChatIntent);
}
}
}
My main class
public class ConnectionsActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mViewPager;
private TextView person_name,person_email;
private RecyclerView recyclerView;
private DatabaseReference mRef;
private LinearLayoutManager mLinearLayoutManager;
private ArrayList<User> users;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connections);
//Database
mRef = FirebaseDatabase.getInstance().getReference("Users");
mRef.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
users = new ArrayList<>();
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String name = postSnapshot.child("Name").getValue(String.class);
String email = postSnapshot.child("Email").getValue(String.class);
String pic = postSnapshot.child("image").getValue(String.class);
users.add(new User(name,email,pic));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Recycler View
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
recyclerView.setAdapter(adapter);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
//VIEWS
toolbar = (Toolbar) findViewById(R.id.tToolbar);
if (toolbar != null)
setSupportActionBar(toolbar);
ImageView profileActivity = (ImageView) toolbar.findViewById(R.id.action_profile);
ImageView homeActivity = (ImageView) toolbar.findViewById(R.id.action_home);
profileActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profileActivity = new Intent(ConnectionsActivity.this, ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
homeActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent home = new Intent(ConnectionsActivity.this, HomeActivity.class);
startActivity(home);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
}
}
So if the recyclerView Activity is the first activity I visit then it will not create the items.
解决方案 Firebase is synchronize so it doesn't block the main thread of your application, that mean that your application continue executing, you need to notify the adapter after firebase finishing his job by using adapter.notifiyDatasetChanged()
after the for
loop
这篇关于RecyclerView不会在第一次启动时加载项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!