布局:
<android.support.design.widget.TabLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed"
app:tabTextColor="@color/colorPrimary"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tl"
></android.support.v4.view.ViewPager>
Main:
package wld.bawei.com.myjindong;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
import wld.bawei.com.myjindong.Bean.MyDd;
import wld.bawei.com.myjindong.MyDownDd.MyDdPresenter;
import wld.bawei.com.myjindong.MyDownDd.MyDdView;
public class MainMyDd extends AppCompatActivity{
private TabLayout tl;
private ViewPager vp;
private List<String> datas = new ArrayList<String>();
private String[] status = new String[]{"0","1","2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_my_dd);
tl = (TabLayout) findViewById(R.id.tl);
vp = (ViewPager) findViewById(R.id.vp);
datas.add("待支付");
datas.add("已支付");
datas.add("已取消");
vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
//进行关联
tl.setupWithViewPager(vp);
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
//带参的构造方法
@Override
public int getCount() {
return datas.size();
}
//返回选项卡的文本 ,,,添加选项卡
@Override
public CharSequence getPageTitle(int position) {
return datas.get(position);
}
@Override
public Fragment getItem(int position) {
//创建fragment对象并返回
Bundle bundle = new Bundle();
bundle.putString("url", status[position]);
//实例化Fragment
AllFragment allFragment = new AllFragment();
allFragment.setArguments(bundle);
return allFragment;
}
}
}
Fragment
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = View.inflate(getActivity(), R.layout.allfragment_layout,null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//获取Activity传来的数据
Bundle bundle = getArguments();
urlTitle = bundle.getString("url").toString();
}