刷新适配器时如何保持RecyclerView的滚动位置?

时间:2018-03-13 04:43:19

标签: android multithreading android-fragments recycler-adapter android-timer

我创建了一个多计时器应用程序。在那里,我使用RecyclerView将每个计时器显示为一个列表。按照我的逻辑每秒,我调用RecyclerView并刷新屏幕以更新秒值并显示它。但我的问题是当计时器列表增加并滚动屏幕时,屏幕没有按住滚动位置。那我该怎么办呢?等待更好的建议。

以下是我的代码: -

public class Time_Tab_Activity extends AppCompatActivity {

 private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Running_Timer_Fragment(), "Running Timer");
        adapter.addFragment(new Stop_Timer_Fragment(), "View Time");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}


public class Running_Timer_Fragment extends Fragment {

 private void countdowntimer() {
        new CountDownTimer(1000, 1000) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {
                showall();
            }

        }.start();
    }

    private void showall()
    {
        Plant_arraylist.clear();
        if(mydb.numberOfRows_PO_Time()>0)
        {
            Log.i("Condition","True");

            if (isAdded()){
                Plant_arraylist = mydb.Running_Get_PO_Time_Details(poNumber);
                RecyclerViewNewtime recyclerViewadapter1 = new RecyclerViewNewtime(Plant_arraylist,getActivity().getBaseContext());
                recyclerView.setAdapter(recyclerViewadapter1);
                countdowntimer();

                // Save state
                 Parcelable recyclerViewState;
                recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();

// Restore state
                recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);

            }
        }
    }
}

public class RecyclerViewNewtime extends RecyclerView.Adapter<RecyclerViewNewtime.ViewHolder>{

 @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {





        final DataAdapter dataAdapter = dataAdapters.get(position);

        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
        String diff ="",diff1="";
        int hours = 0,hours1=0;
        int mins = 0,min1=0;
        int sec=0,sec1=0;
        Date date1 = null,date2=null,date3=null;

        Calendar c = Calendar.getInstance();
        String startDate = format.format(c.getTime());
        try {
            Log.e("Start date",startDate  );
            date1 = format.parse(dataAdapter.getSTART_TIME_D());
            date2 = format.parse(startDate);
            long mills = date2.getTime() - date1.getTime();
            //hours = (int) (mills/(1000 * 60 * 60));
            //mins = (int) (mills/(1000*60)) % 60;
            mins=(int) (mills / 1000)  / 60;
            sec=(int) (mills/1000)%60;


            diff =  String.format("%02d", mins)+ ":"
                    +String.format("%02d", sec);

            date3=format.parse(dataAdapter.getPAUSE_TIME_D());
            long mills1= date3.getTime()-date1.getTime();

           // hours1 = (int) (mills1/(1000 * 60 * 60));
            //min1 = (int) (mills1/(1000*60)) % 60;
            min1=mins=(int) (mills1 / 1000)  / 60;
            sec1= (int) (mills1/1000)%60;

            diff1 =  String.format("%02d", min1)+ ":"
                    +String.format("%02d", sec1);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        String holderpo = "<b>" + "PO: " + "</b> " + "<font color='#3F51B5'>" + dataAdapter.getPRODUCTION_ORDER_D() + "</font>";
        holder.po.setText(Html.fromHtml(holderpo));
        String holderwc = "<b>" + "WC: " + "</b> " + "<font color='#3F51B5'>" + dataAdapter.getWORK_CENTER_D() + "</font>";
        holder.wc.setText(Html.fromHtml(holderwc));
        String holderop = "<b>" + "OP: " + "</b> " + "<font color='#3F51B5'>" + dataAdapter.getOPERATION_D() + "</font>";
        holder.op.setText(Html.fromHtml(holderop));
        String holderopr = "<b>" + "Operator: " + "</b> " + "<font color='#3F51B5'>" + dataAdapter.getOPERATOR_D() + "</font>";
        holder.opr.setText(Html.fromHtml(holderopr));
        /*holder.stp.setText("Time: "+dataAdapter.getSTOP_TIME_D()+":00");*/

1 个答案:

答案 0 :(得分:0)

仅第一次调用setAdapter,并且在更新数据调用之后仅调用notifyDataSetChanged();.它将更新数据而不向后滚动