=《侧滑与viewpager上方导航页》=
《布局》
=《Mainactivity》=
public class MainActivity extends AppCompatActivity {
private DrawerLayout draw;
private ViewPager pager;
private TabLayout tab;
private ImageView image;
private ListView list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
draw = findViewById(R.id.draw);
pager = findViewById(R.id.pager);
tab = findViewById(R.id.tab);
image = findViewById(R.id.image);
list_view = findViewById(R.id.list_view);
draw.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
Toast.makeText(MainActivity.this, "侧滑开启", Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerClosed(@NonNull View view) {
Toast.makeText(MainActivity.this, "侧滑关闭", Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
//页面数据
ArrayList list1 = new ArrayList<>();
list1.add(new Frag01());
list1.add(new Frag02());
list1.add(new Frag03());
list1.add(new Frag04());
list1.add(new Frag05());
list1.add(new Frag06());
//tab文字
String str[] = {"关注", "热点", "科技", "美女", "足球", "NBA"};
//适配器
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), list1, str);
pager.setAdapter(adapter);
//绑定viewpager
tab.setupWithViewPager(pager);
//===============
//加载侧滑圆角图片
Glide.with(MainActivity.this).load("http://t2.hddhhn.com/uploads/tu/201707/200/9.jpg").apply(RequestOptions.bitmapTransform(new RoundedCorners(280))).into(image);
//加载侧滑listview数据
final ArrayList list = new ArrayList<>();
list.add("我的");
list.add("更多");
//设置侧滑listview适配器
list_view.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, list));
//侧滑点击吐司
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_LONG).show();
}
});
}
}
=《HttpUtil》===
public class HttpUtil {
public static String getData(String str){
//封装加载网络数据
try {
URL url=new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置超时时间
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode()==200){
InputStream stream=connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder=new StringBuilder();
String s="";
while((s=reader.readLine())!=null){
builder.append(s);
}
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//创建接口
public interface TaskBack{
void getBack(String s);
}
//封装AsyncTask
public static void getTask(String url, final TaskBack taskBack){
new AsyncTask(){
@Override
protected String doInBackground(String... strings) {
return HttpUtil.getData(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
taskBack.getBack(s);
}
}.execute(url);
}
}
=《MyPagerAdapter》=
public class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList list1;
private String str[];
public MyPagerAdapter(FragmentManager fm, ArrayList list1, String[] str) {
super(fm);
this.list1 = list1;
this.str = str;
}
@Override
public Fragment getItem(int i) {
return list1.get(i);
}
@Override
public int getCount() {
return list1.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return str[position];
}
}
=《Viewpager导航页面fragment代码》
public class Frag01 extends Fragment {
private XListView xlist;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag01, container, false);
xlist = view.findViewById(R.id.xlist);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}