package com.andy.multifragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by Administrator on 2016/6/24.
*/
public class SampleFragment extends Fragment {
public static final String KEY_RANGE = "KEY_RANGES";
private int mRange;
LinearLayout mLayout;
private List<Integer> mStudentId = new ArrayList<>();
private List<Integer> mStudentScore = new ArrayList<>();
private String[][] studentName = new String[][]{
{"张一", "张二", "张三", "张四"},
{"李一", "李二", "李三", "李四"},
{"卓一", "卓二", "卓三", "卓四"},
{"王一", "王二", "王三", "王四"}
};
private int[] imageIds = new int[]
{R.drawable.tiger, R.drawable.nongyu
, R.drawable.qingzhao, R.drawable.libai};
public static SampleFragment getNewInstance(Bundle bundle) {
SampleFragment fragment = new SampleFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRange = getArguments().getInt(KEY_RANGE, 1);
initData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
mLayout = (LinearLayout) rootView.findViewById(R.id.linearLayoutRoot);
initView(inflater);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment, null);
}
public void initData() {
Random rand = new Random();
int randId = rand.nextInt(10000) + 10000;
for (int i = 0; i <= 3; i++) {
mStudentId.add(Integer.valueOf(randId + i));
int score = rand.nextInt(50)+50;
mStudentScore.add(Integer.valueOf(score));
}
}
public void initView(LayoutInflater inflater) {
View view = null;
for (int i = -1; i <= 3; i++) {
if (i == -1) {
view = inflater.inflate(R.layout.item_student_title, null);
} else {
view = inflater.inflate(R.layout.item_student_content, null);
ImageView header = (ImageView)view.findViewById(R.id.student_image);
TextView id = (TextView)view.findViewById(R.id.student_id);
TextView name = (TextView)view.findViewById(R.id.student_name);
TextView score = (TextView)view.findViewById(R.id.student_score);
header.setImageResource(imageIds[i]);
id.setText(String.valueOf(mStudentId.get(i)));
score.setText(String.valueOf(mStudentScore.get(i).intValue()));
switch (mRange){
case 1:
name.setText(studentName[i][0]);
break;
case 2:
name.setText(studentName[i][1]);
break;
case 3:
name.setText(studentName[i][2]);
break;
case 4:
name.setText(studentName[i][3]);
break;
}
}
if (i % 2 == 0) {
view.setBackgroundResource(R.color.achi_conent_bg);
}
Log.d("andylog ", "initView" + String.valueOf(i));
mLayout.addView(view);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
学习android两周多了,这几天在根据产品的要求改页面,刚好是左右滑动切换界面。于是把这部分单独拿出来搞了个Demo。希望对跟我一样初学android的朋友有所帮助。Demo主要用到的知识点包括:TabLayout, ViewPager,左右滑动切换tab界面,点击选项卡也可切换界面,一个activity包含多个fragment。tab页界面布局一致,但加载数据不同。
资源推荐
资源详情
资源评论
收起资源包目录
MultiFragment.zip (58个子文件)
MultiFragment
MultiFragment.iml 944B
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
.gradle
2.10
taskArtifacts
cache.properties.lock 17B
cache.properties 31B
app
proguard-rules.pro 662B
src
test
java
com
andy
multifragment
ExampleUnitTest.java 329B
androidTest
java
com
andy
multifragment
ApplicationTest.java 365B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable-xhdpi
bg.9.png 84B
drawable
tiger.gif 2KB
nongyu.gif 2KB
qingzhao.gif 2KB
libai.gif 2KB
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 753B
strings.xml 79B
styles.xml 2KB
dimens.xml 757B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
bg.png 84B
layout
activity_main.xml 1KB
fragment.xml 339B
item_student_content.xml 3KB
item_student_title.xml 2KB
values-w820dp
dimens.xml 364B
java
com
andy
multifragment
MainActivity.java 2KB
SampleFragment.java 4KB
AndroidManifest.xml 681B
app.iml 9KB
.gitignore 8B
build
build.gradle 698B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
.idea
gradle.xml 739B
runConfigurations.xml 564B
misc.xml 3KB
.name 13B
copyright
profiles_settings.xml 76B
modules.xml 363B
encodings.xml 159B
compiler.xml 686B
workspace.xml 167KB
libraries
android_android_23.xml 241B
support_v4_23_0_1.xml 903B
appcompat_v7_23_0_1.xml 761B
hamcrest_core_1_3.xml 526B
junit_4_12.xml 477B
support_annotations_23_0_1.xml 495B
design_23_0_1.xml 578B
.gitignore 105B
local.properties 428B
settings.gradle 16B
build
intermediates
dex-cache
cache.xml 64B
generated
build.gradle 521B
共 58 条
- 1
资源评论
- sleepdragon2017-03-16学习一下。
- qq_337145512016-08-25studio?无语
- mousetian20012016-12-07studio 不会用,只能漂过
- ahe1682017-09-30学习一下。
wxq888
- 粉丝: 61
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功