实例:
这是一个简单的主界面框架。主界面是
MainActivity
。其中
底部包含了“首页”、“消息”、“笔记”和“我的”四个按钮,分别指向四个Fragment 界面模块。已知该项目使用了 ViewPage+Fragment
方式构建,其中“首页”、“消息”、 “笔记”和“我的”都是 Fragment
重点:
本文主要是像讲ListView在Fragment中的使用。在我的界面中,这些一行一行的信息栏是通过ListView在"我的"Fragment界面中展示。
项目目录:
MyFragment页面编写:
在Fragment中,绑定视图与Activity的方法不同
在Activity中:
view = View.inflate(MainActivity.this,R.layout.itemlayout,null);
在Fragment中
通过这个inflater.inflate进行绑定
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
myList = view.findViewById(R.id.mylist);
MyAdapter myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
return view;
}
完整代码:
activity_main.xml布局
上面一个ViewPage 下面四个按钮
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<!-- fragment 换成布局 占据一段位置-->
<!-- <LinearLayout-->
<!-- android:id="@+id/fragment"-->
<!-- android:layout_width="300dp"-->
<!-- android:layout_height="300dp"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent"-->
<!-- android:orientation="vertical" />-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/wx_vp"
android:layout_width="match_parent"
android:layout_height="660dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消息"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintStart_toEndOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="笔记"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/button5"
app:layout_constraintStart_toEndOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>
首页、消息、笔记界面Fragment xml布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".ContactFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="info"
android:textSize="50dp"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".DongtaiFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="note"
android:textSize="50dp"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MessageFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="home"
android:textSize="50dp"/>
</FrameLayout>
“我的”界面布局
这里因为下面两行与上面两行差别比较大,所以上面的 8行用的是ListView,下面两行是单独写的界面。
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MyFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/one"
android:paddingLeft="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的信息"
android:background="#FDFCFB"
android:gravity="center"
android:textSize="30dp"
/>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#AA5858"/>
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="350dp"
android:divider="#D3C5C5"
android:dividerHeight="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="工作状态"
android:textSize="25dp"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:text="未设置"
android:paddingLeft="165dp"
android:textSize="25dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/two"
android:id="@+id/icon1"
/>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#AA5858"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="个人实名认证"
android:textSize="25dp"
android:paddingLeft="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/five"
android:paddingLeft="80dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未认证"
android:textSize="25dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/two"
/>
</LinearLayout>
</LinearLayout>
MyFragment.java
其中包含了ListView的Adapter,并对Adapter进行编写使它可以完整的显示信息。
package com.edu.nchu.lab3exam02;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Scanner;
/**
* A simple {@link Fragment} subclass.
* create an instance of this fragment.
*/
public class MyFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
ListView myList = null;
String[] lefts = {"头像","昵称","学号","电话","二维码名片","性别","生日","地区",};
String[] rights = {"大海","7895789","18874589856","男","1980/11/26","江西-南昌",};
int[] images = {R.drawable.three,R.drawable.four};
TextView text1;
TextView text2;
ImageView image1;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
myList = view.findViewById(R.id.mylist);
MyAdapter myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
return view;
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {//获取item的数量
return lefts.length;
}
@Override
public Object getItem(int i) {//i表示当前item的序号,序号从0开始编写,getIte用于获取某个item
return lefts[i];
}
@Override
public long getItemId(int i) {//获取选中的item的编号
return i;
}
@Override//用于配置listview要加载的内容,包括视图和数据
public View getView(int i, View view, ViewGroup viewGroup) {
//这种写法,每次调用都要生成一个新的View,那么当数据多了,那就要生成很多个view,
// 这样会造成资源浪费
if (i != 0 && i != 4) {
View view1 = getActivity().getLayoutInflater().inflate(R.layout.itemlayout1, null);
text1 = view1.findViewById(R.id.text1);
text2 = view1.findViewById(R.id.text2);
text1.setText(lefts[i]);
if (i > 0 && i < 4) {
text2.setText(rights[i - 1]);
} else if (i > 4 && i < 8) {
text2.setText(rights[i - 2]);
}
return view1;
}else if(i == 0){
System.out.println("0");
View view2 = getActivity().getLayoutInflater().inflate(R.layout.itemlayout2, null);
text1 = view2.findViewById(R.id.text1);
image1 = view2.findViewById(R.id.icon1);
text1.setText(lefts[i]);
image1.setBackgroundResource(images[0]);
return view2;
}else if(i == 4){
System.out.println("4");
View view3 = getActivity().getLayoutInflater().inflate(R.layout.itemlayout2, null);
text1 = view3.findViewById(R.id.text1);
image1 = view3.findViewById(R.id.icon1);
text1.setText(lefts[i]);
image1.setImageResource(images[1]);
return view3;
}
return view;
}
}
}
MyPageAdpater
package com.edu.nchu.lab3exam02;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class MyPageAdapter extends FragmentPagerAdapter {
//创建fragmentList集合
private List<Fragment> fragmentList;
//构造方法实现传值
public MyPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
//返回fragmentList对象
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//返回fragmentList对象个数
@Override
public int getCount() {
return fragmentList.size();
}
}
MainActivity
package com.edu.nchu.lab3exam02;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MessageFragment messageFragment=new MessageFragment();
DongtaiFragment dongtaiFragment=new DongtaiFragment();
ContactFragment contactFragment=new ContactFragment();
MyFragment myFragment = new MyFragment();
ViewPager viewPager;
List<Fragment> fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=findViewById(R.id.button2);
Button button2=findViewById(R.id.button3);
Button button3=findViewById(R.id.button4);
Button button4=findViewById(R.id.button5);
viewPager = findViewById(R.id.wx_vp);
fragmentList.add(messageFragment);
fragmentList.add(dongtaiFragment);
fragmentList.add(contactFragment);
fragmentList.add(myFragment);
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragmentList);
//绑定适配器
viewPager.setAdapter(adapter);
button1.setOnClickListener((View.OnClickListener) this);
button2.setOnClickListener((View.OnClickListener) this);
button3.setOnClickListener((View.OnClickListener) this);
button4.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button2:
//通过setCurrentItem()方法实现点击跳转;
viewPager.setCurrentItem(0);
break;
case R.id.button3:
viewPager.setCurrentItem(1);
break;
case R.id.button4:
viewPager.setCurrentItem(2);
break;
case R.id.button5:
viewPager.setCurrentItem(3);
break;
default:
break;
}
}
}
另外三个的Fragment的java文件就不放了,就是自动生成的那个。