在recyclerview中的notifydatachanged期间保持涟漪效应

时间:2015-09-12 19:50:27

标签: android android-recyclerview ripple

我开始修改我的应用程序以支持棒棒糖。基本上,我有一个带有recyclerview的文件浏览器片段,当用户打开这个片段时,他会看到他的根目录下的所有文件夹,当用户点击我需要获取所有子文件夹+文件的文件夹并将其显示给用户使用与notifydatachanged相同的recyclerview。功能正常的问题是当用户点击文件夹时,在notifydatachanged期间不会保留涟漪效应。

file_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/file_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:drawablePadding="15dp"
    android:ellipsize="marquee"
    android:focusable="true"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="14dip"
    android:paddingRight="15dip"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="?android:attr/textColorAlertDialogListItem" />

Recyclerview适配器:

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.CustomViewHolder>{

    private List<Item> _data;
    private Context _context;
    private IFileListener _listener;

    public RecycleAdapter(Context context, List<Item> data,IFileListener listener) {
        _data = data;
        _context = context;
        _listener = listener;
    }

    public void setData(List<Item> data)
    {
        _data = data;
        notifyDataSetChanged();
    }


    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(_context).inflate(R.layout.file_item, viewGroup,false);

        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
        final Item file = _data.get(i);

        //Setting text view title
        customViewHolder.textView.setText(file._fileName);
        customViewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _listener.onFileClickListener(file._fileName);
            }
        });
//            customViewHolder.textView.setOnTouchListener(new View.OnTouchListener() {
//                @Override
//                public boolean onTouch(View v, MotionEvent event) {
//
//                    switch (event.getActionMasked())
//                    {
//                        case MotionEvent.ACTION_UP:
//                            _listener.onFileClickListener(file._fileName);
//                            break;
//                    }
//                    return false;
//                }
//            });
//            customViewHolder.textView.setOnTouchListener(new View.OnTouchListener() {
//                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
//                @Override
//                public boolean onTouch(View v, MotionEvent event) {
//                    v.getBackground().setHotspot(event.getX(), event.getY());
//
//                    return(false);
//                }
//            });


        if (file._isFolder) {
            customViewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.directory_icon, 0, 0, 0);
        } else {
            customViewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.file_icon, 0, 0, 0);
        }

    }

    @Override
    public int getItemCount() {
        return (null != _data ? _data.size() : 0);
    }



    public class CustomViewHolder extends RecyclerView.ViewHolder
    {
        protected TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            this.textView = (TextView) view.findViewById(R.id.file_name);
        }
    }

    public interface IFileListener
    {
        public void onFileClickListener(String file);
    }


}

1 个答案:

答案 0 :(得分:4)

在这里查看答案:

https://stackoverflow.com/a/34523222/1847734

在onClick方法中(假设在回调中),请确保在notifyDataSetChanged()之后不要致电notifyItemChanged(position)

notifyDataSetChanged()会与默认的连锁反应发生冲突。

new recyclerAdapter.ClickListener() {
    @Override
    public void onClick(int position) {

        ... awesome item onClick code ...

        notifyItemChanged(position);
        //notifyDataSetChanged(); <//--- Causes the no ripple bug
    }
};