//最外层的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000"
/>
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="首页"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
/>
<RadioButton
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="电影"
android:button="@null"
android:layout_weight="1"
/>
</RadioGroup>
</LinearLayout>
<fragment
class="com.example.moni.fragment.AFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
/>
</android.support.v4.widget.DrawerLayout>
//侧拉框里面布局
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="所有收件箱"
android:textSize="20dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="26dp"
/>
//多列表布局一
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="范围内服务"
/>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_alignParentRight="true"
/>
//多列表布局二
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="分外无法"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
android:layout_marginLeft="120dp"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
android:layout_marginLeft="240dp"
/>
//第一个页面fragment布局
<?xml version="1.0" encoding="utf-8"?>//第二个页面fragment布局
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到频道管理"
/>
TableLaytout布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
xlistViewV布局
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<com.example.xlist.me.maxwin.view.XListView
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
//Main方法实现具体页面
package com.example.moni;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.moni.activity.BaseActivity;
import com.example.moni.fragment.BFragment;
import com.example.moni.fragment.CFragment;
public class MainActivity extends BaseActivity {
private DrawerLayout drawer;
private RadioGroup group;
private TextView t1,t2;
private ViewPager viewpager;
private ActionBarDrawerToggle toggle;
@Override
protected int getId() {
return R.layout.activity_main;
}
@Override
protected void initview() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
toggle = new ActionBarDrawerToggle(this,drawer,R.string.open,R.string.close);
toggle.syncState();
drawer.addDrawerListener(toggle);
}
@Override
protected void init() {
drawer = findViewById(R.id.drawer);
group = findViewById(R.id.group);
t1 = findViewById(R.id.t1);
t2 = findViewById(R.id.t2);
viewpager = findViewById(R.id.viewpager);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
BFragment bfragment = new BFragment();
return bfragment;
case 1:
CFragment cfragment = new CFragment();
return cfragment;
}
return null;
}
@Override
public int getCount() {
return 2;
}
});
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.btn1:
viewpager.setCurrentItem(0);
break;
case R.id.btn2:
viewpager.setCurrentItem(1);
break;
}
}
});
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
group.check(R.id.btn1);
t1.setBackgroundColor(Color.BLACK);
t2.setBackgroundColor(Color.WHITE);
break;
case 1:
group.check(R.id.btn2);
t2.setBackgroundColor(Color.BLACK);
t1.setBackgroundColor(Color.WHITE);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
//封装基类
package com.example.moni.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getId());
init();
initview();
}
protected abstract int getId();
protected abstract void initview();
protected abstract void init();
}
//实现Xlistview多条目adapter
package com.example.moni.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.moni.ImageConfs;
import com.example.moni.R;
import com.example.moni.bean.Person;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class Heyadapter extends BaseAdapter {
List<Person.DateBean> list;
Context context;
@Override
public int getItemViewType(int position) {
if(position % 3 == 0){
return 0;
}else{
return 1;
}
}
public void setDate(List<Person.DateBean> dateBeans){
list = dateBeans;
notifyDataSetChanged();
}
public void addDate(List<Person.DateBean> dateBeans){
if(dateBeans != null){
list = dateBeans ;
notifyDataSetChanged();
}
}
@Override
public int getViewTypeCount() {
return 2;
}
public Heyadapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<Person.DateBean> list){
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType(position) == 0){
Viewhloder vh;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.dfragment_text1,null);
vh = new Viewhloder(convertView);
}else{
vh = (Viewhloder) convertView.getTag();
}
Person.DateBean data = (Person.DateBean) getItem(position);
vh.BindDate(data);
return convertView;
}else{
Viewhloder1 vh;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.dfragment_text2,null);
vh = new Viewhloder1(convertView);
}else{
vh = (Viewhloder1) convertView.getTag();
}
Person.DateBean data = (Person.DateBean) getItem(position);
vh.BindDate(data);
return convertView;
}
}
class Viewhloder{
TextView t1;
ImageView image;
public Viewhloder(View view) {
t1 = view.findViewById(R.id.t1);
image = view.findViewById(R.id.image);
view.setTag(this);
}
public void BindDate(Person.DateBean data){
t1.setText(data.getTitle());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s(),image,ImageConfs.getpon());
}
}
class Viewhloder1{
TextView t1;
ImageView image1;
ImageView image2;
ImageView image3;
public Viewhloder1(View view) {
t1 = view.findViewById(R.id.t1);
image1 = view.findViewById(R.id.image1);
image2 = view.findViewById(R.id.image2);
image3 = view.findViewById(R.id.image3);
view.setTag(this);
}
public void BindDate(Person.DateBean data){
t1.setText(data.getTitle());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s(),image1,ImageConfs.getpon());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s02(),image2,ImageConfs.getpon());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s03(),image3,ImageConfs.getpon());
}
}
}
//实现TableLayoutadapter
package com.example.moni.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
public class Myadapter extends FragmentPagerAdapter {
List<Fragment> main;
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = main.get(i);
return fragment;
}
@Override
public int getCount() {
return main.size();
}
public void setfragments(ArrayList<Fragment> list) {
main = list;
}
}
//请求网络数据类
package com.example.moni.bean;
import java.util.List;
public class Person {
public String msg;
public int code;
public List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<DateBean> getData() {
return data;
}
public void setData(List<DateBean> data) {
this.data = data;
}
public class DateBean{
public String title;
public String thumbnail_pic_s;
public String thumbnail_pic_s02;
public String thumbnail_pic_s03;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
//Afragment实现侧拉框里面数据
package com.example.moni.fragment;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.moni.R;
import java.util.ArrayList;
public class AFragment extends Fragment {
private ListView lv;
ArrayList list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
list = new ArrayList<>();
lv = view.findViewById(R.id.lv);
for (int i = 0; i <10 ; i++) {
list.add("所有邮箱");
}
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Vhes vh;
if(convertView == null){
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.afragment_list_text,null);
vh = new Vhes();
vh.text = convertView.findViewById(R.id.text);
convertView.setTag(vh);
}else{
vh = (Vhes) convertView.getTag();
}
vh.text.setText(list.get(position));
return convertView;
}
});
return view;
}
class Vhes{
TextView text;
}
}
//Bfragment实现频道管理数据
package com.example.moni.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.example.moni.R;
import java.util.ArrayList;
import java.util.List;
public class BFragment extends Fragment {
private Button btn;
private List<ChannelBean> list;
private String jsonStr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
btn =view.findViewById(R.id.btn);
button();
return view;
}
private void button() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(list == null){
list = new ArrayList<>();
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("军事",true));
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),list);
}else if (jsonStr!=null){//当判断保存的字符串不为空的时候,直接加载已经有了的字符串
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),jsonStr);
}
}
});
}
}
//Cfragment实现TableLayout
package com.example.moni.fragment;
import android.os.Bundle;
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.moni.R;
import com.example.moni.adapter.Myadapter;
import java.util.ArrayList;
public class CFragment extends Fragment {
private TabLayout table;
private ViewPager viewpager;
private ArrayList<Fragment> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_c, container, false);
// Inflate the layout for this fragment
table = view.findViewById(R.id.table);
viewpager = view.findViewById(R.id.viewpager);
init();
return view;
}
private void init() {
list = new ArrayList<>();
list.add(new DFragment());
list.add(new EFragment());
Myadapter ma = new Myadapter(getChildFragmentManager());
ma.setfragments(list);
viewpager.setAdapter(ma);
table.setupWithViewPager(viewpager);
table.getTabAt(0).setText("娱乐");
table.getTabAt(1).setText("电影");
}
}
//Dfragment实现Xlistview多条目
package com.example.moni.fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.Person;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.moni.R;
import com.example.moni.adapter.Heyadapter;
import com.example.xlist.me.maxwin.view.XListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
public class DFragment extends Fragment {
private XListView xlv;
private Heyadapter ha;
private int page = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_d, container, false);
xlv = view.findViewById(R.id.xlv);
ha = new Heyadapter(getActivity());
// Inflate the layout for this fragment
xlv.setAdapter(ha);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page =1;
Loadehzj(page);
}
@Override
public void onLoadMore() {
Loadehzj(page);
}
});
return view;
}
private void stopdf() {
page++;
xlv.stopRefresh();
xlv.stopLoadMore();
}
private void Loadehzj(int page) {
final String path = "http://www.xieast.com/api/news/news.php?page=1";
if(!HaveNetWork()) {
Toast.makeText(getActivity(), "没有网络", Toast.LENGTH_SHORT).show();
}else{
String Urlwith = path +page;
new AsyncTask<String,String, List<com.example.moni.bean.Person.DateBean>>(){
@Override
protected List<com.example.moni.bean.Person.DateBean> doInBackground(String... strings) {
com.example.moni.bean.Person dian = null;
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("GET");
int request = httpURLConnection.getResponseCode();
if(request == 200){
String str = String2(httpURLConnection.getInputStream());
dian = new Gson().fromJson(str, com.example.moni.bean.Person.class);
}
} catch (Exception e) {
e.printStackTrace();
}
return dian == null ? null : dian.getData();
}
@Override
protected void onPostExecute(List<com.example.moni.bean.Person.DateBean> dateBean) {
if(dateBean == null){
Toast.makeText(getActivity(),"下拉数据失败",Toast.LENGTH_SHORT).show();
}
updateset(dateBean);
stopdf();
super.onPostExecute(dateBean);
}
}.execute(Urlwith);
}
}
//判断网络
public boolean HaveNetWork() {
ConnectivityManager con = (ConnectivityManager) getActivity().getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo act = con.getActiveNetworkInfo();
return act!=null && act.isAvailable();
}
private void updateset(List<com.example.moni.bean.Person.DateBean> dateBean) {
if(page == 1){
ha.setDate(dateBean);
Date date = new Date();
xlv.setRefreshTime(date.toLocaleString());
}else{
ha.addDate(dateBean);
}
}
private String String2(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String text = null;
while((text=bufferedReader.readLine())!=null){
stringBuilder.append(text);
}
bufferedReader.close();
return stringBuilder.toString();
}
}
//