Android ViewPager+Fragment实现左右滑屏效果 Android基于ViewPager+Fragment实现左右滑屏效果的方法
只要你能好 人气:0想了解Android基于ViewPager+Fragment实现左右滑屏效果的方法的相关内容吗,只要你能好在本文为您仔细讲解Android ViewPager+Fragment实现左右滑屏效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ViewPager,Fragment,滑屏,下面大家一起来学习吧。
本文实例讲述了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程序设计有所帮助。
加载全部内容