最近项目需要写一个订单管理的APP,原来使用ViewPager+ListView实现发现代码代码冗余 运行缓慢,所以打算使用Fragmengt+ListView来再实现一次。Fragment能把Activity拆分成很多模块,需要哪个的时候调用就可以了,比起原来的不知道方便多少。好了,废话不多说,下面就开始实现Fragment:
package www.imooc.com.dome;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import www.imooc.com.dome.MyFragment.AlreadyFragment;
import www.imooc.com.dome.MyFragment.CompletedFragment;
import www.imooc.com.dome.MyFragment.ReceivceFragment;
import www.imooc.com.dome.MyFragment.NewFragment;
import www.imooc.com.dome.orderfrom.Login;
public class MainActivity extends Activity implements OnClickListener {
private AlreadyFragment alreadyFragment;
private CompletedFragment completedFragment;
private NewFragment newFragment;
private ReceivceFragment receivceFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题
setContentView(R.layout.activity_main);
init();
new Login().login();
// 设置默认的Fragment
setDefaultFragment();
}
private void init() {
// 初始化控件和声明事件
Button new_order = (Button) findViewById(R.id.new_order);
Button already_assigned = (Button) findViewById(R.id.already_assigned);
Button received_order = (Button) findViewById(R.id.received_order);
Button completed = (Button) findViewById(R.id.completed);
new_order.setOnClickListener(this);
already_assigned.setOnClickListener(this);
received_order.setOnClickListener(this);
completed.setOnClickListener(this);
}
private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
newFragment = new NewFragment();
transaction.replace(R.id.id_content, newFragment);
transaction.commit();
}
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.new_order:
if (newFragment == null) {
newFragment = new NewFragment();
}
// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.id_content, newFragment);
break;
case R.id.already_assigned:
if (alreadyFragment == null) {
alreadyFragment = new AlreadyFragment();
}
transaction.replace(R.id.id_content, alreadyFragment);
break;
case R.id.received_order:
if (receivceFragment == null) {
receivceFragment = new ReceivceFragment();
}
transaction.replace(R.id.id_content, receivceFragment);
break;
case R.id.completed:
if (completedFragment == null) {
completedFragment = new CompletedFragment();
}
transaction.replace(R.id.id_content, completedFragment);
break;
}
// transaction.addToBackStack();
// 事务提交
transaction.commit();
}
}
以上是MainActivity.java的代码,主要就是定义出Fragment类,绑定控件,设置监听等操作,值得一提的就是在监听里替换Fragment用的是replace,是移除remove和加载add的结合体。
<?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">
<fragment
android:id="@+id/id_fragment_title"
android:name="www.imooc.com.dome.MyFragment.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp" />
<include
android:id="@+id/id_ly_bottombar"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/bottombar" />
<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title" />
</RelativeLayout>
activitymain的布局文件,中间是个FrameLayout用来替换不同的fragment。底部的按钮布局我就不贴了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
每个Fragment里面都是一个listview,这里我就贴出来一个 其他三个都类似的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单号:"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="haoma"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="55dp">
<TextView
android:id="@+id/user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/laiyuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="40dp"
android:text="laiyuan"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="88dp"
android:layout_marginRight="88dp"
android:paddingLeft="20dp"
android:text="time"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignBaseline="@+id/laiyuan"
android:layout_alignBottom="@+id/laiyuan"
android:layout_toLeftOf="@+id/laiyuan"
android:layout_toStartOf="@+id/laiyuan" />
</RelativeLayout>
</LinearLayout>
这里是listview的布局文件,我这里用的是SimpleAdapter做适配。
下面是Fragment.java 的文件:
public class NewFragment extends Fragment implements AdapterView.OnItemClickListener {
public ListView listView;
public Context context = MyApplication.getContext();
/**
* 可能用到的三个东西
* 1、 content
* 2、 getActivity()
* 3、 view
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_neworder, container, false);
listView = (ListView) view.findViewById(R.id.lv1);
SimpleAdapter adapte = new SimpleAdapter(context, new Getinfo().getData(res1, res11),
R.layout.item_view,
new String[]{"id", "name", "createTime", "laiyuan"},
new int[]{R.id.number, R.id.user_id, R.id.time, R.id.laiyuan});
listView.setAdapter(adapte);
listView.setOnItemClickListener(this);
return view;
}
SimpleAdapter 里的第二个参数是数据List,根据不同情况返回不同的List集合,最后把当前页面的view返回到main.java里在那边的监听器里实例化这个类,然后替换掉id是id_content的FrameLayout。在这里有个难点就是上下文比较难以获得,因为content是继承与activity的,所以在方法里不传进来就没有这个值。我们可以自己先定义一个MyApplication类来获得全局的context,这样不管在哪都可以获得content了。代码如下:
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
接下来就是自定义的AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.info, null);//获取自定义布局
builder.setView(layout);
builder.setIcon(R.mipmap.ic_launcher);//设置标题图标
builder.setTitle("订单详情");//设置标题内容
//builder.setMessage("");//显示自定义布局内容
TextView orderNumber = (TextView) layout.findViewById(R.id.orderNumber);
orderNumber.append(laiyuan);
Button button = (Button) layout.findViewById(R.id.cancelOrder);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "取消订单", Toast.LENGTH_SHORT).show();
}
});
Button button1 = (Button) layout.findViewById(R.id.exit);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "退出", Toast.LENGTH_SHORT).show();
}
});
final AlertDialog dlg = builder.create();
dlg.show();
“` 这里的控件全都是根据自己的布局来绑定id,然后设置textview的信息或者给button来设置监听。