Android 应用程序可能在Andriod的主线程(Firebase数据库)上做了太多工作

Android 应用程序可能在Andriod的主线程(Firebase数据库)上做了太多工作,android,multithreading,arraylist,firebase-realtime-database,android-recyclerview,Android,Multithreading,Arraylist,Firebase Realtime Database,Android Recyclerview,片段代码 ref.addValueEventListener(新的ValueEventListener(){ @凌驾 公共void onDataChange(DataSnapshot DataSnapshot){ 列表=新的ArrayList(); 对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){ 对于(DataSnapshot dataSnapshot2:dataSnapshot1.child(“Posts”).getChil

片段代码

ref.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
列表=新的ArrayList();
对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){
对于(DataSnapshot dataSnapshot2:dataSnapshot1.child(“Posts”).getChildren()){
试一试{
模型listdata=新模型();
_post_details的模型=dataSnapshot2.child(“info”).getValue(Model.class);
//Toast.makeText(getActivity(),“”+dataSnapshot1.child(“用户名”).getValue(),Toast.LENGTH_SHORT).show();
字符串路径=用于发布详细信息。getPath();
字符串位置=用于发布详细信息。getLocation();
字符串caption=for_post_details.getCaption();
字符串username=”“+dataSnapshot1.child(“username”).getValue();
setPath(路径);
listdata.setLocation(位置);
setCaption(标题);
setUsername(用户名);
list.add(listdata);
}捕获(忽略异常){
}
}
}
Adapter recyclerview=新适配器(列表,getActivity());
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getActivity());
回收器。setLayoutManager(layoutmanager);
setItemAnimator(新的DefaultItemAnimator());
recycler.setAdapter(recyclerview);
}
@凌驾
已取消的公共无效(DatabaseError错误){
}
});
适配器代码

公共类适配器扩展了RecyclerView.Adapter{
名单;
语境;
公共适配器(列表、上下文){
this.list=列表;
this.context=上下文;
}
@凌驾
公共MyHoder onCreateViewHolder(视图组父级,int-viewType){
视图=LayoutFlater.from(上下文)。充气(R.layout.recycler\u趋势,父项,false);
MyHoder MyHoder=新MyHoder(视图);
返回麦霍德;
}
@凌驾
BindViewHolder上的公共无效(MyHoder holder,内部位置){
Model mylist=list.get(位置);
holder.caption.setText(mylist.getCaption());
holder.location.setText(mylist.getLocation());
holder.username_top.setText(mylist.getUsername());
holder.username_bottom.setText(mylist.getUsername());
带(上下文)滑动
.load(mylist.getPath())
.插入(支架图像);
}
@凌驾
public int getItemCount(){
返回list.size();
}
类MyHoder扩展了RecyclerView.ViewHolder{
text查看总喜好、标题、位置、用户名顶部、用户名底部;
图像视图图像;
图像按钮式,共享;
公共MyHoder(查看项目视图){
超级(项目视图);
total_likes=(TextView)itemView.findViewById(R.id.total_likes);
caption=(TextView)itemView.findViewById(R.id.caption);
username\u top=(TextView)itemView.findViewById(R.id.username\u top);
username\u bottom=(TextView)itemView.findViewById(R.id.username\u 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/Choreographer(1378):跳过了65帧!应用程序可能是 在它的主线程上做了太多的工作

我做错了什么

你知道Instagram使用哪种技术来显示帖子吗 顺利


感谢addValueEventListener在创建的ui被更改时不断调用,尝试使用addListenerForSingleValueEvent,它将只读取一次数据,并添加一个计时器任务来定期更新应用程序的数据。它只会在计时器完成时偶尔更新数据,不会卡住ui上述方法在
runOnUiThread()
中,只需在片段中的
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) {
                }
            });
        }
    });

而在
onViewCreated()
内部,只需调用
newloaddata().execute();

就可以了,你可能正在加载大量的数据images@AswinPAshok图像大小仅为15kb-17kb。是否通过单击按钮获取数据?单击加载片段时创建的视图测试我的代码并检查它是否仍然挂起UI。我已尝试使用“addListenerForSingleValueEvent”但没有任何更改。结果相同。请检查您是否同时使用其他线程操作。@jubinpatel我对线程一无所知。请指导我。发布您的整个活动。@MohammedFarhan这是整个活动,rest代码只是视图初始化。尝试第二种方法。如果有效,请接受答案:)
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);

        }
    }
    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) {
                }
            });
        }
    });
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();
        }
    }
}