作业
一、MainActivity的代码
(1) 布局xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_page"
android:layout_above="@id/layout_xx"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<TextView
android:visibility="gone"
android:layout_alignParentRight="true"
android:gravity="center"
android:background="#ff0"
android:textColor="#000"
android:textSize="20sp"
android:id="@+id/text_time"
android:text="倒计时:"
android:layout_width="125dp"
android:layout_height="55dp" />
<View
android:id="@+id/view_1"
android:layout_centerInParent="true"
android:layout_width="0dp"
android:layout_height="0dp"/>
<Button
android:visibility="gone"
android:background="#12E0AF"
android:id="@+id/button_time"
android:layout_below="@id/view_1"
android:layout_centerInParent="true"
android:layout_marginTop="150dp"
android:text="立即体验"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/layout_xx"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:textColor="#000"
android:textSize="20sp"
android:text="一"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:id="@+id/radio_one"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:textColor="#000"
android:textSize="20sp"
android:text="二"
android:gravity="center"
android:button="@null"
android:id="@+id/radio_two"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
(2) Activity代码
public class MainActivity extends AppCompatActivity {
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private ViewPager viewPager;
private Button button_time;
private TextView textView_time;
private List<BlankFragment> list = new ArrayList<>();
private List<JavaBean.DataBean> dataList = new ArrayList<>();
private int index = 0;
private int sendTime = 5;
private FragmentManager manager;
private Timer timer1;
private Timer timer;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
viewPager.setCurrentItem(index);
++index;
if(index == list.size()){
index = 0;
}
break;
case 2:
textView_time.setText("倒计时:"+sendTime+"秒");
sendTime --;
if(sendTime == -1){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
timer1.cancel();
finish();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
new MyAsyncTask(dataList,list,viewPager,manager).execute(path);
}
private void initView() {
manager = getSupportFragmentManager();
viewPager = findViewById(R.id.view_page);
textView_time = findViewById(R.id.text_time);
button_time = findViewById(R.id.button_time);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
},0,1000);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if(i == list.size()-1){
textView_time.setVisibility(View.VISIBLE);
button_time.setVisibility(View.VISIBLE);
timer.cancel();
timer1 = new Timer();
timer1.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(2);
}
},0,1000);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
button_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
timer1.cancel();
finish();
}
});
}
}
二、异步任务中(很重要)
public class MyAsyncTask extends AsyncTask<String, Void, List<JavaBean.DataBean>> {
private List<JavaBean.DataBean> totalList;
private List<BlankFragment> list;
private ViewPager viewPager;
private FragmentManager manager;
public MyAsyncTask(List<JavaBean.DataBean> totalList, List<BlankFragment> list, ViewPager viewPager, FragmentManager manager) {
this.totalList = totalList;
this.list = list;
this.viewPager = viewPager;
this.manager = manager;
}
@Override
protected List<JavaBean.DataBean> doInBackground(String... strings) {
List<JavaBean.DataBean> dataBeans = HttpUtils.LoadString(strings[0]);
if (dataBeans!= null && dataBeans.size() > 0) {
return dataBeans;
}
return null;
}
@Override
protected void onPostExecute(List<JavaBean.DataBean> dataBeans) {
if(dataBeans != null && dataBeans.size() > 0){
totalList.addAll(dataBeans);
for (int i = 0; i < 4; i++) {
BlankFragment fragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("title",totalList.get(i).getTitle());
bundle.putString("icon",totalList.get(i).getPic());
fragment.setArguments(bundle);
list.add(fragment);
}
viewPager.setAdapter(new FragmentStatePagerAdapter(manager) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
}
}
}
三、碎片中
(1) 具体功能代码
public class BlankFragment extends Fragment {
private TextView textView_title;
private ImageView imageView_pic;
public BlankFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
textView_title = inflate.findViewById(R.id.text_title);
imageView_pic = inflate.findViewById(R.id.iamge_pic);
Bundle bundle = getArguments();
String title = bundle.getString("title");
String icon = bundle.getString("icon");
textView_title.setText(title);
Glide.with(getActivity()).load(icon).into(imageView_pic);
return inflate;
}
}
(2) 碎片布局页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/text_title"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:textColor="#000"
android:text="我是标题"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iamge_pic"
android:layout_marginTop="100dp"
android:layout_centerInParent="true"
android:layout_below="@id/text_title"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
android:layout_width="175dp"
android:layout_height="175dp" />
</RelativeLayout>
四、其他功能类
(1) HttpUtils
package com.example.day_06_07_homework.enity;
import android.util.Log;
import com.example.day_06_07_homework.MyBean.JavaBean;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class HttpUtils {
public static List<JavaBean.DataBean> LoadString(String s){
InputStream is = null;
ByteArrayOutputStream bos = null;
HttpURLConnection connection;
try {
URL url = new URL(s);
connection = (HttpURLConnection) url.openConnection();
Log.i("TAG", "doInBackground: ->"+connection.getResponseCode());
if(connection.getResponseCode() == 200){
is = connection.getInputStream();
bos = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) != -1){
bos.write(b, 0, len);
}
JavaBean javaBean = new Gson().fromJson(bos.toString(), JavaBean.class);
return javaBean.getData();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
(2) JavaBean类
package com.example.day_06_07_homework.MyBean;
import java.util.List;
public class JavaBean {
private int ret;
private List<DataBean> data;
@Override
public String toString() {
return "JavaBean{" +
"ret=" + ret +
", data=" + data +
'}';
}
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
@Override
public String toString() {
return "DataBean{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", pic='" + pic + '\'' +
", collect_num='" + collect_num + '\'' +
", food_str='" + food_str + '\'' +
", num=" + num +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
第二个Activity什么都没有 直接跳就OK了