核心知识点:fragment的切换
1、实例化Fragment对象
2、 获取FragmentMenage实例
3、获取FragmentTransaction实例
4、添加一个Fragment
5、提交
6、切换Fragment
界面:
布局 activity_manager
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MangerActivity">
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/newlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
布局 fregment_student
<?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"
tools:context=".fragment.Fragment_Student">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/Lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
布局 list_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/titles"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="@string/t_account"
android:textSize="22sp" />
<TextView
android:id="@+id/detail"
android:layout_width="150sp"
android:layout_height="16dp"
android:layout_margin="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:lines="3"
android:text="@string/name"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<Button
android:id="@+id/idelete"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/delete" />
<Button
android:id="@+id/iupadta"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/updata" />
<Button
android:id="@+id/iquary"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/quary" />
</LinearLayout>
JAVA 代码:
MangerActivity
package com.example.handclass;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.handclass.dateBase.DatabaseOperate;
import com.example.handclass.entity.Administrator;
import com.example.handclass.entity.Course;
import com.example.handclass.entity.Student;
import com.example.handclass.entity.Teacher;
import com.example.handclass.fragment.Fragment_Course;
import com.example.handclass.fragment.Fragment_Student;
import com.example.handclass.fragment.Fragment_Teacher;
import java.util.ArrayList;
import java.util.List;
public class MangerActivity extends AppCompatActivity {
private Button bt_add;
private ListView listview;
private DatabaseOperate dateOperator = new DatabaseOperate(this);
private Student student;
private Teacher teacher;
String string=null;
int type=1;
private int removePosition;
// 实例化Fragment对象
Fragment_Student fragmentStudent = new Fragment_Student();
Fragment_Teacher fragmentTeacher = new Fragment_Teacher();
Fragment_Course fragmentCourse=new Fragment_Course();
FragmentManager fragmentManager;
FragmentTransaction beginTransaction;
private ArrayList<Student> students;
private ArrayList<Teacher> teachers;
public ArrayList<Student> getStudents() {
return students;
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
beginTransaction=fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_home:
bt_add.setText("添加学生");type=1;
fragment = fragmentStudent;
beginTransaction.replace(R.id.newlist, fragment);
beginTransaction.commit();
return true;
case R.id.navigation_dashboard:
bt_add.setText("添加老师");type=2;
fragment = fragmentTeacher;
beginTransaction.replace(R.id.newlist, fragment);
beginTransaction.commit();
return true;
case R.id.navigation_notifications:
bt_add.setText("添加课程");type=3;
fragment = fragmentCourse;
beginTransaction.replace(R.id.newlist, fragment);
beginTransaction.commit();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manger);
Intent intent = getIntent();
bt_add=(Button)findViewById(R.id.add);
// //获取FragmentMenage实例
fragmentManager = getSupportFragmentManager();
// //获取FragmentTransaction实例
beginTransaction = fragmentManager.beginTransaction();
// //添加一个Fragment
beginTransaction.add(R.id.newlist, fragmentStudent);
// //提交
beginTransaction.commit();
//切换Fragment
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bt_add.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
add();
}
});
}
public void add()
{
//添加教师和学生是同一个layout
if (type==1||type==2)
{
Intent intent=new Intent(MangerActivity.this, MainActivity.class);
intent.putExtra("type",type);
startActivity(intent);
}
else {
Intent intent=new Intent(MangerActivity.this, Addcourse.class);
intent.putExtra("type",type);
startActivity(intent);
}
}
}
java代码:Fragment_Student
package com.example.handclass.fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.handclass.MainActivity;
import com.example.handclass.MangerActivity;
import com.example.handclass.R;
import com.example.handclass.ShowInfoActivity;
import com.example.handclass.StudentClassActivity;
import com.example.handclass.dateBase.DatabaseOperate;
import com.example.handclass.entity.Student;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment_Student extends Fragment {
private View view;
private ListView listview;
private ArrayList<Student> students;
private int removePosition;
String string=null;
private Student student;
public Adapter adapter;
public MangerActivity activity;
private DatabaseOperate dateOperator;
public Fragment_Student() {
// Required empty public constructor
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
inin();
}
public void inin()
{
//初始化列表信息
activity=(MangerActivity) getActivity();
dateOperator=new DatabaseOperate((MangerActivity)getActivity());
String sql="select * from student";
students=dateOperator.queryStudentData(sql);
if(view!=null)
{
listview=(ListView)view.findViewById(R.id.Lv);
if (students!=null)
{
adapter=new Adapter(activity,R.layout.list_item,students);
listview.setAdapter(adapter);
}
}
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
string=students.get(position).getID();
student=students.get(position);
removePosition=position;
//传递参数显示学生信息
Intent intent=new Intent(activity, ShowInfoActivity.class);
intent.putExtra("type",1);
intent.putExtra("account",string);
Toast.makeText(activity,string,Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//解析布局
view= inflater.inflate(R.layout.fragment_student, container, false);
return view;
}
//适配器
public class Adapter extends ArrayAdapter {
public ArrayList<Student>students;
public Context context;
public Adapter( Context context, int resource, List objects) {
super(context, resource, objects);
this.context=context;
students= (ArrayList<Student>) objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView=View.inflate(getActivity(),R.layout.list_item,null);
TextView title = (TextView) convertView.findViewById(R.id.titles);
TextView detail = (TextView) convertView.findViewById(R.id.detail);
Button idelete=(Button)convertView.findViewById(R.id.idelete);
Button iupdata=(Button)convertView.findViewById(R.id.iupadta);
Button iquary=(Button)convertView.findViewById(R.id.iquary);
iquary.setText("班级");
final Student student=students.get(position);
title.setText(student.getID());
detail.setText(student.getName());
//删除信息
idelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(activity).setTitle("删除").setMessage("是否删除信息")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
dateOperator.deleteData(student.getID(),1);
students.remove(student);
Toast.makeText(activity,"删除成功",Toast.LENGTH_SHORT).show();
notifyDataSetChanged();//更新
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
});
//修改个人信息
iupdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(activity, MainActivity.class);
intent.putExtra("account",student.getID());
intent.putExtra("type",3);
Toast.makeText(activity,string,Toast.LENGTH_SHORT).show();
startActivity(intent);
notifyDataSetChanged();
}
});
//修改班级
iquary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(activity, StudentClassActivity.class);
intent.putExtra("account",student.getID());
Toast.makeText(activity,string,Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
return convertView;//自处返回填充的Viwe对象
}
}
}