import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
/**
* Activity 基类
*
* @author zhaoliang
* @version 1.0
* @create 2018/7/24
*/
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
/**
* 初始化
*/
private void init() {
setFullScreen();
setContentView(initLayout());
findView();
setOnClickLisener();
initData();
}
/**
* 设置点击事件
*/
protected abstract void setOnClickLisener();
/**
* 初始化数据
*/
protected abstract void initData();
/**
* 查找控件
*/
protected abstract void findView();
/**
* 设置全屏
*/
protected abstract void setFullScreen();
/**
* 填充布局
*
* @return
*/
protected abstract int initLayout();
/**
* 全屏
*/
protected void fullScreen() {
// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
import android.content.Intent;
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.view.View;
import android.widget.ImageView;
import com.bawei.channelmanager.activity.ChannelActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TabLayout tab;
private ViewPager vp;
private List<String> tabs;
private List<Fragment> fragments;
private ImageView intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = findViewById(R.id.tab);
vp = findViewById(R.id.vp);
intent = findViewById(R.id.intent);
intent.setOnClickListener(this);
tabs = new ArrayList<>();
tabs.add("首页");
tabs.add("附近");
tabs.add("发现");
tabs.add("订单");
tabs.add("我的");
tab.setTabMode(TabLayout.MODE_FIXED);
tab.addTab(tab.newTab().setText(tabs.get(0)));
tab.addTab(tab.newTab().setText(tabs.get(1)));
tab.addTab(tab.newTab().setText(tabs.get(2)));
tab.addTab(tab.newTab().setText(tabs.get(3)));
tab.addTab(tab.newTab().setText(tabs.get(4)));
fragments = new ArrayList<>();
fragments.add(new Fragment01());
fragments.add(new Fragment02());
fragments.add(new Fragment03());
fragments.add(new Fragment04());
fragments.add(new Fragment05());
MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
vp.setAdapter(adapter);
//将tablelayout与viewpage关联起来
tab.setupWithViewPager(vp);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.intent:
Intent intent = new Intent();
startActivity(new Intent(MainActivity.this,Main2Activity.class));
break;
}
}
public class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs.get(position);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_below="@id/vp"
android:layout_weight="1"
app:tabBackground="@color/colorPrimaryDark"
app:tabSelectedTextColor="#ff0033"
app:tabTextColor="#00ff96">
</android.support.design.widget.TabLayout>
<ImageView
android:id="@+id/intent"
android:layout_width="30dp"
android:layout_height="40dp"
android:src="@drawable/ic_arrow_forward_black_24dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:background="#f00"
android:layout_gravity="start"
android:layout_width="200dp"
android:layout_height="match_parent"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
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 android.widget.Toast;
import com.bawei.jane.mxlistview.view.XListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by lenovo on 2018/7/23.
*/
public class Fragment01 extends Fragment{
private List<Result.DataBean> data = new ArrayList<>();
private int pageIndex=1;//第一页的数据
private String urlpart="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
private int operType = 1;
private String url =urlpart + pageIndex;
private ViewPager vp;
private XListView xlv;
private List<String> picUrl;
private Handler handler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int index = vp.getCurrentItem();
index++;
vp.setCurrentItem(index);
sendEmptyMessageDelayed(0,2000);
}
};
private MyAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1, null);
picUrl = new ArrayList<>();
vp = view.findViewById(R.id.vp);
xlv = view.findViewById(R.id.xlv);
if (NetUtils.isConn(getActivity())){
initData();
MyTask myTask = new MyTask();
myTask.execute(url);
Toast.makeText(getActivity(),"有网络",Toast.LENGTH_SHORT).show();
vp.setAdapter(new MyPageAdapter(picUrl,getActivity()));
vp.setCurrentItem(picUrl.size()*10);
handler.sendEmptyMessageDelayed(0,2000);
}else {
NetUtils.openDg(getActivity());
}
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
pageIndex = 1;
url = urlpart + pageIndex;
operType=1;
MyTask myTask = new MyTask();
myTask.execute(url);
}
@Override
public void onLoadMore() {
pageIndex++;
url = urlpart + pageIndex;
operType=2;
Toast.makeText(getActivity(),"加载更多",Toast.LENGTH_SHORT).show();
MyTask task = new MyTask();
task.execute(url);
}
});
return view;
}
private void initData() {
picUrl.add("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=353970577,2929946804&fm=200&gp=0.jpg");
picUrl.add("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3382711420,562977377&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2267393344,4258926851&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1211379323,1253125292&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1132229679,4204511222&fm=27&gp=0.jpg");
}
class MyTask extends AsyncTask<String,Void,String> {
private MyAdapter adapter;
@Override
protected String doInBackground(String... strings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setReadTimeout(5000);
c.setConnectTimeout(5000);
c.setRequestMethod("GET");
if (c.getResponseCode() == 200) {
InputStream inputStream = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = reader.readLine()) != null) {
builder.append(str);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class);
if (operType == 1) {
data.clear();
}
data.addAll(result.getData());
setAdapter();
//关闭头尾布局
if (operType == 1) {
xlv.stopRefresh();
Date date = new Date(System.currentTimeMillis());//获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间
String refreShTime = sf.format(date);
xlv.setRefreshTime(refreShTime);
} else {
xlv.stopLoadMore();//关闭尾布局
}
}
}
public void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter(data, getActivity());
xlv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="300dp"></android.support.v4.view.ViewPager>
<com.bawei.jane.mxlistview.view.XListView
android:layout_below="@id/vp"
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by lenovo on 2018/7/23.
*/
public class Fragment02 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment2, null);
return view;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<com.bawei.channelmanager.ui.ChannelView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/channel_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
-------------------------------------------------------------------------------------------------------------------------------------------------------
开头动画:
import android.content.Intent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RelativeLayout;
/**
* 闪屏页面
*
* @author zhaoliang
* @version 1.0
* @create 2018/7/24
*/
public class SplashActivity extends BaseActivity implements Animation.AnimationListener {
// 渐变动画
private AlphaAnimation alphaAnimation;
// 跟布局
private RelativeLayout rlSplash;
@Override
protected void setOnClickLisener() {
}
@Override
protected void initData() {
alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
alphaAnimation.setAnimationListener(this);
rlSplash.startAnimation(alphaAnimation);
}
@Override
protected void findView() {
rlSplash = findViewById(R.id.rl_splash);
}
@Override
protected void setFullScreen() {
fullScreen();
}
@Override
protected int initLayout() {
return R.layout.activity_splash;
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
toMain();
}
/**
* 跳转到主界面
*/
private void toMain() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash" />
-------------------------------------------------------------------------------------------------------------------------------------------------------
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by lenovo on 2018/7/21.
*/
public class MyAdapter extends BaseAdapter {
private List<Result.DataBean> list;
private Context context;
private ImageView img;
private TextView title;
public MyAdapter(List<Result.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view==null){
view = View.inflate(context,R.layout.item,null );
holder = new ViewHolder();
holder.iocn = view.findViewById(R.id.img);
holder.t1 = view.findViewById(R.id.title);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.t1.setText(list.get(i).getTITLE());
ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(),holder.iocn);
return view;
}
private class ViewHolder{
ImageView iocn;
TextView t1;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:textSize="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/img"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by lenovo on 2018/7/21.
*/
public class MyPageAdapter extends PagerAdapter {
private List<String>urls;
private Context context;
public MyPageAdapter(List<String> urls, Context context) {
this.urls = urls;
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
//加载显示图片
ImageLoader.getInstance().displayImage(urls.get(position%urls.size()),imageView, ImageLoaderUtils.getImageOptions(context));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center"
android:text="我的频道"
android:textAppearance="@android:style/TextAppearance.Holo.Medium" />
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center"
android:text="更多频道"
android:textAppearance="@android:style/TextAppearance.Holo.Medium" />
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.wangxinpeng20180721">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------------------------------------------------------------------------------------------------------
tableLayout在viewPage下面的布局
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="@id/vp"
android:layout_weight="1"
app:tabBackground="@color/colorPrimaryDark"
app:tabSelectedTextColor="#ff0033"
app:tabTextColor="#00ff96">
</android.support.design.widget.TabLayout>
<ImageView
android:id="@+id/intent"
android:layout_width="30dp"
android:layout_height="40dp"
android:src="@drawable/ic_arrow_forward_black_24dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f00"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------
浙公网安备 33010602011771号