时间:2021-01-09 11:17:13 | 栏目:Android代码 | 点击:次
本文实例讲述了Android基于ViewPager+Fragment实现左右滑屏效果的方法。分享给大家供大家参考,具体如下:
1.xml布局模板
<android.support.v4.view.ViewPager android:id="@+id/local_software_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#78A7A1F1" />
2.fragment布局模板(可选)
<fragment android:name="cn.edu.gzu.BackPP.Fragment.AllSoftwareFragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
3.fragment适配
import cn.edu.gzu.BackPP.activities.R; import android.support.v4.app.Fragment; public class UserSoftwareFragment extends Fragment { private RelativeLayout loadingLayout; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //相关逻辑处理部分 } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //布局文件适配部分 View view=inflater.inflate(R.layout.fragment_user_software_manager, null); softList=(ListView) view.findViewById(R.id.fragment_user_software_listv); loadingLayout=(RelativeLayout) view.findViewById(R.id.user_loading_rlayout); return view; } }
4.activity匹配
import java.util.ArrayList; import cn.edu.gzu.BackPP.Fragment.AllSoftwareFragment; import cn.edu.gzu.BackPP.Fragment.UserSoftwareFragment; import cn.edu.gzu.BackPP.adapter.MyFragmentAdapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; public class LocalSoftwareManagerActivity extends FragmentActivity{ private ViewPager viewPager; private Fragment allsoftFrag; private Fragment usersoftFrag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_local_software_manager); findViewById(); } private void findViewById(){ viewPager=(ViewPager) findViewById(R.id.local_software_viewpager); initFragment(); } private void initFragment(){ ArrayList<Fragment> mFragments=new ArrayList<Fragment>(); allsoftFrag=new AllSoftwareFragment(); usersoftFrag=new UserSoftwareFragment(); mFragments.add(allsoftFrag); mFragments.add(usersoftFrag); viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),mFragments)); viewPager.setCurrentItem(0); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { switch(arg0){ case 0: //滑动至第一页处理逻辑 break; case 1: //滑动至第二页处理逻辑 break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。