java - 在 View.OnClick 中更新 RecyclerView 的最佳做法是什么?

标签 java android

不确定解决此问题的最佳方法。我有一个 RecyclerView 代表数据库中的记录列表(我使用的是 SugarOrm;但是,这对问题来说无关紧要)。

我想表示数据更改并允许用户通过 onClickonLongClick 事件执行 CRUD 功能。例如,如果用户长按 RecyclerView 中的 View ,我希望他们可以选择删除记录。问题是更新很容易反射(reflect)在仅使用 ViewHolder 的 View 中;但是,删除记录并不是那么容易。 ViewHolder 作为静态内部类,无法访问 RecyclerView 本身来修改适配器数据或通知数据已更改。

一个选择是我可以使内部 ViewHolder不是静态的;但是,我应该担心潜在的内存泄漏吗?感觉那将是最简单的解决方案;但是,我应该使用完全不同的模式吗(例如让另一个类成为 onClickListener)?

我希望尽可能保持代码的可读性和标准做法;但是,如果它会违反最佳实践或变得低效,则不会。

为了清楚起见,请参见下文。

要在 ViewHolder 中显示的 SugarOrm 模型类:

public class SomeModel extends SugarRecord{

    @Column(name="Name")
    public String name;
    @Column(name="AddedDate")
    public Date addedDate = new Date();
}

RecyclerViewAdapter 和 ViewHolder:

public class SomeModelRecyclerViewAdapter
        extends RecyclerView.Adapter<SomeModelRecyclerViewAdapter.ViewHolder>{

    private List<SomeModel> data;

    public SomeModelRecyclerViewAdapter(List<SomeModel> data) {
        this.data = data;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.someModel = data.get(position);
        holder.bindData();
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder
    implements View.OnClickListener, View.OnLongClickListener {

        private static final SimpleDateFormat dateFormat =
                new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        SomeModel someModel;
        TextView modelNameLabel;
        TextView modelDateLabel;

        public SomeModel getSomeModel() {
            return someModel;
        }

        public void setSomeModel(SomeModel someModel) {
            this.someModel = someModel;
        }


        public ViewHolder(View itemView) {
            super(itemView);
        }

        public void bindData() {
            modelNameLabel = (TextView) itemView.findViewById(R.id.modelNameLabel);
            modelDateLabel = (TextView) itemView.findViewById(R.id.modelDateLabel);
            modelNameLabel.setText(someModel.name);
            modelDateLabel.setText(dateFormat.format(someModel.addedDate));
        }

        @Override
        public void onClick(View v) {
            someModel.addedDate = new Date();
            someModel.save();
            bindData();
        }

        @Override
        public boolean onLongClick(View v) {
            someModel.delete();
            return true;
        }
    }
}

Activity :

public class MainActivity extends AppCompatActivity {

    EditText modelName;
    Button addModelButton;
    RecyclerView modelList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        modelName = (EditText) findViewById(R.id.modelName);
        addModelButton = (Button) findViewById(R.id.addModelButton);
        modelList = (RecyclerView) findViewById(R.id.modelList);
        addModelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SomeModel newRecord = new SomeModel();
                newRecord.name = modelName.getText().toString();
                newRecord.save();
                setupRecyclerView();
            }
        });
        setupRecyclerView();
    }

    private void setupRecyclerView() {
        List<SomeModel> allModels = SomeModel.listAll(SomeModel.class);
        SomeModelRecyclerViewAdapter adapter = new SomeModelRecyclerViewAdapter(allModels);
        modelList.setHasFixedSize(true);
        modelList.setLayoutManager(new LinearLayoutManager(this));
        modelList.setAdapter(adapter);
    }
}

Activity 布局(activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.aaronmbond.recyclerviewdilemaexample.MainActivity">

    <EditText
        android:id="@+id/modelName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        />

    <Button
        android:id="@+id/addModelButton"
        android:layout_alignParentStart="true"
        android:layout_below="@id/modelName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/addModelButtonText"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/modelList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/addModelButton"
        android:layout_alignParentStart="true"
        />

</RelativeLayout>

RecyclerView 项目布局(model_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/modelNameLabel"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/modelDateLabel"
        android:layout_alignParentStart="true"
        android:layout_below="@id/modelNameLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

最佳答案

几天前我遇到了类似的要求。您说得对 - ViewHolder 作为静态内部类,无权访问 RecyclerView 本身来修改适配器数据或通知数据已更改。

所以处理这个问题的方法是定义一个接口(interface),将所有操作封装在您的 viewholder 中,它需要在 RecyclerView 上触发某些操作。我的代码中的示例接口(interface)定义 -

/**
 * Parent fragment or activity to implement this interface to listen to item deletes.
 * Item deletes effect the state of the parent
 */
public interface OnItemModifiedListener {
    void itemDeleted(Cart.CartItem item);
    void itemQuantityChanged(Cart.CartItem item, int newQuantity);
    void itemRemovedAll();
}

父 fragment 或 Activity 实现此接口(interface)并将其作为其构造函数的一部分传递给适配器。再次使用我的代码中的示例构造函数 -

public SimpleItemRecyclerViewAdapter(Context context, List<Cart.CartItem> items, OnItemModifiedListener l) {

    //this variable is declared as a adapter state variable
    mItemModifiedListener = l;

}

现在,当您的 viewholder 中发生特定操作(特别是点击)时,然后在此接口(interface)上调用适当的方法。从我的代码中再次采样,当一行被删除时我调用这个接口(interface) -

holder.mDeleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //show an alert to user to confirm before remving the item from cart
                    AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                    //alertDialog.setTitle("Alert");
                    alertDialog.setMessage(getString(R.string.alert_remove_item_from_cart_text));
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(android.R.string.ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    mValues.remove(holder.mItem);
                                    if(null != mItemModifiedListener)mItemModifiedListener.itemDeleted(holder.mItem);
                                    notifyItemRemoved(position);
                                    //notifyDataSetChanged();
                                }
                            });
                    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(android.R.string.no),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.show();
                }
            });

下面的链接也很不错 - https://antonioleiva.com/recyclerview-listener/

希望这有助于...

关于java - 在 View.OnClick 中更新 RecyclerView 的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42059720/

相关文章:

java - AOP实现

java - Spring数据MongoDB : Query by class instance

Android矢量drawables vrs png

java - 对象初始化数组

java - 从 Android 中的 Assets 传递路径

android - 从服务器视频链接android生成缩略图

android - javax.net.ssl.SSLProtocolException : SSL handshake aborted: ssl=0x7fa6b56a00: Failure in SSL library, 通常是协议(protocol)错误

android - Tab on focus 或 Tab on click 要增加的图像尺寸

java - 如何从java程序在终端运行命令?

android - 使用 Material Design 主题进行 fragment 隔离测试