该应用程序可能在Andriod中的主线程(Firebase数据库)上进行了太多工作



片段代码

 ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                list = new ArrayList<>();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {
                        try {
                            Model listdata = new Model();
                            Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);
                            //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();
                            String path = for_post_details.getPath();
                            String location = for_post_details.getLocation();
                            String caption = for_post_details.getCaption();
                            String username = "" + dataSnapshot1.child("username").getValue();
                            listdata.setPath(path);
                            listdata.setLocation(location);
                            listdata.setCaption(caption);
                            listdata.setUsername(username);
                            list.add(listdata);
                        } catch (Exception ignored) {
                        }
                    }
                }
                Adapter recyclerview = new Adapter(list, getActivity());
                RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutmanager);
                recycler.setItemAnimator(new DefaultItemAnimator());
                recycler.setAdapter(recyclerview);
            }
            @Override
            public void onCancelled(DatabaseError error) {
            }
        });

适配器代码

public class Adapter extends RecyclerView.Adapter<Adapter.MyHoder> {
    List<Model> list;
    Context context;
    public Adapter(List<Model> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @Override
    public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_trending,parent,false);
        MyHoder myHoder = new MyHoder(view);
        return myHoder;
    }
    @Override
    public void onBindViewHolder(MyHoder holder, int position) {
        Model mylist = list.get(position);
        holder.caption.setText(mylist.getCaption());
        holder.location.setText(mylist.getLocation());
        holder.username_top.setText(mylist.getUsername());
        holder.username_bottom.setText(mylist.getUsername());
        Glide.with(context)
                .load(mylist.getPath())
                .into(holder.image);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyHoder extends RecyclerView.ViewHolder{
        TextView total_likes, caption, location, username_top, username_bottom;
        ImageView image;
        ImageButton like, share;
        public MyHoder(View itemView) {
            super(itemView);
            total_likes = (TextView) itemView.findViewById(R.id.total_likes);
            caption = (TextView) itemView.findViewById(R.id.caption);
            username_top = (TextView) itemView.findViewById(R.id.username_top);
            username_bottom = (TextView) itemView.findViewById(R.id.username_bottom);
            location = (TextView) itemView.findViewById(R.id.location);
            image = (ImageView) itemView.findViewById(R.id.image);
            like = (ImageButton) itemView.findViewById(R.id.like);
            share = (ImageButton) itemView.findViewById(R.id.share);
        }
    }

这是我使用的代码,用于构建Instagram之类的应用。

代码运行良好,但是UI很慢,滚动速度很慢,应用程序挂起。

我收到了此消息。

I/编舞家(1378):跳过65帧!申请可能是 在其主线程上做太多工作。

我在做什么错?

您知道Instagram用来显示帖子的技术吗 顺利?

谢谢

runOnUiThread()内运行上述方法,只需在fragment

中复制 onViewCreated()
    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    list = new ArrayList<>();
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {
                            try {
                                Model listdata = new Model();
                                Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);
                                //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();
                                String path = for_post_details.getPath();
                                String location = for_post_details.getLocation();
                                String caption = for_post_details.getCaption();
                                String username = "" + dataSnapshot1.child("username").getValue();
                                listdata.setPath(path);
                                listdata.setLocation(location);
                                listdata.setCaption(caption);
                                listdata.setUsername(username);
                                list.add(listdata);
                            } catch (Exception ignored) {
                            }
                        }
                    }
                    Adapter recyclerview = new Adapter(list, getActivity());
                    RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                    recycler.setLayoutManager(layoutmanager);
                    recycler.setAdapter(recyclerview);
                }
                @Override
                public void onCancelled(DatabaseError error) {
                }
            });
        }
    });

更新另一种方法是使用AsyncTask

onViewCreated()之外复制此代码

private class LoadData extends AsyncTask<String, String, String>
{
    final ProgressDialog pDialog = new ProgressDialog(getActivity());
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        pDialog.setMessage("Loading please wait..");
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setIndeterminate(true);
        pDialog.show();
    }
    @Override
    protected String doInBackground(String... strings)
    {
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                list = new ArrayList<>();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {
                        try {
                            Model listdata = new Model();
                            Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);
                            //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();
                            String path = for_post_details.getPath();
                            String location = for_post_details.getLocation();
                            String caption = for_post_details.getCaption();
                            String username = "" + dataSnapshot1.child("username").getValue();
                            listdata.setPath(path);
                            listdata.setLocation(location);
                            listdata.setCaption(caption);
                            listdata.setUsername(username);
                            list.add(listdata);
                        } catch (Exception ignored) {
                        }
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError error) {
            Log.d("anyError", error.toString());
            }
        });
    }
    @Override
    protected void onPostExecute(String lengthOfFile)
    {
               Adapter recyclerview = new Adapter(list, getActivity());
                RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutmanager);
                recycler.setAdapter(recyclerview);
        if ((pDialog != null)  && (pDialog.isShowing()))
        {
            pDialog.dismiss();
        }
    }
}

和内部onViewCreated()只需致电new LoadData().execute();

addvalueEventListener均不断调用,无论何时更改了创建UI的数据,可以使用AddListListenerForsingLevalueevent尝试使用一次,该aDdlistEnerforsingLevalueEvent将仅读取数据,并添加计时器任务以定期更新您的应用程序的数据计时器完成时偶尔更新数据,并且不会粘贴UI

最新更新