如何从Recycler视图适配器访问父布局。我的目标是在Recycler视图中获取qty的总和并更新父级中的小计。
这是我的代码。
这是主要活动
public class project_bid extends AppCompatActivity {
DatabaseReference mRef;
RecyclerView ItemRecyclerView;
EditText subtotal, tax, grandTotal;
String key;
LinearLayout bid_total_layout;
projectItemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
key = getIntent().getStringExtra("key");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_bid);
mRef = FirebaseDatabase.getInstance().getReference().child("projects/1/project_line");
subtotal = (EditText) findViewById(R.id.project_bid_subtotal);
tax = (EditText) findViewById(R.id.project_bid_tax);
grandTotal = (EditText) findViewById(R.id.project_bid_grandTotal);
bid_total_layout = (LinearLayout) findViewById(R.id.project_bid_total_layout);
ItemRecyclerView = (RecyclerView) findViewById(R.id.projectRecycler);
ItemRecyclerView.setHasFixedSize(false);
ItemRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<ProjectDetailsLineBuilder> list = new ArrayList<ProjectDetailsLineBuilder>();
for (DataSnapshot snapshot: dataSnapshot.getChildren()){
ProjectDetailsLineBuilder item = snapshot.getValue(ProjectDetailsLineBuilder.class);
list.add(item);
}
adapter = new projectItemAdapter(list, project_bid.this);
ItemRecyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onStart() {
super.onStart();
}
另外,我使用上面的方法来获取并将值传递给适配器。 for循环会产生性能问题,还有更好的解决方案吗?
这是我的适配器和视图持有者类。
public class projectItemAdapter extends RecyclerView.Adapter<projectItemAdapter.projectBidItems>{
ArrayList<ProjectDetailsLineBuilder> data;
Context context;
LinearLayout totalLayout;
public projectItemAdapter(ArrayList<ProjectDetailsLineBuilder> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public projectBidItems onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.project_bid_line, parent, false);
View main = LayoutInflater.from(context).inflate(R.layout.activity_project_bid, parent, false);
return new projectBidItems(v);
}
@Override
public void onBindViewHolder(projectBidItems holder, int position) {
ProjectDetailsLineBuilder item = data.get(position);
holder.qty.setText(String.valueOf(item.getQuantity()));
holder.quantity.setText("Quantity: 20 " + item.getUnit());
holder.title.setText(item.getTitle());
holder.desc.setText(item.getDescription());
holder.unitprice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public static class projectBidItems extends RecyclerView.ViewHolder {
View mView;
EditText qty;
EditText total, unitprice;
TextView quantity, desc, title;
public projectBidItems(View item) {
super(item);
mView = item;
unitprice = (EditText) mView.findViewById(R.id.project_line_unit_price);
total = (EditText) mView.findViewById(R.id.project_line_total);
quantity = (TextView) mView.findViewById(R.id.project_line_qty);
qty = (EditText) mView.findViewById(R.id.project_line_cal_qty);
title = (TextView) mView.findViewById(R.id.project_line_title);
desc = (TextView) mView.findViewById(R.id.project_lined_description);
}
}
}