import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.myapplication.R; import com.example.myapplication.adapter.historyFragmentAdapter; import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; /** * @author:created By ZhangHao * 时间:2019/4/24 18 * 邮箱:188660586@qq.com */ public class HistoryFragment extends Fragment { @BindView(R.id.top_tabViewPager) ViewPager top_tabViewPager; @BindView(R.id.top_tab) TabLayout top_tab; private ArrayList<String> titles; private historyFragmentAdapter adapter; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initData(); } @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.history_fragment, container, false); ButterKnife.bind(this, view); adapter = new historyFragmentAdapter(getChildFragmentManager()); top_tabViewPager.setAdapter(adapter); top_tab.setupWithViewPager(top_tabViewPager); //更新适配器数据 adapter.setTitles(titles); return view; } private void initData() { titles = new ArrayList<>(); titles.add("待出行的订单"); titles.add("已出行的订单"); } }