如何在Recyclerview项目中保存按钮状态

时间:2020-04-30 16:49:25

标签: android android-recyclerview adapter

您好,我有一个水平的回收站视图,其中包含一些产品,在加号按钮中,我将产品添加到购物车中,添加产品后,图标会更改。我这样做了,但是我的问题是保存此按钮的状态,因为当我在脆弱/活动之间导航以及应用程序启动时,按钮保持不变。这是我拥有的:

enter image description here

我的适配器中的代码是:

{% extends "invoicedata/base.html" %}
{% block content %}

<main role="main" class="container" style="margin-right: 11%;">
<div class="col-md-12">
<h2>Extract From Pdf</h2>
<hr>

<div class="container">
<div class="row">     
<div class="col-12">
<div class="card">

    <div class="card-header">
    Featured
    </div>
    <img class="card-img" src="https://vectorified.com/images/parser-icon-30.png" alt="bgimage">
    <div class="card-img-overlay ">


    <div class="card" style="width:40%; margin-top:50px;" >
    <div class="card-body" >
        <form method="POST" class="post-form" enctype="multipart/form-data">
            {% csrf_token %}  
            {{ form.as_p }} 
            <input class="btn btn-primary" onclick='start(0)' type="Submit" name="submit" value="Extract & Save">

        </form>
        <br>
        <h5>EXtracted Data will apear here</h5>
        <hr>

        {% for key, value in result.items %}
        <h6>{{key}} : {{value}}</h6>
        {% endfor %}
    </div>
    </div>
    </div>
</div>
</div>
</div>
</div>

</div>
</main>
{% endblock content %}

我进行了很多搜索,解决方案可能是使用共享首选项来保存状态,并带有sharedprerences.contains(preferences_id),但我对此感到困惑。有没有人有任何建议我如何实现这一目标或可能的解决方案。 预先感谢。

2 个答案:

答案 0 :(得分:0)

在大多数情况下,将按钮的状态保存在模型列表中就足够了。但是,当用户更改其片段或活动时,由于内存使用情况,系统可能会破坏您的模型列表。对于这种情况,您可以选择是使用sharedpref,file,db等持久存储数据,还是将数据保存在寿命比活动或片段更长的商店中。您共享的页面似乎与应用程序的主要功能有关,因此您应该将数据保留在其范围取决于应用程序范围的存储库中。我建议您看一下mvvm架构和存储库模式。

答案 1 :(得分:-1)

我假设您要在点击button之后将其保留不变:

如果这是您的问题,是的,我们将使用shared prefrences

@Override
public void onBindViewHolder(@NonNull ArtikujtViewHolder holder, int position) {

//the item
final Artikujt mArtikull = artikujt.get(position);

..........
..........

//read the prefrences
SharedPreferences pref = getSharedPreferences("Button", MODE_PRIVATE);
String state = pref.getString(String.valueOf(position)+"pressed", "no");

if(state.equals("yes")){

//the button must be pressed, make it pressed (check mark icon)

//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_check_black, 0);

holder.addItems.setEnabled(false);

}else{
//the button must not be pressed, make it not pressed (default add icon)

//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_add_icon, 0);

holder.addItems.setEnabled(true);
}


//after clicking the add icon

holder.addItems.setOnClickListener(v -> {

//save to preferences
SharedPreferences.Editor editor = getSharedPreferences("Button", MODE_PRIVATE).edit();
editor.putString(String.valueOf(position)+ "pressed", "yes");
editor.apply();


//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_check_black, 0);
            //======================================================================================
// Display a toast message
final Toast toast = Toast.makeText(mContext, R.string.shtuar_ne_shporte, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Handler handler = new Handler();
handler.postDelayed(toast::cancel, 1000);
            //======================================================================================

assert artikullObject != null;
count_key = artikullObject.size();

String cartCount = String.valueOf(count_key);
Intent my_intent = new Intent("msg");
my_intent.putExtra("cart_count", cartCount);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(my_intent);

v.setEnabled(false);

});


}