美文网首页实用
Fragment与ViewPager

Fragment与ViewPager

作者: 鴻9527 | 来源:发表于2019-08-08 21:34 被阅读5次

MainActivity类布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/tabLayout"/>

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

Fragment布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentLayout"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

MyFragment类
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MyFragment extends Fragment {

private int imageResource;

public MyFragment(int imageResource) {
    this.imageResource = imageResource;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View linearLayout = getLayoutInflater().inflate(R.layout.fragment_layout,null);
    ImageView imageView =linearLayout.findViewById(R.id.imageView);
    imageView.setImageResource(imageResource);
    return linearLayout;
}

}

MainActivity类
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//图片数据源
private int[] images = {
        R.mipmap.pic1,
        R.mipmap.pic2,
        R.mipmap.pic3,
        R.mipmap.pic4,
        R.mipmap.pic5,
        R.mipmap.pic6,
        R.mipmap.pic7,
        R.mipmap.pic8,
        R.mipmap.pic9
};
//标题
String[] title = {
        "可爱1",
        "可爱2",
        "可爱3",
        "可爱4",
        "可爱5",
        "可爱6",
        "可爱7",
        "可爱8",
        "可爱9"
};

private List<MyFragment> list = new ArrayList<>();
private ViewPager viewPager;
private TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = findViewById(R.id.viewPager);
    tabLayout = findViewById(R.id.tabLayout);
    for (int image : images) {
        list.add(new MyFragment(image));
    }
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);
}


class MyAdapter extends FragmentPagerAdapter {


    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }
}

}

相关文章

网友评论

    本文标题:Fragment与ViewPager

    本文链接:https://www.haomeiwen.com/subject/yagfjctx.html