2014-10-02 113 views
1

正如标题所暗示的,我想实现所有这些功能的一次。最初我有一个全功能的side-nav,带有一个包含列表视图的SwipeRefreshLayout(使用自定义列表适配器)。然后,我在SwipeRefreshLayout中添加了一个ViewPager,并且一切主要工作...除了当我滑动列表视图有时不会出现在新的“选项卡”中。大多数时候我看到第一页和列表视图,我向右滑动,没有,我向右滑动,没有任何,我刷卡,列表视图,我刷到开头,没有。实施SwipeView,SwipeRefreshlayout和抽屉式导航在一个活动

我要补充一点,一切完全是动态的,导航项目是由我的服务器接收(这也是页数),并且每个页面都有不同的列表项的自定义列表适配器。所有这些动态信息似乎被接收并每页刷卡等

正确适应现在的代码!

抽屉布局swiperefreshlayout和视图寻呼机嵌入。 “drawer.xml”

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.widget.SwipeRefreshLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/swipe_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

</android.support.v4.widget.SwipeRefreshLayout> 
    <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 


    <!-- android:layout_gravity="start" tells DrawerLayout to treat 
     this as a sliding drawer on the left side for left-to-right 
     languages and on the right side for right-to-left languages. 
     The drawer is given a fixed width in dp and extends the full height of 
     the container. A solid background is used for contrast 
     with the content view. --> 
    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@color/grey_lighter" 
     android:dividerHeight="1dp" 
     android:background="#FFFFFF"/> 
</android.support.v4.widget.DrawerLayout> 

列表视图,其持有的活动 “dashboard.xml” 动态地创建自定义列表项:

<?xml version="1.0" encoding="utf-8"?> 


    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/LV_dashboard" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_alignRight="@+id/textView1" 
     android:layout_below="@+id/textView1" > 

    </ListView> 

现在的片段活动:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mContext = this.getApplicationContext(); 
     navItems = new ArrayList<String>(); 
     this.setContentView(R.layout.drawer); 
     mMerchantIds = Session.get().getMerchID(); //array retrieved from cache used for api 

     mCustomerPagerAdapter = new CustomerPagerAdapter(getSupportFragmentManager()); 


     // Set up the ViewPager, attaching the adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mCustomerPagerAdapter); 

     mTitle = mDrawerTitle = getTitle(); 

     mNavTitles = getResources().getStringArray(R.array.nav_array); //I realize this isn't dynamic, I haven't gotten around to that just yet. 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 
     connection = new WiselyRequest(); 

     // set a custom shadow that overlays the main content when the drawer opens 
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
     // set up the drawer's list view with items and click listener 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.nav_item, mNavTitles)); 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     // enable ActionBar app icon to behave as action to toggle nav drawer 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     // ActionBarDrawerToggle ties together the the proper interactions 
     // between the sliding drawer and the action bar app icon 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description for accessibility */ 
       R.string.drawer_close /* "close drawer" description for accessibility */ 
       ) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      selectItem(0); 
     }  
    } 

该支成两个部分,我们将与抽屉开始:

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       selectItem(position); 
      } 
     } 

     private void selectItem(int position) { 
      // update the main content by replacing fragments (this is not what the pager does) 
      Fragment fragment = null; 

      fragment = new CustomerFragment(); 
      Bundle args = new Bundle(); 
      args.putString(KEY_MERCHANT_ID, (mMerchantIds.get(position))); 
      fragment.setArguments(args); 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 


      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 

      setTitle(mNavTitles[position]); 



      mDrawerLayout.closeDrawer(mDrawerList); 

     } 

这里是查看传呼机:

public class CustomerPagerAdapter extends FragmentPagerAdapter { 

      public CustomerPagerAdapter(FragmentManager fm) { 
       super(fm); 
      } 

      @Override 
      public Fragment getItem(int i) { 
       Fragment fragment = new CustomerFragment(); 
       Bundle args = new Bundle(); 
       Log.d("Wisely", "merchants: "+mMerchantIds.get(i)); 
       args.putString(KEY_MERCHANT_ID, (mMerchantIds.get(i))); // Our object is just an integer :-P 
       fragment.setArguments(args); 
       return fragment; 
      } 

      @Override 
      public int getCount() { 
       // For this contrived example, we have a 100-object collection. 
       return mMerchantIds.size(); //equal to the number of merchantss (that many customer objects) 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       return "OBJECT " + (position + 1); 
      } 
     } 

下面是两个抽屉和寻呼机是在刷新利用和地方的处理客户的片段:

public class CustomerFragment extends Fragment implements OnRefreshListener { 
       private View rootView; 
       SwipeRefreshLayout swipeLayout; 
       public CustomerFragment() { 

       } 

       @Override 
       public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
        Bundle args = getArguments(); 
        String merchantID = args.getString(KEY_MERCHANT_ID); 
        if(connection.checkConnectivity(getApplicationContext())){ 
         getRecentCustomers(API.getRecentCustomers()+"?merchant_id="+merchantID); //api call works totally fine, and correctly sets up the adapter 
        } 
        else 
         Toast.makeText(DashboardActivity.this, "Need to be connected to the internet!", 4000).show(); 
        rootView = inflater.inflate(R.layout.dashboard, container, false);//inflates the dashboard 
        swipeLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container); 
        swipeLayout.setOnRefreshListener(this); 
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); 
        registerClickCallback(rootView); 

        return rootView;  
       } 

       @Override 
       public void onRefresh() { 
        if(connection.checkConnectivity(getApplicationContext())) 
         reloadActivity(); //literally turns the activity on and off without animation 
        else 
         Toast.makeText(DashboardActivity.this, "Need to be connected to the internet!", 4000).show(); 
       } 

我相信这个问题退出在我布局,而不是列表适配器或客户片段似乎工作正常(每次我滑动到新页面的API被调用,并将正确的数据放入列表视图。我只是看不到它。我也意识到navdrawer不是动态的,但我目前更大的问题是能够在页面上浏览多个列表视图。当我改变我的customerFragment以使其中的数字充满简单的文本视图时,它似乎工作正常,除了第一页永远不会消失,其他页面只会堆积在它之上(我认为这与框架布局有关在抽屉里,但删除它打破了代码,因为我的唠叨抽屉依赖于它)。有什么建议么?

回答

0

我想,你必须创建扩展DrawerLayout或ViewPager自己的类。 里面你的类,你必须覆盖的方法:

onInterceptTouchEvent() - 在这里你评估的TouchEvent并返回true,如果你正在拦截它(不传递给子View)

你必须调整条件拦截的基础上你试图达到什么。

指南在这里: http://developer.android.com/training/gestures/viewgroup.html