如何在recyclerView适配器中只运行一次代码?

时间:2017-11-29 07:51:40

标签: android android-fragments android-recyclerview recycler-adapter

在我的应用程序中,我想使用RecyclerView,为此我在RecyclerView中设置了多个布局
我使用以下代码片段中的按钮更改了布局:

public class AuctionCurrentFragment extends BaseFragment {
    @BindView(R.id.recyclerList)
    RecyclerView recyclerList;
    private ImageView list_item;
    private ArrayList countries = new ArrayList<>();
    private RecyclerView.Adapter adapter;
    private Context context;
    private LinearLayoutManager layoutManager;
    private boolean clickFlag = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_auction_current_tab, null);

        ButterKnife.bind(this, v);
        context = getActivity();
        list_item = getActivity().findViewById(R.id.list_item);
        recyclerList.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerList.setLayoutManager(layoutManager);
        adapter = new AuctionCurrentRecyclerAdapter(context, countries, 1);

        initViews();

        list_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickFlag) {
                    adapter = new AuctionCurrentRecyclerAdapter(context, countries, 1);
                    recyclerList.setAdapter(adapter);
                    clickFlag = false;
                } else {
                    adapter = new AuctionCurrentRecyclerAdapter(context, countries, 2);
                    recyclerList.setAdapter(adapter);
                    clickFlag = true;
                }
            }
        });

        return v;
    }

    private void initViews() {

        for (int i = 0; i <= 10; i++) {
            countries.add("Title " + i);
        }
        recyclerList.setAdapter(adapter);
    }
}

在其中一个布局中,我使用progressBar来显示 count downer ,但是当显示此布局设置时再切换layouts(使用片段中的按钮)唐纳!
但是我希望当切换布局不能再次启动时,继续计算downer。

适配器代码:

public class AuctionCurrentRecyclerAdapter extends RecyclerView.Adapter {
    private ArrayList<String> countries;
    private CountDownTimer countDownTimer;
    boolean isRunning = false;
    long timeInMillis;
    private int timeToEnd;
    private View view;
    private int itemLayout = 0;
    private LayoutInflater mInflater;
    private Context context;

    public AuctionCurrentRecyclerAdapter(Context context, ArrayList<String> countries, int itemLayout) {
        this.context = context;
        this.countries = countries;
        this.itemLayout = itemLayout;
        mInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        switch (itemLayout) {
            case 1:
                view = mInflater.inflate(R.layout.list_item_auction_large_new, viewGroup, false);
                return new LargeViewHolder(view);
            case 2:
                view = mInflater.inflate(R.layout.list_item_auction_normal, viewGroup, false);
                return new NormalViewHolder(view);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int i) {
        switch (itemLayout) {
            case 1:
                ((LargeViewHolder) viewHolder).tv_country.setText(countries.get(i));
                break;
            case 2:
                ((NormalViewHolder) viewHolder).tv_country.setText(countries.get(i));
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (itemLayout == 1) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {
        return countries.size();
    }

    public class LargeViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_country, progressTxt;
        private ProgressBar progressBar;

        public LargeViewHolder(View view) {
            super(view);

            tv_country = (TextView) view.findViewById(R.id.text_view_16962);
            progressTxt = (TextView) view.findViewById(R.id.progressTxt);
            progressBar = view.findViewById(R.id.progress);

            timeToEnd = new Random().nextInt(100);
            long timeInput = timeToEnd * 1000;
            timeInMillis = timeInput;
            progressBar.setMax((int) timeInMillis / 1000);
            isRunning = true;

            countDownTimer = new CountDownTimer(timeInMillis, 100) {
                @Override
                public void onTick(long millisUntilFinished) {
                    progressTxt.setText("" + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
                    progressBar.setProgress(Math.round(millisUntilFinished * 0.001f));
                }

                @Override
                public void onFinish() {

                }
            }.start();
            countDownTimer.start();
        }
    }

    public class NormalViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_country;

        public NormalViewHolder(View view) {
            super(view);

            tv_country = (TextView) view.findViewById(R.id.text_view_16962);
        }
    }
}

我怎么能这样?

但是我希望当切换布局不能再次启动时,继续算下去。

2 个答案:

答案 0 :(得分:0)

每次单击listview时都在创建适配器.Instead在适配器内创建一个方法来设置itemlayout

public void setItemLayout(int itemLayout){
    this.itemLayout = itemLayout;
    notifyDataSetChanged();
}

在onclick方法中调用setItemLayout方法。

if(adapter==null){
    adapter = new AuctionCurrentRecyclerAdapter(context, countries, 1);
    recyclerList.setAdapter(adapter);
}
if (clickFlag) {
    adapter.setItemLayout(1);
    clickFlag = false;
} else {
    adapter.setItemLayout(2);
    clickFlag = true;
}

答案 1 :(得分:-1)

再次将适配器分配给recyclerView时,将再次创建视图和ViewHolder,因此您的countDownTimer将再次启动。要修改RecyclerView的内容,最好不要重新创建适配器,而是调用&#39; notifyDataSet()&#39;。

关于您的问题,您能描述一下您想要实现的目标吗?你真的需要在每个ViewHolder中创建一个CountDownTimer吗?另外,请注意,在执行此操作时,您始终存储在相同的countDownTimer中,因此您只能引用其中一个countDowns。如果这对您的用例不是强制性的,您可以在RecyclerView中声明它。

此外,作为性能提示,建议避免在bindViewHolder()或createViewHOlder()中创建对象(分配内存),以避免在滚动内容时出现滞后。