Android Studio Viewpager 案例
滑动布局:Viewpager
activityViewpager
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.myapplicationmeituan.R;
import java.util.ArrayList;
import java.util.List;
public class activityViewpager extends AppCompatActivity implements View.OnClickListener {
private RadioGroup radioGroup;
private ViewPager viewPager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oderfour_list_view_pager);
initView();
}
private void initView() {
radioGroup = findViewById(R.id.rg);
viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(new adpterViewpager(getSupportFragmentManager(),setFragment()));
}
private List<Fragment> setFragment() {
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new MyOnefragment());
fragmentList.add(new MyFourfragment());
fragmentList.add(new MyThreefragment());
return fragmentList;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.Radio1:
showFragment(new MyOnefragment());
break;
case R.id.Radio2:
showFragment(new MyOnefragment());
break;
case R.id.Radio3:
showFragment(new MyOnefragment());
break;
}
}
/**
* @param fragment 显示
*/
private void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().show(fragment).commit();
}
/**
* 隐藏
*/
private void hideFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().hide(fragment).commit();
}
}
adpterViewpager
package Fragment;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class adpterViewpager extends FragmentPagerAdapter {
private List<Fragment>fragmentList;
public adpterViewpager(@NonNull FragmentManager fm,List<Fragment>fragmentList) {
super(fm);
this.fragmentList=fragmentList;//返回它的对象
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/aaaa"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#03A9F4" />
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignBottom="@id/aaaa"
android:background="@mipmap/jk1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:textSize="22dp"
android:textColor="#FBFBFB"
/>
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="170dp"
android:layout_marginTop="15dp"
android:text="联系人"
android:textColor="#FBFBFB"
android:textSize="24dp" />
<!-- </RelativeLayout>-->
<!--
<RelativeLayout
android:id="@+id/bobo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/aaaa">
<Button
android:id="@+id/bobo_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/oderfour_shape1"
android:text="好友" />
<Button
android:id="@+id/bobo_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/bobo_1"
android:background="@drawable/oderfour_shape1"
android:text="分组" />
<Button
android:id="@+id/bobo_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/bobo_2"
android:background="@drawable/oderfour_shape1"
android:text="群聊" />
<Button
android:id="@+id/bobo_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/bobo_3"
android:background="@drawable/oderfour_shape1"
android:text="设备" />
</RelativeLayout>
-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/rg"
android:layout_below="@id/aaaa" />
<!-- android:layout_above="@id/bbbb"-->
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/Radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:button="@null"
android:drawableTop="@mipmap/map"
android:gravity="center"
android:paddingTop="12dp"
android:text="消息"
android:textColor="@color/black" />
<View
android:id="@+id/d1"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000"/>
<RadioButton
android:id="@+id/Radio2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:button="@null"
android:drawableTop="@mipmap/my"
android:gravity="center"
android:paddingTop="12dp"
android:text="联系人"
android:textColor="@color/black" />
<View
android:id="@+id/d2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000"/>
<RadioButton
android:id="@+id/Radio3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/phone"
android:gravity="center"
android:button="@null"
android:paddingTop="12dp"
android:text="电话"
android:textColor="@color/black" />
<View
android:id="@+id/d3"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000"/>
<RadioButton
android:id="@+id/Radio4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/sousuo"
android:gravity="center"
android:button="@null"
android:paddingTop="12dp"
android:text="搜索"
android:textColor="@color/black" />
</RadioGroup>
</RelativeLayout>