Android安卓欢迎页和viewpager和Fragment的结合
2017-11-20
展开全部
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:id="@+id/rb1"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/icon"
android:checked="true"
android:button="@null" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/icon"
android:button="@null" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/icon"
android:button="@null" />
</RadioGroup>
</RelativeLayout>
********************************************************************
package com.example.myapplication;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
List<View> list=new ArrayList<>();
RadioGroup rg;
RadioButton[] rb;
PageListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg=(RadioGroup)findViewById(R.id.rg);
//实例化RadioButton的集合
rb=new RadioButton[rg.getChildCount()];
for (int i=0;i<rg.getChildCount();i++){
//从RadioGroup中循环获取RadioButton,并放进数组
rb[i]=(RadioButton) rg.getChildAt(i);
}
vp=(ViewPager)findViewById(R.id.vp);
list.add(View.inflate(this,R.layout.view1,null));
list.add(View.inflate(this,R.layout.view2,null));
list.add(View.inflate(this,R.layout.view3,null));
MyAdapter adapter=new MyAdapter(list);
vp.setAdapter(adapter);
//单选按钮点击事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int i=0;i<rg.getChildCount();i++){
//循环,如果当前RadioButton的Id和checkedId相等,就取出索引i
if(rb[i].getId()==checkedId){
//ViewPager设置当前索引项
vp.setCurrentItem(i);
break;
}
}
}
});
//vp.setOnPageChangeListener();
//添加页面改变事件
listener=new PageListener();
vp.addOnPageChangeListener(listener);
}
public void click(View view) {
//界面跳转
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
class PageListener implements ViewPager.OnPageChangeListener{
//页面滑动过程中执行的方法
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//页面滑动完后执行的方法
@Override
public void onPageSelected(int position) {
//页面滑动时,把当前页面对应的索引位置的RadioButton设置为选中状态
rb[position].setChecked(true);
}
//滑动过程中状态改变的方法
@Override
public void onPageScrollStateChanged(int state) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//移除指定的监听事件
vp.removeOnPageChangeListener(listener);
//清空所有的页面改变事件
vp.clearOnPageChangeListeners();
}
}
*************************************************************************
package com.example.myapplication;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by 猴嫂 on 2017/11/18.
*/
public class MyAdapter extends PagerAdapter {
List<View> list;
public MyAdapter(List<View> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
*****************************************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bb"
>
</LinearLayout>
// 如上欢迎页界面 最后一个加按钮跳转
**********************************************************************
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/aa"
android:button="@null"
android:gravity="center"
android:checked="true"
android:text="消息" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/aa"
android:button="@null"
android:gravity="center"
android:text="联系人" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/aa"
android:button="@null"
android:gravity="center"
android:text="动态" />
</RadioGroup>
</RelativeLayout>
网易云信
2023-12-06 广告
2023-12-06 广告
IM UI支持多种终端,包括但不限于:1. PC端:用户可以在个人电脑上使用IM UI进行聊天、文件传输、语音通话等操作。2. 移动端:用户可以在智能手机、平板电脑上使用IM UI,享受与PC端相同的聊天、文件传输、语音通话等服务。3. 浏...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询