尽管没有任何听众+ imageViews,我的RecyclerView仍然落后,为什么?

时间:2016-10-07 01:37:11

标签: android performance android-recyclerview performance-testing

首先,我读过其他与我有同样问题的帖子;从我总结的,问题通常是因为imageviews /图片+听众。但这不是我的情况。我甚至没有为delButtonaddButton创建听众,而且我仍然落后。

我打算做什么: https://gyazo.com/ff984e87fc11543d44d1b549147f870c

点击添加按钮时,系统会创建新的 listRow

这是我的RecyclerVIew课程:

public class RecyclerAdapter extends RecyclerView.Adapter {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.saved_list_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).bindView(position);
    }

    @Override
    public int getItemCount() {
        return SavedLists.mListRow.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView mListTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            mListTextView = (TextView) itemView.findViewById(R.id.currentLIst);
        }

        public void bindView(int position){
            mListTextView.setText("List # " + position);
        }
    }

}

这是我的Acvitiy课程

    public class SavedLists extends AppCompatActivity {
       // private yourListNumber[] mHoles = new yourListNumber[0];
        public static final ArrayList<ListRow> mListRow  = new ArrayList<ListRow>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_saved_lists);
            Button mButton = (Button) findViewById(R.id.addList);



            //RECYCLERADAPTER set-up here:
            final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(new RecyclerAdapter());

            mButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    mListRow.add(new ListRow());

                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(new RecyclerAdapter());
                    //instead of repeating these 3 lines of code, you can use refresh
                }
            });
        }
    }

2 个答案:

答案 0 :(得分:0)

这是因为只要点击按钮,您就会始终重置recyclerView适配器。 改变onClick监听器变得像这样

mButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        mListRow.add(new ListRow());

        recyclerView.notifyDataSetChanged();
    }
});

答案 1 :(得分:0)

为什么要从bindViewolder调用viewholders方法?把它改成这样的

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 780
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/home/archie/servicepap/public/index.php') in server.php line 21

}

并且不要在活动中设置两次回收器适配器。并在构造函数中传递数据并将其分配给全局变量

public class RecyclerAdapter extends RecyclerView.Adapter {
ArrayList<ListRow> adapterListData;

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

   public void RecyclerAdapter(ArrayList<ListRow> mListRow){
    adapterListData = mListRow;
   }

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.saved_list_layout, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      holder.mListTextView.setText("List # " + adapterListData.get(position));

}

@Override
public int getItemCount() {
    return adapterListData.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView mListTextView;

    public ViewHolder(View itemView) {
        super(itemView);
        mListTextView = (TextView) itemView.findViewById(R.id.currentLIst);
    }
}