包含recyclelerview数据的TabLayout在backPressed

时间:2017-02-01 11:10:57

标签: android android-recyclerview fragment android-tablayout

使用activity,tabLayout和片段。从包含tabLayout的活动移动时,在其第一个Tab中包含RecyclerView,以显示其中一个reclerView项的数据。当用户按下后退按钮时,recyclelerView数据将被删除。 尝试调试代码,源不会创建tabLayout或重新获取数据。在backPressed上没有调用任何方法。

包含tabLayouts

的类文件代码
public class HomeFragment extends Fragment implements TabLayout.OnTabSelectedListener {

@BindView(R.id.tvRouteName)
TextView tvRouteName;
@BindView(R.id.pager)
ViewPager viewPager;
@BindView(R.id.tabLayout)
TabLayout tabLayout;
private TabPagerAdapter adapter;

public HomeFragment() {
    // Required empty public constructor
}

public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, view);

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("All"));
    tabLayout.addTab(tabLayout.newTab().setText("Pending"));
    tabLayout.addTab(tabLayout.newTab().setText("Complete"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //Creating our pager adapter
    adapter = new TabPagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);

    tabLayout.setupWithViewPager(viewPager);

    // set route name to the route
    String routeName = SiconApp.getInstance().getRouteName();
    if (routeName != null && !routeName.isEmpty())
        tvRouteName.setText(routeName);

    return view;
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    //viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}

所有努力都适用。尝试从onViewCreated,onResume和刷新tabLayout等更改代码。

下面提供的图片是为了找出recyclerView中的总金额

CheckImage

1 个答案:

答案 0 :(得分:1)

将此答案视为上述评论的回复:

  

数量可以动态更改,金额根据数量和金额变化的GrandTotal进行更改。我已经实现了这一点,但每次我一次又一次地阅读整个数组时都会改变文本。如果你有更好的方法来完成这个。

首先,在OnTextChange()进行计算并不好。相反,您应该考虑点击事件widget 小部件可以是一些button或键盘的done按钮。

以下是其他方法: -

  • 尝试打开微调器 - pro如果您提供最大数量,则可以限制数量
  • 点击dialog进行计算,打开editText done并完成按钮。

    [更新1]

    添加我对计算的想法: -

    // Calculate GrandTotal for the very first time
    int grandTotal = 0;
    for (int i = 0; i < mDataSet.size(); i++) {
        // addition of all the sub-totals
    }
    
    // Get an event of quantity change of recyclerview row - using dialogbox or spinner or anything
    // basePrice is price of 1 quantity
    // oldQuantity is before updating new quantity to model
    currowModel = mDataSet.get(position); // Assuming currModel as current dataset item
    grandTotal = grandTotal - (currowModel.getBasePrice() * currModel.getQuantity());
    currowModel.setQuantity(updatedQuantity); // Setting new quanity to model
    grandTotal = grandTotal + (currowModel.getBasePrice() * currModel.getQuantity());
    

快乐的编码!