要实现的就是一个标签滑动页,第一个fragment分类是全部,后面3个是不同分类属性。以前做这种效果估计就是创建4个fragment然后依次添加进fragment数组,再根据滑动、切换的操作加载显示不同页。现在就只使用一个fragment,根据传入值加载不同页。
首先,创建fragment的界面和滑动activity的界面:fragment就是一个简单的列表
<?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"
android:background="#eeeff4">
<ListView
android:id="@+id/lv_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/ll_nodata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/infomation_no_data"/>
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="该分类暂无数据"
android:textColor="#979797"
android:layout_marginTop="20dp"
android:textSize="15dp"/>
</LinearLayout>
</RelativeLayout>
activity界面:
<?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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#fff"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_all"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全部"
android:textColor="@color/checked"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_YJ"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_YJ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已接"
android:textColor="@color/normal"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_JXZ"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_JXZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进行中"
android:textColor="@color/normal"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_YWC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_YWC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已完成"
android:textColor="@color/normal"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="2dp"
android:background="@color/checked" />
<android.support.v4.view.ViewPager
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" />
</LinearLayout>
新建FragBase,继承于Fragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragBase extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_base, container, false);
return view;
}
}
新建OrderActivity,继承自AppCompatActivity
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class OrderActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tvAll;
private LinearLayout llALL;
private LinearLayout llYJ;
private LinearLayout llJXZ;
private LinearLayout llYWC;
private TextView tvYJ;
private TextView tvJXZ;
private TextView tvYWC;
private View view1;
private ViewPager mViewPager;
private int mScreenWidth;
private int item_width;
private int endPosition;
private int beginPosition = 0;
private int currentFragmentIndex;
private FragmentPagerAdapter mAdapter;
private ArrayList<Fragment> mFragments;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
item_width = (int) ((mScreenWidth / 4.0 + 0.5f));
initView();
initFragment();
}
private void initView(){
tvAll = (TextView) findViewById(R.id.tv_all);
tvYJ = (TextView) findViewById(R.id.tv_YJ);
tvJXZ = (TextView) findViewById(R.id.tv_JXZ);
tvYWC = (TextView) findViewById(R.id.tv_YWC);
llALL = (LinearLayout) findViewById(R.id.ll_all);
llYJ = (LinearLayout) findViewById(R.id.ll_YJ);
llJXZ = (LinearLayout) findViewById(R.id.ll_JXZ);
llYWC = (LinearLayout) findViewById(R.id.ll_YWC);
view1 = findViewById(R.id.view1);
mViewPager = (ViewPager) findViewById(R.id.vp_view);
view1.getLayoutParams().width = item_width;
llALL.setOnClickListener(this);
llYJ.setOnClickListener(this);
llJXZ.setOnClickListener(this);
llYWC.setOnClickListener(this);
}
private void initFragment() {
mFragments = new ArrayList<Fragment>();
for (int i = 0; i < 4; i++) {
FragBase fragment = new FragBase();
fragment.setArguments(data);
mFragments.add(fragment);
}
initAdapter();
}
private void initAdapter() {
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int arg0) {
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
resetColor();
switch (position) {
case 0:
tvAll.setTextColor(getResources().getColor(R.color.checked));
break;
case 1:
tvYJ.setTextColor(getResources().getColor(R.color.checked));
break;
case 2:
tvJXZ.setTextColor(getResources().getColor(R.color.checked));
break;
case 3:
tvYWC.setTextColor(getResources().getColor(R.color.checked));
break;
default:
break;
}
currentFragmentIndex = position;
Animation animation = new TranslateAnimation(beginPosition, position * item_width, 0, 0);
beginPosition = position * item_width;
if (animation != null) {
animation.setFillAfter(true);
animation.setRepeatCount(1);
animation.setDuration(1000);
view1.startAnimation(animation);
}
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
if (currentFragmentIndex == position) {
endPosition = item_width * currentFragmentIndex +
(int) (item_width * positionOffset);
}
if (currentFragmentIndex == position + 1) {
endPosition = item_width * currentFragmentIndex -
(int) (item_width * (1 - positionOffset));
}
Animation mAnimation = new TranslateAnimation(beginPosition, endPosition, 0, 0);
mAnimation.setFillAfter(true);
mAnimation.setDuration(1000);
view1.startAnimation(mAnimation);
beginPosition = endPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void resetColor() {
tvAll.setTextColor(getResources().getColor(R.color.normal));
tvYJ.setTextColor(getResources().getColor(R.color.normal));
tvJXZ.setTextColor(getResources().getColor(R.color.normal));
tvYWC.setTextColor(getResources().getColor(R.color.normal));
}
@Override
public void onClick(View v) {
resetColor();
switch (v.getId()) {
case R.id.ll_all:
tvAll.setTextColor(getResources().getColor(R.color.checked));
mViewPager.setCurrentItem(0);
currentFragmentIndex = 0;
break;
case R.id.ll_YJ:
tvYJ.setTextColor(getResources().getColor(R.color.checked));
mViewPager.setCurrentItem(1);
currentFragmentIndex = 1;
break;
case R.id.ll_JXZ:
tvJXZ.setTextColor(getResources().getColor(R.color.checked));
mViewPager.setCurrentItem(2);
currentFragmentIndex = 2;
break;
case R.id.ll_YWC:
tvYWC.setTextColor(getResources().getColor(R.color.checked));
mViewPager.setCurrentItem(3);
currentFragmentIndex = 3;
break;
default:
break;
}
}
}
不同fragment的数据集合
private List<OrderItem> listAll = new ArrayList<>();
private List<OrderItem> listYJ = new ArrayList<>();
private List<OrderItem> listJXZ = new ArrayList<>();
private List<OrderItem> listYWC = new ArrayList<>();
OrderItem关联Parcelable实现对象数组值传递
import android.os.Parcel;
import android.os.Parcelable;
public class OrderItem implements Parcelable{
private String title;
private String content;
private String status;
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(content);
dest.writeString(status);
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<OrderItem> CREATOR = new Creator<OrderItem>() {
@Override
public OrderItem createFromParcel(Parcel in) {
OrderItem item=new OrderItem();
item.title=in.readString();
item.content=in.readString();
item.status=in.readString();
return item;
}
@Override
public OrderItem[] newArray(int size) {
return new OrderItem[size];
}
};
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
得到列表数据后进行数据分类
if (!listAll.isEmpty()) {
listAll.clear();
}
String flag = method.data().getString("flag");
String msg = method.data().getString("message");
if (flag.equals("1")) {
JSONArray mArray = method.data().getJSONArray("data");
for (int i = 0; i < mArray.size(); i++) {
JSONObject repairOrder = (JSONObject) mArray.get(i);
OrderItem orderItem = JSON.toJavaObject(repairOrder, OrderItem.class);
listAll.add(orderItem);
switch (orderItem.getStatus()) {
case "0":
listYJ.add(orderItem);
break;
case "1":
listJXZ.add(orderItem);
break;
case "2":
listYWC.add(orderItem);
break;
default:
break;
}
}
} else {
toast(msg);
}
在初始化的时候,将数组传过去
private void initFragment() {
mFragments = new ArrayList<Fragment>();
for (int i = 0; i < 4; i++) {
Bundle data = new Bundle();
switch (i) {
case 0:
data.putParcelableArrayList("list", (ArrayList<OrderItem>) listAll);
break;
case 1:
data.putParcelableArrayList("list", (ArrayList<OrderItem>) listYJ);
break;
case 2:
data.putParcelableArrayList("list", (ArrayList<OrderItem>) listJXZ);
break;
case 3:
data.putParcelableArrayList("list", (ArrayList<OrderItem>) listYWC);
break;
}
FragBase fragment = new FragBase();
fragment.setArguments(data);
mFragments.add(fragment);
}
}
FragBase接收数据,填充数据,适配器自行设置,不做展示,完整代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class FragBase extends Fragment {
private View view;
private LinearLayout noData;
private ListView orderListView;
private List<OrderItem> listOrder = new ArrayList<>();
private OrderAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_base, container, false);
initView();
return view;
}
private void initView(){
Bundle args = getArguments();
listOrder=args.getParcelableArrayList("list");
noData= (LinearLayout) view.findViewById(R.id.ll_nodata);
orderListView=(ListView) view.findViewById(R.id.lv_order);
if (listOrder.isEmpty()) {
orderListView.setVisibility(View.GONE);
noData.setVisibility(View.VISIBLE);
} else {
orderListView.setVisibility(View.VISIBLE);
noData.setVisibility(View.GONE);
}
adapter=new OrderAdapter(getActivity());
adapter.setListData(listOrder);
orderListView.setAdapter(adapter);
}
}