Android中的Fragment能否实现下拉刷新

xiao猪 发布于 2013/11/14 16:01
阅读 2K+
收藏 1

本人才刚刚上学毕业,在一家公司实习开发android。

项目之中要用到左右滑页和下拉刷新的功能,本应该每一个布局文件对应一个Activity。

但为了实现左右滑页的功能,我用了ActionBar和Fragment,需要把几个文件都继承了Fragment,但这个时候问题也就来了,继承了Fragment之后,下拉刷新的功能就在这卡壳了,因为我在网上看到大部分下拉刷新的例子都是继承Activity的。想了好几天,实在是想的头痛。

希望各位师兄师姐帮帮忙,解答一下,最好能够附上代码。。谢谢大家了。

最后我附上我的代码

 

RightActivity类:

public class RightActivity extends FragmentActivity implements ActionBar.TabListener {
 
 private PageActivity pageFrament = new PageActivity();
 private OnselfActivity onselfFrament = new OnselfActivity();
 private ManyActivity manyFrament = new ManyActivity();
 
  private static final int TAB_INDEX_COUNT = 3; 
    
     private static final int TAB_INDEX_ONE = 0; 
     private static final int TAB_INDEX_TWO = 1; 
     private static final int TAB_INDEX_THREE = 2; 
      
     private ViewPager mViewPager; 
     private ViewPagerAdapter mViewPagerAdapter; 
      
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.main); 
          
         setUpActionBar(); 
         setUpViewPager(); 
         setUpTabs(); 
     } 
      
  private void setUpActionBar() { 
         final ActionBar actionBar = getActionBar(); 
         actionBar.setHomeButtonEnabled(false); 
         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
         actionBar.setDisplayShowTitleEnabled(false); 
         actionBar.setDisplayShowHomeEnabled(false); 
     } 
      
     private void setUpViewPager() { 
         mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 
          
         mViewPager = (ViewPager)findViewById(R.id.pager); 
         mViewPager.setAdapter(mViewPagerAdapter); 
         mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 

    @Override 
             public void onPageSelected(int position) { 
                 final ActionBar actionBar = getActionBar(); 
                 actionBar.setSelectedNavigationItem(position); 
             } 
              
             @Override 
             public void onPageScrollStateChanged(int state) { 
                 switch(state) { 
                     case ViewPager.SCROLL_STATE_IDLE: 
                         //TODO 
                         break; 
                     case ViewPager.SCROLL_STATE_DRAGGING: 
                         //TODO 
                         break; 
                     case ViewPager.SCROLL_STATE_SETTLING: 
                        //TODO 
                         break; 
                     default: 
                         //TODO 
                         break; 
                 } 
             } 
         }); 
     } 
      
   
  private void setUpTabs() { 
         final ActionBar actionBar = getActionBar(); 
         for (int i = 0; i < mViewPagerAdapter.getCount(); ++i) { 
             actionBar.addTab(actionBar.newTab() 
                     .setText(mViewPagerAdapter.getPageTitle(i)) 
                     .setTabListener(this)); 
         } 
     } 
      
     @Override 
     protected void onDestroy() { 
         super.onDestroy(); 
     } 
      
     public class ViewPagerAdapter extends FragmentPagerAdapter { 
  
         public ViewPagerAdapter(FragmentManager fm) { 
             super(fm); 
             // TODO Auto-generated constructor stub 
         } 
  
         @Override 
         public Fragment getItem(int position) { 
             // TODO Auto-generated method stub 
             switch (position) { 
                 case TAB_INDEX_ONE: 
                     return pageFrament; 
                 case TAB_INDEX_TWO: 
                     return onselfFrament; 
                 case TAB_INDEX_THREE: 
                     return manyFrament; 
             } 
             throw new IllegalStateException("No fragment at position " + position); 
         } 
  
         @Override 
         public int getCount() { 
             // TODO Auto-generated method stub 
             return TAB_INDEX_COUNT; 
         }    
         @Override 
         public CharSequence getPageTitle(int position) { 
             String tabLabel = null; 
             switch (position) { 
                 case TAB_INDEX_ONE: 
                     tabLabel = getString(R.string.pageMe); 
                     break; 
                 case TAB_INDEX_TWO: 
                     tabLabel = getString(R.string.onselfMe); 
                     break; 
                 case TAB_INDEX_THREE: 
                     tabLabel = getString(R.string.manyMe); 
                     break; 
             } 
             return tabLabel; 
         } 
     }   

 @Override
 public void onTabReselected(Tab tab, FragmentTransaction arg1) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onTabSelected(Tab tab, FragmentTransaction arg1) {
  // TODO Auto-generated method stub
  mViewPager.setCurrentItem(tab.getPosition());
 }

 @Override
 public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
  // TODO Auto-generated method stub
 } 

}

 

 

ManyActivity类:

public class ManyActivity extends Fragment {
  @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
         return inflateAndSetupView(inflater, container, savedInstanceState, R.layout.many);     
     }

 private View inflateAndSetupView(LayoutInflater inflater,
   ViewGroup container, Bundle savedInstanceState, int many) {
  // TODO Auto-generated method stub
   View layout = inflater.inflate(many, container, false); 
  return layout;
 } 

还有两个类,跟ManyActivity一样。所以就不附上了

加载中
0
Glide
Glide
都可以的。Fragment甚至还可以再嵌套Fragment.
x
xiao猪
但我就是不知道该怎么往下写代码了。。网上大部分例子都是继承Activity
0
Xushao
Xushao
能啊,甚至很多时候比activity灵活。
Xushao
Xushao
回复 @xiao猪 : 就是你看看换成fragment,卡在了哪里
x
xiao猪
师兄,能再具体点吗?还是有些不太明白
Xushao
Xushao
回复 @xiao猪 : 下拉刷新的话,如果需要用到activity的上下文,你改成getActivity()或者getSherlockActivity()代替就行。
x
xiao猪
依我的这代码该如何往下继续代码
返回顶部
顶部