创建ViewPager或同等学历,在Android的功能 [英] Create a ViewPager or equivalent WITH functionality in Android

查看:120
本文介绍了创建ViewPager或同等学历,在Android的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能听起来像一个可怕的问题要问,但我一直在研究,就像我能算出这个。我有一个需要查看传呼机水平滚动显示不同的视图的应用程序。内的每个视图,它需要的功能,为一个视图它可能只是pressing一个按钮,另一视图需要从服务器下载数据(例如,检索最新Twitter的饲料),以及其他功能。主要的一点是,查看传呼机内,每个视图需要的功能。

I know this may sound like a terrible question to ask but I have been researching as much as I could to figure this out. I have an application that requires a view pager to scroll horizontally to display different views. Within each view, it needs functionality, for one view it could be just pressing a button, another view is required to download data from a server (for example, retrieving the latest Twitter feed) as well as other functionality. The main point is that within the View Pager, each view requires functionality.

我最初的想法是按照这个教程:

My original idea was to follow this tutorial:

<一个href=\"http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/\" rel=\"nofollow\">http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

然而,这仅仅是提供具有无相互作用的观点。我已经成功地实现了这一点,并添加我的布局,然而这只是解决了问题的一半的。它显示了如何如一个按钮的注释中添加基本操作。我想每个视图都有自己的活动,它能够做自己独特的东西。

However, this is just providing views which have no interaction. I have managed to implement this and add my layouts, however this is only solving half of the problem. It shows how to add basic operations within a comment such as a single button. I want each view to have its own activity which is capable of doing its own unique thing.

下面是我本来有:

public class DashboardContainerActivity extends Activity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //Set content view to the dashboard container xml view
    setContentView(R.layout.dashboard_container);

    //Create a new instance of my page adapter from below
    MyPageAdapter adapter = new MyPageAdapter();
    //Reference the view pager used in the dashboard container xml view
    ViewPager myPager = (ViewPager) findViewById(R.id.dashboardpanelpager);
    //set an adapter to the view pager
    myPager.setAdapter(adapter);
    //First panel to be shown when opened
    myPager.setCurrentItem(3);
  }

}

/*------------------------------------------------------*/
class MyPageAdapter extends PagerAdapter {

  public int getCount() {
      //Return 7 as there will be 7 panes in the dashboard
      return 7;
  }

  public Object instantiateItem(View collection, int position) {

      LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View v = null;
      switch (position) {
      case 0:
          v = inflater.inflate(R.layout.dashboard_social, null);
          break;
      case 1:
          v = inflater.inflate(R.layout.dashboard_info, null);
          //DISPLAY INFORMATION FROM SERVER AND DISPLAY HERE
          break;
      case 2:
          v = inflater.inflate(R.layout.dashboard_allcourses, null);
          //LIST OF COURSES

          break;
      case 3:
          v = inflater.inflate(R.layout.dashboard_news, null);
          //USE HTTP HERE FOR TWITTER FEED


          break;
      case 4:
          v = inflater.inflate(R.layout.dashboard_mycourse, null);
          //DOWNLOAD USER'S PERSONAL INFORMATION AND DISPLAY HERE

          break;
      case 5:
          v = inflater.inflate(R.layout.dashboard_media, null);
          //DISPLAY LATEST UPLOADED MULTIMEDIA
          break;
      case 6:
          v = inflater.inflate(R.layout.dashboard_extras, null);
          break;
      }

      ((ViewPager) collection).addView(v, 0);

       return v;
  }

  @Override
  public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((View) arg2);

  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);

  }

  @Override
  public Parcelable saveState() {
      return null;
  }
 }

我对加入ViewPager每个版面单独活动,但我猜测,这些活动不能被添加到ViewPager而不仅仅是布局。

I have separate activities for each layout added to the ViewPager but I am guessing that these activities cannot be added into the ViewPager instead of just the layouts.

我看过一些有关的片段,但我不知道这是否是与API级别8兼容,显然这是针对Honeycomb和Ice Cream Sandwich系统。

I have read something about fragments, but I am not sure whether that's compatible with API level 8, apparently this is targeted at Honeycomb and Ice Cream Sandwich.

推荐答案

这是不可能有一个活动作为ViewPager的一部分,但没有理由为什么你不能在你所描述的功能添加到每个页面的ViewPager。要分配在每个视图的交互或事件的组件只是添加正确的听众instantiateItem()在每个case语句:

It's not possible to have an Activity as part of a ViewPager, however there is no reason why you can't add the functionality you describe to each page in your ViewPager. To assign interaction or events to components in each view just add the correct listeners in instantiateItem() in each case statement:

case 0:
      v = inflater.inflate(R.layout.dashboard_social, null);
      Button myButton = (Button) v.findViewById(R.id.name_of_button_in_social_dashboard);
      myButton.setOnClickListener(...);
      break;

有关其他任何互动,喜欢把自己的http请求的Twitter的饲料,只执行那些作为主活动的一部分(类似于HTTP请求应该理所当然的后台线程来完成)。当你想更新的Twitter页面的UI,只需使用 ViewPager.getChildAt(3)来获取子元素。您的活动看作只是一个大的布局,7名儿童的观点是所有可用的一次(但用户只能看到他们,因为他们刷卡)。

For any other interactions, like getting the http requests for the twitter feed, just execute those as part of your main activity (something like http requests should be done in a background thread of course). When you want to update the UI in the twitter page, just use ViewPager.getChildAt(3) to fetch the child element. Think of your Activity as just a big layout with 7 children views that are all available at once (but the user will only see them as they swipe).

与所有的说,一个更好的设计模式可能是使用片段用的 FragmentPagerAdapter 支持你ViewPager。这使得不同的页面放到不同类别的更好的逻辑分解 - 碎片还提供其他用途,如能够为更大的屏幕布局(片)一次加载屏幕上的多个

With all that said, a better design patter might be to use Fragments with a FragmentPagerAdapter backing your ViewPager. This allows better logical breakdown of the various pages into different classes - Fragments also provide other uses like being able to load multiple on-screen at once for larger screen layouts (tablets).

像ViewPager,片段通过支持库回到API级别4所有的方式都可以(见的片段)。所以你不必担心向后兼容。

Like ViewPager, Fragments are available all the way back to API Level 4 via the support library (see Fragment). So you don't need to worry about backward compatibility.

这篇关于创建ViewPager或同等学历,在Android的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆