在打开或关闭片段对话框时,recyclerview的onbindviewholder出现问题。即使我不为适配器调用notifychanges,它始终会触发onbindviewholder
。
有人经历过吗?使用回调或Fragment.MyFunction()
从适配器调用到片段,仍然触发onbindviewholder
。
我的适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
....
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_product_extruck2,
parent, false);
final ViewHolder holder1 = new ViewHolder(v);
return holder1;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.priceTxt.setText("0.00");
holder.lin_product_info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyFragment.showdialog(list.get(position),position);
}
});
}
@Override
public int getItemCount() { return list.size(); }
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(false);
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.lin_product_info) LinearLayout lin_product_info;
@BindView(R.id.price) TextView priceTxt;
@BindView(R.id.itemCode) TextView itemCodeTxt;
@BindView(R.id.productName) TextView productNameTxt;
@BindView(R.id.img) ImageView img;
@BindView(R.id.inventory_item_layout) LinearLayout inventory_item_layout;
public ViewHolder(final View convertView) {
super(convertView);
ButterKnife.bind(this,convertView);
convertView.setTag(this);
}
}
// adding new items add in que
void updateItemsInternal(final List<Inventory> newItems) {
final List<Inventory> oldItems = new ArrayList<>(this.list);
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
Log.d(">>", "run thread updatesinternal "+newItems.size());
final DiffUtil.DiffResult diffResult =
DiffUtil.calculateDiff(new DiffInventory(oldItems, newItems));
handler.post(new Runnable() {
@Override
public void run() {
Log.d(">>", "run thread updatesinternal handler");
applyDiffResult(newItems, diffResult);
}
});
}
}).start();
}
// This method is called when the background work is done
protected void applyDiffResult(List<Inventory> newItems,
DiffUtil.DiffResult diffResult) {
pendingUpdates.remove(newItems);
if (pendingUpdates.size() > 0) {
List<Inventory> latest = pendingUpdates.pop();
pendingUpdates.clear();
updateItemsInternal(latest);
}
dispatchUpdates(newItems, diffResult);
}
// updating and notifying adapter
protected void dispatchUpdates(List<Inventory> newItems,
DiffUtil.DiffResult diffResult) {
this.list = newItems;
diffResult.dispatchUpdatesTo(this);
}
}
我使用diffutil在适配器(updateItems())
上添加项目
片段:
public class MyFragment extends Fragment{
....
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cache = Cache.getInstance(getActivity());
dbH = new DBHelper(getActivity());
mContext = getContext();
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
view = inflater.inflate(R.layout.fragment_sales_entry, container, false);
mLayoutManager = new LinearLayoutManager(getContext());
mContext = getContext();
inventoryListAdapter = new MyAdapter (getContext(), this,
new ArrayList<Inventory>(), pageFlag);
inventoryListAdapter.setHasStableIds(true);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(inventoryListAdapter);
//add sample items
inventoryListAdapter.updateItems(arr_inv);
}
public static void showdialog(Inventory inv, final int pos){
final Inventory inventory = inv;
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog_price_adjustment);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_confirm);
final EditText et_orig_price = (EditText) dialog.findViewById(R.id.et_orig_price);
final EditText et_discount = (EditText) dialog.findViewById(R.id.et_discount);
final TextView tv_item_total = (TextView) dialog.findViewById(R.id.tv_item_total);
String at = "0.00";
tv_item_total.setText(at);
et_orig_price.setText(inventory.getInvSrp(""));
et_discount.setText(inventory.getDiscount());
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
et_orig_price.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0) {
Double amt = Double.parseDouble(s.toString());
String amount = formatMoney(amt);
tv_item_total.setText(amount);
} else {
tv_item_total.setText("0.00");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_discount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
et_orig_price.setText(formatMoney(s.toString()));
}
@Override
public void afterTextChanged(Editable s) {
}
});
dialog.show();
}
}