[英]RecyclerView showing item only when editing the next item to be added
I'm currently building an activity in my app where products are added using a FAB. 我目前正在我的应用程序中构建一个活动,使用FAB添加产品。 Clicking the FAB opens a new pop-up activity with startActivityForResult, and in it a form is filled and sent back via a Parcelable object.
单击FAB将打开一个带有startActivityForResult的新弹出式活动,并在其中填充一个表单并通过一个Parcelable对象将其发送回去。
The problem I'm facing right now is that after adding a product it is not displayed in the RecyclerView, but when I click the FAB again, the second I start editing any field inside the form (meaning any EditText, for example) for the next product, I see the previous one appearing in the background behind my pop-up form. 我现在面临的问题是,添加产品后,它没有显示在RecyclerView中,但是当我再次单击FAB时,第二个我开始为表单编辑表单内的任何字段(例如,表示任何EditText)。在下一个产品中,我看到上一个产品出现在弹出式窗体后面的背景中。 When I finish the second product, it won't appear until I start editing the third one, etc.
当我完成第二个产品时,直到我开始编辑第三个产品时,它才会出现,依此类推。
I went through the XML and it seems OK to me... And tried reading several solutions (and my code again and again) but none of them were really helpful. 我遍历了XML,对我来说似乎还行。。。尝试读取几个解决方案(以及一次又一次地阅读我的代码),但没有一个真正有用。
Relevant pieces of code (I put ... where I neglected some code that's irrelevant for this case): From the main activity (in it the products are meant to be shown): 相关代码段(我把...放在这里忽略了与本例无关的代码):从主要活动(其中应显示产品):
''' '''
public class EditOffersActivity extends AppCompatActivity {
...
private ArrayList<Offer> offers_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_products);
offers_list = new ArrayList<>();
...
RecyclerView rvOffers = findViewById(R.id.offers_RecyclerView);
OfferAdapter adapter = new OfferAdapter(offers_list);
rvOffers.setAdapter(adapter);
rvOffers.setLayoutManager( new LinearLayoutManager(this));
// Setting up the FAB so it leads to the Product Details Pop-up
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(EditOffersActivity.this, ProductDetailsPopupActivity.class);
startActivityForResult(intent, HTZ_ADD_OFFER); // HTZ_ADD_OFFER = 1
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Offer created_offer = data.getParcelableExtra("offer");
offers_list.add(created_offer);
}
}
''' '''
The Adapter: ''' 适配器:“''
public class OfferAdapter extends android.support.v7.widget.RecyclerView.Adapter<OfferAdapter.OfferViewholder> {
private ArrayList<Offer> mOffers;
public OfferAdapter(ArrayList<Offer> offers) {
mOffers = offers;
}
@Override
public OfferViewholder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.item_product;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layoutIdForListItem, viewGroup, false);
OfferViewholder viewHolder = new OfferViewholder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(OfferViewholder holder, int position) {
Offer current_offer = mOffers.get(position);
... // a lot of binding code
}
@Override
public int getItemCount() {
return mOffers.size();
}
class OfferViewholder extends RecyclerView.ViewHolder {
TextView name_TextView;
TextView time_TextView;
...
public OfferViewholder(View itemView) {
super(itemView);
// Init all views
name_TextView = itemView.findViewById(R.id.product_item_name_TextView);
time_TextView = itemView.findViewById(R.id.product_item_time_TextView);
...
}
}
}
''' '''
And the bit from the popup activity that closes it after the user click 'finished': ''' 在用户单击“完成”后,弹出活动将其关闭的位:'''
mPublishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(p_photo_url == null) {
...
} else {
// Create offer from user input
Offer n_offer = new Offer (...)
);
...
// Return offer to EditOffersActivity
Intent resultIntent = new Intent();
resultIntent.putExtra("offer", n_offer); // Offer is Parcelable
setResult(HTZ_ADD_OFFER, resultIntent);
finish();
}
}
});
''' '''
And the RecyclerView's XML: 而RecyclerView的XML:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/offers_RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_product"
tools:orientation="vertical"
tools:scrollbars="horizontal"
tools:spanCount="1">
you can use notifydatasetchanged as below code : 您可以使用notifydatasetchanged如下代码:
mPublishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(p_photo_url == null) {
...
} else {
// Create offer from user input
Offer n_offer = new Offer (...)
);
...
// Return offer to EditOffersActivity
Intent resultIntent = new Intent();
resultIntent.putExtra("offer", n_offer); // Offer is Parcelable
setResult(HTZ_ADD_OFFER, resultIntent);
OfferAdapter.notifydatasetchanged();
finish();
}
}
});
OfferAdapter.notifydatasetchanged();// method will refresh the adapter which is set on your view. OfferAdapter.notifydatasetchanged(); //方法将刷新您在视图中设置的适配器。 and the values will be refresh.
值将刷新。
Don't forget to appreciate and like the answer. 不要忘记欣赏和喜欢答案。
Add a method on your adapter like this: 在适配器上添加如下方法:
public void update(ArrayList<Offer> offers){
mOffers = offers;
}
Update your onActivityResult and notify adapter like this: 更新您的onActivityResult并像这样通知适配器:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Offer created_offer = data.getParcelableExtra("offer");
offers_list.add(created_offer);
adpater.update(offers_list);
adapter.notifydatasetchanged();
}
after adding to ArrayList You Have to notify changes to Adaptor by calling RecyclerViewAdapter's notifydatasetchanged() 添加到ArrayList之后,您必须通过调用RecyclerViewAdapter的notifydatasetchanged()来通知对适配器的更改
like: 喜欢:
adapter.notifydatasetchanged() adapter.notifydatasetchanged()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.