视图寻呼机的无限适配器概念

时间:2014-09-08 13:33:26

标签: android

任何人都可以解释如何为查看寻呼机制作无限的适配器概念

我目前正在使用查看寻呼机查看我的数据。在每次浏览视图寻呼机时,我需要点击服务器并采取动态响应并需要更新viewpager。显然我们需要使用无限的适配器概念。但我对确切的概念感到困惑。任何人请做必要的......

提前致谢...

2 个答案:

答案 0 :(得分:0)

好吧,让我们从头开始吧。

如果你想拥有“无穷无尽”的页数,你需要使用一些技巧。例如。你不能在内存中存储无数个页面。可能Android会在每次看不到时都会破坏PageView。为了避免破坏和重新创建这些视图,你可以考虑使用回收机制,例如,列表显示。 Here您可以检查和分析如何实现寻呼机适配器的回收机制。

此外,为了使您的UI流畅,请在用户进入第X0页(10,20,30,40 ......)之前尝试发出请求并下载新数据。您可以开始下载数据,例如当用户处于X5th(5,15,25 ......)页面时。将数据从请求存储到模型(可能是例如sqlite db),以及基于页码的用户适当数据。

这只是一个简短的解决方案,但它也是一个有趣的问题;)

修改

我开始寻找灵感,刚刚找到Jake Wharton实施的独立视图回收工具并致电Salvage。也许这将是为您的问题创建解决方案的良好开端。

答案 1 :(得分:0)

我实现了一个无尽的ViewPager。我觉得它适合你的需要。在AsyncTask线程中使用时间延迟模拟请求。

// ViewPagerActivity

public class ViewPagerActivity extends FragmentActivity {

    private ViewPager vp_endless;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_endless_view_pager);

        vp_endless = (ViewPager) findViewById(R.id.vp_endless);
        vp_endless.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager()));
    }

}

// FragmentViewPagerAdapter

public class FragmentViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<CustomObject> _customObjects;
    private volatile boolean isRequesting;
    private static final int ITEMS_PER_REQUEST = 10;


    public FragmentViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        _customObjects = HandlerCustomObject.INSTANCE._customObjects;
    }

    @Override
    public Fragment getItem(int position) {
        CustomFragment fragment = new CustomFragment();
        fragment.setPositionInViewPager(position);

        if (position == _customObjects.size() && !isRequesting)
            new AsyncRequestItems().execute("www.test.com");

        return fragment;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    public class AsyncRequestItems extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... urls) {
            isRequesting = true;

            //Fake request lag
            try {Thread.sleep(2500);}
            catch (InterruptedException e) {e.printStackTrace();}

            for (int i = 0; i < ITEMS_PER_REQUEST; i++) {
                _customObjects.add(new CustomObject());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            isRequesting = false;
        }

    }

}

// CustomFragment

public class CustomFragment extends Fragment {

    private CustomObject _customObject;
    private TextView tv_position;
    private ProgressBar pb_loading;
    private View root;
    private int _positionInViewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frament_endless_view_pager, container, false);
        pb_loading = (ProgressBar) root.findViewById(R.id.pb_loading);
        tv_position = (TextView) root.findViewById(R.id.tv_position);

        _customObject = retrieveDataSafety();
        if(_customObject != null) bindData();
        else createCountDownToListenerForUpdates();

        return root;
    }

    public void createCountDownToListenerForUpdates() {
        new CountDownTimer(10000, 250) {

            public void onTick(long millisUntilFinished) {
                _customObject = retrieveDataSafety();
                if(_customObject != null) {
                    bindData();
                    cancel();
                }
            }

            public void onFinish() {}

        }.start();
    }

    private CustomObject retrieveDataSafety() {
        List<CustomObject> customObjects = HandlerCustomObject.INSTANCE._customObjects;
        if(customObjects.size() > _positionInViewPager)
            return customObjects.get(_positionInViewPager);
        else
            return null;
    }

    private void bindData() {
        pb_loading.setVisibility(View.GONE);
        String feedback = "Position: " + _positionInViewPager;
        feedback += System.getProperty("line.separator");
        feedback += "Created At: " + _customObject._createdAt;
        tv_position.setText(feedback);
    }

    public void setPositionInViewPager(int positionAtViewPager) {
        _positionInViewPager = positionAtViewPager;
    }

}

// CustomObject

public class CustomObject {
    public String _createdAt;

    public CustomObject() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        _createdAt = dateFormat.format(new Date());
    }
}

// HandlerCustomObject

public enum  HandlerCustomObject {
    INSTANCE;

    public List<CustomObject> _customObjects = new ArrayList<CustomObject>();

}