使用calllogs在listview上平滑滚动

时间:2018-03-30 07:53:29

标签: android listview

我在listview中添加了自定义适配器。数据是来自手机的通话记录。我通过仅显示3天的记录来减少列表。问题是,当我尝试从上到下滚动listview时,我有一个很大的滞后。我的滚动不顺畅。有没有办法让listview滚动更流畅?

这是我的自定义适配器:

    public class CallListAdapter extends ArrayAdapter<CallList> {

    Activity activity;

    public CallListAdapter(Context context, ArrayList<CallList> calls, Activity activity) {
        super(context, 0, calls);
        this.activity = activity;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final CallList callList = getItem(position);

        int actualPosition = 0;

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.call_list, parent, false);
        }

        final TextView call1 = convertView.findViewById(R.id.callNumber);
        final TextView call2 = convertView.findViewById(R.id.callDate);
        final TextView call3 = convertView.findViewById(R.id.conversationTime);
        final TextView call4 = convertView.findViewById(R.id.callType);
        final Button callView = convertView.findViewById(R.id.getViewName);
        final ImageView bio = convertView.findViewById(R.id.lookBio);
        final ImageView edit = convertView.findViewById(R.id.edit_call);
        final ImageView block = convertView.findViewById(R.id.blockCall);
        final ImageView call = convertView.findViewById(R.id.callUser);
        final TextView bioLabel = convertView.findViewById(R.id.BioLabelSug);
        final TextView editLabel = convertView.findViewById(R.id.NoteLabel);
        final TextView blockLabel = convertView.findViewById(R.id.BlockLabelSug);
        final TextView callLabel = convertView.findViewById(R.id.CallLabelSug);
        final ConstraintLayout callContainer = convertView.findViewById(R.id.contact_container);
        final ConstraintLayout bioContainer = convertView.findViewById(R.id.bio_container);
        final ConstraintLayout blockContainer = convertView.findViewById(R.id.ignore_container);
        final ConstraintLayout noteContainer = convertView.findViewById(R.id.note_container);
        final TextView btnMarg = convertView.findViewById(R.id.buttonMargin);
        final TextView callListNr2 = convertView.findViewById(R.id.callNumber2);

        final LayoutInflater factory = activity.getLayoutInflater();

        final View fullView = factory.inflate(R.layout.fragment_calls, null);

        final RelativeLayout loading = fullView.findViewById(R.id.loadingBar);


        String[] jsonData = new manageCalls().intentCallValues(position);

        StringBuilder builder = new StringBuilder();
        for (String s : jsonData) {
            builder.append(s + "\n");
        }
        String str = builder.toString();

        final String num = jsonData[0];
        final String dat = jsonData[1];
        final String typeCall = jsonData[2];
        final String dur = jsonData[3];
        final String authToken = SaveSharedPreferences.getPrefTokenName(getContext());

        final Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slideup);
        final Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.slidedown);

        final Handler handler = new Handler();

        callView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (bioContainer.getVisibility() == View.GONE) {

                    callListNr2.setVisibility(View.GONE);
                    bio.setVisibility(View.VISIBLE);
                    bioLabel.setVisibility(View.VISIBLE);
                    edit.setVisibility(View.VISIBLE);
                    editLabel.setVisibility(View.VISIBLE);

                } else if (bioContainer.getVisibility() == View.VISIBLE) {

                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            bio.setVisibility(View.GONE);
                            callContainer.setVisibility(View.GONE);
                            bioContainer.setVisibility(View.GONE);
                            noteContainer.setVisibility(View.GONE);
                            blockContainer.setVisibility(View.GONE);

                        }
                    }, 300);

                }
            }
        });


        if (actualPosition != position) {

            if (bioContainer.getVisibility() == View.VISIBLE) {

                bioContainer.setVisibility(View.GONE);
                callContainer.setVisibility(View.GONE);
                noteContainer.setVisibility(View.GONE);
                blockContainer.setVisibility(View.GONE);

            }

            actualPosition = position;

        }


        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                call.setEnabled(false);
                loading.setVisibility(View.VISIBLE);
                Intent intentCall = new Intent(view.getContext(), CallUserActivity.class);

                intentCall.putExtra("number", num);
                intentCall.putExtra("authToken", authToken);
                intentCall.putExtra("Date", dat);

                activity.startActivityForResult(intentCall, position);

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        call.setEnabled(true);
                        loading.setVisibility(View.GONE);
                    }
                }, 1000);
            }
        });

        call2.setText(callList.callDate);
        call3.setText(callList.conversationTime);
        call4.setText(callList.callType);

        return convertView;
        }
}

2 个答案:

答案 0 :(得分:1)

你的获取是巨大的。 您的(如果convertview == null)基本上几乎没有效果,因为您无论如何都要再次设置视图。

你需要做的是重构getview,不要太慢。你可以做的一件事就是创建一个类,它已经为你完成了所有的查找,然后将它放在转换后的视图的.tag中。更改你的onclicks以使用它,以一种你不必重新创建它们的方式(其他方法也存在)。

理想情况下,如果您已经转换了视图,那么您的代码应该只是.settexts()。

取决于列表的大小,您可以为每个调用者创建一个视图,并避免完全回收转换后的视图,在这种情况下,您可以提前创建它们。

同样取决于列表的大小,您可以在滚动视图中创建一个简单的线性布局。如果你的名单不是很大而且不适合某些旧款手机,它的效果也不错(在你尝试使用手机之前,不要将其作为糟糕的建议,在列表视图开始变得更有意义之前可能会有多大) 。

答案 1 :(得分:1)

尝试使用ViewHolder并使用AsyncTask加载位图。 你可以这样试试。

&#13;
&#13;
    private static class ViewHolder {
            public TextView call1;
            public TextView call2;
            public TextView call3;
            public TextView call4;
            public Button callView;
            public ImageView bio;
            public ImageView edit;
            public ImageView block;
            public ImageView call;
            public TextView bioLabel;
            public TextView editLabel;
            public TextView blockLabel;
            public TextView callLabel;
            public ConstraintLayout callContainer;
            public ConstraintLayout bioContainer;
            public ConstraintLayout blockContainer;
            public ConstraintLayout noteContainer;
            public TextView btnMarg;
            public TextView callListNr2;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
                // inflate the layout
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(layoutResourceId, parent, false);
                holder = new ViewHolder();
                holder.call1 = convertView.findViewById(R.id....);
                holder.call2 = convertView.findViewById(R.id....);
                //Same for all other views
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

        holder.call1.setText(....);
        //Lazy load for bitmap
        loadBitmap(yourFileName..., bio)
       
        return convertView;
    }

    static class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;

        BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(String... params) {
            return decodeSampledBitmapFromResource(params[0], 300, 300);
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }


    public void loadBitmap(String fileName, ImageView imageView) {
        BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        task.execute(fileName);
    }

  public static Bitmap decodeSampledBitmapFromResource(String fileName,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(fileName, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(fileName, options);
}
&#13;
&#13;
&#13;

相关问题