@[toc]作业 10.10
##日考6
activity
package com.example.app2;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.app2.fragment.BlankFragment;
import com.example.app2.fragment.BlankFragment2;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout lin0005;
private RadioGroup radioG;
private RadioButton radioB1;
private RadioButton radioB2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.view_pager);
lin0005 = (LinearLayout) findViewById(R.id.lin0005);
radioG = (RadioGroup) findViewById(R.id.radio_G);
radioB1 = (RadioButton) findViewById(R.id.radio_B1);
radioB2 = (RadioButton) findViewById(R.id.radio_B2);
FragmentManager manager = getSupportFragmentManager();
final FragmentTransaction transaction = manager.beginTransaction();
final BlankFragment blankFragment = new BlankFragment();
final BlankFragment2 blankFragment1 = new BlankFragment2();
transaction.add(R.id.lin0005,blankFragment);
transaction.add(R.id.lin0005,blankFragment1);
transaction.hide(blankFragment1);
transaction.commit();
radioG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_B1:
String s = radioB1.getText().toString();
FragmentManager manager1 = getSupportFragmentManager();
FragmentTransaction transaction1 = manager1.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("key",s);
blankFragment.setArguments(bundle);
transaction1.show(blankFragment);
transaction1.hide(blankFragment1);
transaction1.commit();
break;
case R.id.radio_B2:
String s1 = radioB2.getText().toString();
FragmentManager manager2 = getSupportFragmentManager();
FragmentTransaction transaction2 = manager2.beginTransaction();
Bundle bundle1 = new Bundle();
bundle1.putString("key1",s1);
blankFragment1.setArguments(bundle1);
transaction2.show(blankFragment1);
transaction2.hide(blankFragment);
transaction2.commit();
break;
}
}
});
}
}
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_pager"
android:layout_width="wrap_content"
android:layout_height="100dp">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/lin0005"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
<RadioGroup
android:id="@+id/radio_G"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton
android:layout_weight="1"
android:id="@+id/radio_B1"
android:button="@null"
android:text="天气"
android:gravity="center"
android:textSize="30sp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<RadioButton
android:layout_weight="1"
android:id="@+id/radio_B2"
android:button="@null"
android:text="火山"
android:gravity="center"
android:textSize="30sp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</RadioGroup>
</RelativeLayout>
package com.example.app2.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.app2.R;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment2 extends Fragment {
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
Bundle arguments = getArguments();
Toast.makeText(getActivity(),"天气", Toast.LENGTH_SHORT).show();
return inflate;
}
}
package com.example.app2.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.Toast;
import com.example.app2.R;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
Toast.makeText(getActivity(),"火山", Toast.LENGTH_SHORT).show();
return inflate;
}
}
介绍 (油焖大虾)
package com.example.day1010_homework.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.TextView;
import com.example.day1010_homework.R;
/**
* A simple {@link Fragment} subclass.
*/
public class MyFragment extends Fragment {
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_my, container, false);
TextView textView = inflate.findViewById(R.id.fm_tv);
Bundle arguments = getArguments();
if(arguments != null){
String key = arguments.getString("key");
textView.setText(key);
}
return inflate;
}
}
//这是网络请求
package com.example.day1010_homework.MyJava;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Http_Utils {
public static String getJsonFromUrl(String page){
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//传入网址
URL url = new URL(page);
//获取连接
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//设置使用G请求
connection.setRequestMethod("GET");
//设置连接和请求超时
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
//判断连接是否成功
if(connection.getResponseCode() == 200){
//获取流
is = connection.getInputStream();
baos = new ByteArrayOutputStream();
//定义一个整型变量
int len = 0;
//定义一个字节数组
byte[] bys = new byte[1024];
//读取
while ((len = is.read(bys)) != -1){
baos.write(bys,0,len);
baos.flush();
}
String json = baos.toString();
return json;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
//这是activoty
package com.example.day1010_homework;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.example.day1010_homework.MyJava.Food;
import com.example.day1010_homework.MyJava.MyAdapter;
import com.example.day1010_homework.MyJava.MyAsyncTask;
import com.example.day1010_homework.fragment.MyFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private LinearLayout linear01;
private String page = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private List<Food.DataBean> lists = new ArrayList<>();
private MyAdapter adapter;
private MyAsyncTask asyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//执行网络操作
LoadFromUrl();
}
private void LoadFromUrl() {
new MyAsyncTask(this,lists,adapter).execute(page);
}
private void initView() {
listView = (ListView) findViewById(R.id.list_view);
linear01 = (LinearLayout) findViewById(R.id.linear01);
adapter = new MyAdapter(this,lists);
listView.setAdapter(adapter);
final FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
final MyFragment fragment = new MyFragment();
transaction.add(R.id.linear01,fragment);
transaction.commit();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String title = lists.get(position).getTitle();
FragmentManager manager1 = getSupportFragmentManager();
FragmentTransaction transaction1 = manager1.beginTransaction();
MyFragment fragment1 = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("key",title);
fragment1.setArguments(bundle);
transaction1.replace(R.id.linear01,fragment1);
transaction1.commit();
}
});
}
}
//这是适配器
package com.example.day1010_homework.MyJava;
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.bumptech.glide.Glide;
import com.example.day1010_homework.R;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Food.DataBean> lists;
public MyAdapter(Context context, List<Food.DataBean> lists) {
this.context = context;
this.lists = lists;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.layout_home,null);
holder.textView = convertView.findViewById(R.id.text_title);
holder.imageView = convertView.findViewById(R.id.image_home);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(lists.get(position).getTitle());
Glide.with(context).load(lists.get(position).getPic()).into(holder.imageView);
return convertView;
}
private class ViewHolder {
private ImageView imageView;
private TextView textView;
}
}
//这是异步任务
package com.example.day1010_homework.MyJava;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.List;
public class MyAsyncTask extends AsyncTask<String,Void, List<Food.DataBean>> {
private static final String TAG = "MyAsyncTask";
private Context context;
private List<Food.DataBean> lists;
private MyAdapter adapter;
public MyAsyncTask(Context context, List<Food.DataBean> lists, MyAdapter adapter) {
this.context = context;
this.lists = lists;
this.adapter = adapter;
}
@Override
protected List<Food.DataBean> doInBackground(String... strings) {
Log.i(TAG, "doInBackground: 这是1");
String json = Http_Utils.getJsonFromUrl(strings[0]);
Log.i(TAG, "doInBackground: 这是2");
Food food = new Gson().fromJson(json, Food.class);
List<Food.DataBean> data = food.getData();
if(data == null){
Toast.makeText(context, "数据获取失败!!!", Toast.LENGTH_SHORT).show();
}else {
return data;
}
return null;
}
@Override
protected void onPostExecute(List<Food.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
if(dataBeans != null){
lists.addAll(dataBeans);
adapter.notifyDataSetChanged();
}else {
Toast.makeText(context, "Json数据获取失败!!", Toast.LENGTH_SHORT).show();
}
}
}
这是生命周期
package com.example.app3.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.app3.R;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
private static final String TAG = "BlankFragment";
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach: ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: ");
}
}
//这是activity
package com.example.app3;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.example.app3.fragment.BlankFragment;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.line002);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.line002,new BlankFragment());
transaction.commit();
}
}