1.MainActivity 类
public class MainActivity extends AppCompatActivity {
private List<Fragment> list_fragment = new ArrayList<>(); //定义要装fragment的列表
private TabLayout tab;
private ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
inData();
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tab_main);
vp = (ViewPager) findViewById(R.id.vp_main);
}
private void inData() {
List<String> listTitle = TetleBean.getTitle();
for (int i = 0; i <listTitle.size() ; i++) {
MyFragment myFragment=new MyFragment();
list_fragment.add(myFragment);
}
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(),list_fragment,listTitle);
vp.setAdapter(adapter);
tab.setupWithViewPager(vp);
}
}
MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_main"
android:layout_width="match_parent"
android:layout_height="55dp"
app:tabIndicatorColor="#f7c203"
app:tabSelectedTextColor="#f7fafa00"
app:tabTextColor="#dc0b0707"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
2.FragmentPagerAdapter适配器
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> titleList;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
3.Fragment类 public class MyFragment extends Fragment implements XListView.IXListViewListener {
private XListView mLv;
private List<CoimBean.DataBean.ComicsBean> list = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
boolean conn = ConnectionUtil.isConn(getContext());
if (!conn) {
ConnectionUtil.setNetworkMethod(getContext());
readJson();
} else {
getDataLoad();
}
}
private void initView() {
mLv = (XListView) getView().findViewById(R.id.xlv_frag);
mLv.setPullRefreshEnable(true);
mLv.setPullLoadEnable(true);
mLv.setXListViewListener(this);
}
@Override
public void onRefresh() {
getDataLoad();
stopLoad();
}
@Override
public void onLoadMore() {
getDataLoad();
stopLoad();
}
/**
* 请求网络数据
*/
public void getDataLoad() {
String url = "http://api.kkmh.com/v1/daily/comic_lists/0?" +
"since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3Vha" +
"Wthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9w" +
"ZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIl" +
"ZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24i" +
"OiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2V" +
"UYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdv" +
"cmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veC" +
"IsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcG" +
"nZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZS" +
"I6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIs" +
"IlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9u" +
"ZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRk" +
"YzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21" +
"lUGFnZSJ9";
RequestParams params = new RequestParams();
params.setUri(url);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(final String result) {
if (result != null) {
new Thread(new Runnable() {
@Override
public void run() {
boolean sdCard = isSdCard();
if ( sdCard){
writeData(result);
}
}
}).start();
Gson gson = new Gson();
CoimBean bean = gson.fromJson(result, CoimBean.class);
List<CoimBean.DataBean.ComicsBean> comics = bean.getData().getComics();
list.addAll(comics);
MyListAdapter adapter = new MyListAdapter(getContext(), list);
mLv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
//判断sdcard卡是否挂载
public boolean isSdCard() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
public void writeData(String str) {
try {
File file = new File(Environment.getExternalStorageDirectory(), "chej.txt");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(str.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
/**
* 停止
*/
public void stopLoad() {
mLv.stopRefresh();
mLv.stopLoadMore();
// 设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前系统时间
String nowTime = df.format(new Date(System.currentTimeMillis()));
// 释放时提示正在刷新时的当前时间
mLv.setRefreshTime(nowTime);
}
//读取sdcard文件数据
private void readJson() {
try {
File file = new File(Environment.getExternalStorageDirectory(),
"chej.txt");
FileInputStream is = new FileInputStream(file);
byte[] b = new byte[is.available()];
is.read(b);
String result = new String(b);
System.out.println("读取成功:" + result);
Gson gson=new Gson();
CoimBean bean = gson.fromJson(result, CoimBean.class);
List<CoimBean.DataBean.ComicsBean> comics = bean.getData().getComics();
list.addAll(comics);
MyListAdapter adapter = new MyListAdapter(getContext(), list);
mLv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
布局:
<?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">
<me.maxwin.view.XListView
android:id="@+id/xlv_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"></me.maxwin.view.XListView>
</LinearLayout>
//关于fragment的适配器和网络判断
//适配器
public class MyListAdapter extends BaseAdapter {
Context context;
List<CoimBean.DataBean.ComicsBean> mList;
public MyListAdapter(Context context, List<CoimBean.DataBean.ComicsBean> mList) {
this.context = context;
this.mList = mList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final CoimBean.DataBean.ComicsBean comicsBean = mList.get(position);
ViewHodel hodel = null;
convertView = null;
if (convertView == null) {
hodel = new ViewHodel();
convertView = View.inflate(context, R.layout.item_layout, null);
hodel.label_text = (TextView) convertView.findViewById(R.id.label_text);
hodel.title_load = (TextView) convertView.findViewById(R.id.title_load);
hodel.cover_image_url = (ImageView) convertView.findViewById(R.id.cover_image_url);
hodel.label_quanji = (TextView) convertView.findViewById(R.id.label_quanji);
convertView.setTag(hodel);
} else {
hodel = (ViewHodel) convertView.getTag();
}
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(20);
drawable.setColor(Color.parseColor(mList.get(position).getLabel_color()));
hodel.label_text.setBackground(drawable);
hodel.label_text.setText(comicsBean.getLabel_text());
hodel.title_load.setText(comicsBean.getTitle());
Glide.with(context).load(comicsBean.getCover_image_url()).into(hodel.cover_image_url);
/**点击频道管理自己做一个即可只不过要把参数传到频道管理去核心代码如下
Intent intent = getIntent();
ArrayList<String> s= intent.getStringArrayListExtra("arg");
for (int i = 0; i <10; i++) {
mUserList.add(s.get(i));
}
for (int i = 0; i <20; i++) {
mOtherList.add(s.get(i));
}
*/
hodel.label_quanji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "dianji", Toast.LENGTH_SHORT).show();
ArrayList<String> s=new ArrayList<String>();
for (int i = 0; i < mList.size(); i++) {
s.add(mList.get(i).getLabel_text());
}
Intent it = new Intent(context, PindaoActivity.class);
it.putStringArrayListExtra("arg",s);
context.startActivity(it);
}
});
return convertView;
}
class ViewHodel {
TextView label_text, title_load, label_quanji;
ImageView cover_image_url;
}
}
//适配器的布局:
<?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="60dp"
android:orientation="horizontal">
<TextView
android:id="@+id/label_text"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:background="#ec4b4b"
android:gravity="center"
android:text="恋爱"
android:textColor="#ffffff" />
<TextView
android:id="@+id/label_quanji"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="8dp"
android:drawableRight="@drawable/w1"
android:gravity="center"
android:text="全集" />
</LinearLayout>
<ImageView
android:id="@+id/cover_image_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title_load"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="match_parent"
android:textSize="20sp"
android:maxLines="2"
android:textColor="#867e7e"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="match_parent"
android:textSize="18sp"
android:gravity="center"
android:textColor="#867e7e"
android:paddingRight="15dp"
android:drawableLeft="@drawable/zan"
android:text="39万"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="18sp"
android:layout_weight="1.5"
android:gravity="center"
android:textColor="#867e7e"
android:drawableLeft="@drawable/xiaoxi"
android:text="5321"/>
</LinearLayout>
</LinearLayout>
//网络工具类
public class ConnectionUtil {
/*
* 判断网络连接是否已开
*true 已打开 false 未打开
* */
public static boolean isConn(Context context) {
boolean bisConnFlag = false;
ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if (network != null) {
bisConnFlag = conManager.getActiveNetworkInfo().isAvailable();
}
return bisConnFlag;
}
/*
* 打开设置网络界面
* */
public static void setNetworkMethod(final Context context){
//提示对话框
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent=null;
//判断手机系统的版本 即API大于10 就是3.0或以上版本
if(android.os.Build.VERSION.SDK_INT>10){
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
}
用到的Xlistview记得导入module让module和app关联
用到的xutils建一个继承application类,在清单中注册
//用到的依赖和权限:
compile 'com.android.support:design:25.3.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.github.bumptech.glide:glide:3.8.0'
compile 'org.xutils:xutils:3.5.0'
compile 'fm.jiecao:jiecaovideoplayer:4.8.3'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.youth.banner:banner:1.4.9'
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />