仿微信界面滑动(9.7)
2015-09-07 10:18
453 查看
主布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/fragment_viewPager" android:layout_height="match_parent" android:layout_weight="1" android:layout_width="match_parent"></android.support.v4.view.ViewPager> <!--<fragment--> <!-- --> <!--class="com.test.dukang.myfragment.fragment.MyFirstFragment"--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"/>--> <FrameLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content"></FrameLayout> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:gravity="center"> <RadioButton android:id="@+id/radioButton1" android:drawableTop="@drawable/msg_background" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" style="@style/textStyle" /> <RadioButton android:id="@+id/radioButton2" android:drawableTop="@drawable/contact_background" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" style="@style/textStyle"/> <RadioButton android:id="@+id/radioButton3" android:drawableTop="@drawable/kongjian_background" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" style="@style/textStyle"/> </RadioGroup> </LinearLayout>
其他布局文件
"**********************第一个布局fragment_test**********************" <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> "**********************第二个布局fragment_second**********************" <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好吗?" android:textSize="24sp"/> </LinearLayout> "**********************第三个布局fragment_thrid**********************" <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好啊,好啊"/> </LinearLayout>
主程序MainActivity
package com.test.dukang.myfragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import com.test.dukang.myfragment.fragment.MyFirstFragment; import com.test.dukang.myfragment.fragment.MySecondFragment; import com.test.dukang.myfragment.fragment.MyThridFragment; import com.test.dukang.myfragment.fragment.com.test.adapter.MyFragmentAdapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity { private RadioGroup mRadioGroup; private FragmentManager mFragmentManager; private RadioButton mRadioButton1; private RadioButton mRadioButton2; private RadioButton mRadioButton3; private FragmentTransaction transaction; private MySecondFragment fragment2; private MyFirstFragment fragment; private MyThridFragment fragment3; private ViewPager mViewPager; private List<Fragment> mFragmentList; private MyFragmentAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup); mViewPager= (ViewPager) findViewById(R.id.fragment_viewPager); mFragmentManager = getSupportFragmentManager(); mFragmentList=new ArrayList<>(); fragment = new MyFirstFragment(); fragment2 = new MySecondFragment(); fragment3 = new MyThridFragment(); mFragmentList.add(fragment); mFragmentList.add(fragment2); mFragmentList.add(fragment3); mAdapter= new MyFragmentAdapter(mFragmentManager,mFragmentList); mViewPager.setAdapter(mAdapter); mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int radioButtonId = group.getCheckedRadioButtonId(); switch (radioButtonId) { case R.id.radioButton1: mViewPager.setCurrentItem(0); break; case R.id.radioButton2: mViewPager.setCurrentItem(1); break; case R.id.radioButton3: mViewPager.setCurrentItem(2); break; default: Toast.makeText(getApplication(), "sd", Toast.LENGTH_SHORT).show(); break; } } }); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: break; case 1: break; case 2: break; } } @Override public void onPageScrollStateChanged(int state) { } }); }
MyFragmentPagerAdapter
package com.test.dukang.myfragment.fragment.com.test.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by Administrator on 2015/9/7. */ public class MyFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> mList; public MyFragmentAdapter(FragmentManager fm,List<Fragment> mList) { super(fm); this.mList=mList; } @Override public Fragment getItem(int position) { return mList.get(position); } @Override public int getCount() { return mList.size(); } }
其他Fragment程序
"*********************MyFirstFragment********************" package com.test.dukang.myfragment.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import com.test.dukang.myfragment.R; /** * Created by Administrator on 2015/9/6. */ //导入support.v4的包,注意这是重点 public class MyFirstFragment extends Fragment { private Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_test,null); button= (Button) view.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(),"你好",Toast.LENGTH_SHORT).show(); } }); return view; } } "*********************MySecondFragment********************" package com.test.dukang.myfragment.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.test.dukang.myfragment.R; /** * Created by Administrator on 2015/9/6. */ public class MySecondFragment extends Fragment { private TextView mTextView; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_second,null); mTextView= (TextView) view.findViewById(R.id.textView); return view; } public void setmTextView(String text){ this.mTextView.setText(text); } } "*********************MyThridFragment********************" package com.test.dukang.myfragment.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.test.dukang.myfragment.R; /** * Created by Administrator on 2015/9/6. */ public class MyThridFragment extends Fragment { private EditText mEditText; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_thrid,null); mEditText= (EditText) view.findViewById(R.id.editText); return view; } public String getText(){ String s=null; if(mEditText!=null){ s=mEditText.getText().toString(); } return s; } }
效果图
FragmentStatePagerAdapter
查看API当界面较多时,使用该方法来避免出错,具体用法查看api
相关文章推荐
- 微信分享
- 上线10天 来看看微信读书中的“匠心”
- 【技术】悬浮微信二维码微博分享置顶
- 微信公众平台所带来的互动式营销
- 零java基础搞定微信Server_4:写一个servlet的demo
- 如何判断不是微信内置浏览器
- Java内部类小程序(成员内部类,静态内部类,匿名内部类)
- 微信开发关闭当前网页跳到公众号主界面
- 判断是否在微信浏览器
- 微信开发学习—柳峰老师博客之目录总结
- 微信可以为整个业界提供很好的一个通信开放平台
- 靠微信的深度和专业化服务冲出一线品牌
- 微信公众号支付Java DEMO
- 微信支付
- C#一个简单的定时小程序实现代码
- java 实现微信搜索附近人功能
- 微信退款API响应:“卖家信息校验不一致,请核实后再试”
- 【转】java初学基础班经典练手小程序300例(飘叶寻梦整理)
- 签名signature算法没错导致invalid signature和permission denied原因
- 用微信浏览器调整字体后页面错位解决方案