我的作业Day10

本文深入讲解Fragment在Android开发中的应用,包括Fragment的生命周期、与Activity的交互、动态添加和替换Fragment的方法,以及通过实例演示如何使用Fragment实现页面切换和数据展示。

作业一 Day6日考

mainActivity及布局xml
package com.example.day10_fragment_hk;

import android.support.v4.app.Fragment;
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 com.example.day10_fragment_hk.Fragment.FirstFragment;
import com.example.day10_fragment_hk.Fragment.SecondFragment;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private FirstFragment firstFragment;
    private SecondFragment secondFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();

        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();

        transaction.add(R.id.layout_sds,firstFragment);
        transaction.add(R.id.layout_sds,secondFragment);

        transaction.commit();
    }

    public void click(View view) {
        switch (view.getId()){
            case R.id.radio_one:

                fragmentManager = getSupportFragmentManager();
                transaction = fragmentManager.beginTransaction();

                transaction.show(firstFragment);
                transaction.hide(secondFragment);

                transaction.commit();
                break;

            case R.id.radio_two:

                fragmentManager = getSupportFragmentManager();
                transaction = fragmentManager.beginTransaction();

                transaction.show(secondFragment);
                transaction.hide(firstFragment);


                transaction.commit();
                break;

        }
    }
}

<?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:layout_width="0dp"
        android:layout_height="0dp">

    </android.support.v4.view.ViewPager>
    <LinearLayout
        android:layout_above="@id/layout_line"
        android:id="@+id/layout_sds"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">


    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/layout_line"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="70dp">

        <RadioGroup
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:onClick="click"
                android:text="第一个"
                android:gravity="center"
                android:id="@+id/radio_one"
                android:button="@null"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <RadioButton
                android:onClick="click"
                android:gravity="center"
                android:text="第二个"
                android:id="@+id/radio_two"
                android:button="@null"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </RadioGroup>

    </LinearLayout>

</RelativeLayout>
碎片布局
package com.example.day10_fragment_hk.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.Button;
import android.widget.Toast;

import com.example.day10_fragment_hk.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {


    public FirstFragment() {
        // 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_first, container, false);

        Button viewById = inflate.findViewById(R.id.button_one);

        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "我是第一个Fragment", Toast.LENGTH_SHORT).show();
            }
        });

        return inflate;
    }

}

package com.example.day10_fragment_hk.Fragment;

import android.content.Context;
import android.net.Uri;
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.Button;
import android.widget.Toast;

import com.example.day10_fragment_hk.R;


public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_second, container, false);

        Button viewById = inflate.findViewById(R.id.button_two);

        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "第二个Fragment", Toast.LENGTH_SHORT).show();
            }
        });

        return inflate;
    }

}

布局页面(碎片)

第一个

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="#fa2"
    tools:context=".Fragment.FirstFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textColor="#000"
        android:textSize="25sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是第一个Fragment" />

    <Button
        android:id="@+id/button_one"
        style="?android:attr/borderlessButtonStyle"
        android:text="点击吐司"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

第二个

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="#ff0"
    tools:context=".Fragment.SecondFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textSize="25sp"
        android:textColor="#000"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是第二个Fragment " />

    <Button
        android:id="@+id/button_two"
        style="?android:attr/borderlessButtonStyle"
        android:text="点击吐司"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

作业二 Day5 日常练习技能一

mainActivity 中及布局xml

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        SecondFragment secondFragment = new SecondFragment();

        transaction.add(R.id.layout_line1,secondFragment);

        transaction.commit();

    }
}

布局

 <LinearLayout
        android:orientation="vertical"
        android:id="@+id/layout_line1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </LinearLayout>

    <fragment
        android:name="com.example.app2.Fragment.FirstFragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
碎片一和碎片二

碎片一

public FirstFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }
碎片二 有生命周期的
 private static final String TAG = "SecondFragment";
    public SecondFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ->");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ->");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ->");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ->");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ->");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ->");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ->");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.i(TAG, "onAttach: ->");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach: ->");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: ->");
    }
碎片布局

第一个

<FrameLayout 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.FirstFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textColor="#000"
        android:gravity="center"
        android:textSize="25sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是静态Fragment" />

</FrameLayout>

第二个

<FrameLayout 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.SecondFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textColor="#000"
        android:textSize="25sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是动态Fragment" />


</FrameLayout>

作业3 Day5 技能二

Main中的功能代码及布局xml

首先是MainActivity的内容
public class MainActivity extends AppCompatActivity {

    private FragmentManager manager;
    private FragmentTransaction transaction;

    private FirstFragment firstFragment;
    private SecondFragment secondFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();

        transaction.add(R.id.linear_one,firstFragment);
        transaction.add(R.id.linear_two,secondFragment);

        transaction.commit();
    }
}
接着是MainActivity的布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/linear_one"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/linear_two"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </LinearLayout>

</LinearLayout>

接着就是一大堆功能类,适配器,JavaBean,异步等

适配器(填充页面一个imageView,一个TextView 就不展示了)
public class MyAdapter extends BaseAdapter {

    private List<JavaBean.DataBean> totalList;
    private Context context;
    private LayoutInflater layoutInflater;

    public MyAdapter(List<JavaBean.DataBean> totalList, Context context) {
        this.totalList = totalList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return totalList.size();
    }

    @Override
    public Object getItem(int position) {
        return totalList.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.inflate(R.layout.layout_list_item,null);

            holder.textView_title = convertView.findViewById(R.id.text_title);
            holder.imageView_pic = convertView.findViewById(R.id.image_pic);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView_title.setText(totalList.get(position).getTitle());
        Glide.with(context).load(totalList.get(position).getPic()).into(holder.imageView_pic);

        return convertView;
    }

    static class ViewHolder{
        TextView textView_title;
        ImageView imageView_pic;
    }
}
异步任务
public class MyAsyncTask extends AsyncTask<String, Void, List<JavaBean.DataBean>> {

    private List<JavaBean.DataBean> totalList;
    private MyAdapter adapter;

    public MyAsyncTask(List<JavaBean.DataBean> totalList, MyAdapter adapter) {
        this.totalList = totalList;
        this.adapter = adapter;
    }

    @Override
    protected List<JavaBean.DataBean> doInBackground(String... strings) {

        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            URL url = new URL(strings[0]);
            HttpURLConnection 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);
                Log.i("---", "doInBackground: "+bos.toString());
                return javaBean.getData();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(List<JavaBean.DataBean> dataBeans) {
        super.onPostExecute(dataBeans);

        if(dataBeans != null && dataBeans.size() > 0){
            totalList.addAll(dataBeans);
            adapter.notifyDataSetChanged();
        }

    }
}

第一个碎片(包含listview的)

首先是碎片一的布局xml
<FrameLayout 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.FirstFragment">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</FrameLayout>
然后是碎片一的内部代码
public class FirstFragment extends Fragment {

    private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
    private List<JavaBean.DataBean> list = new ArrayList<>();
    private MyAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_first, container, false);


        initView(inflate);
        initData();
        return inflate;
    }

    private void initView(View inflate) {
        ListView listView = inflate.findViewById(R.id.list_view);
        adapter = new MyAdapter(list,getActivity());

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //方法之一 因为2个碎片在同一个Activity中,随意可以直接getActivity()来获取视图
                TextView textView = getActivity().findViewById(R.id.text_second);
                textView.setText(list.get(position).getTitle());
            }
        });
    }

    private void initData() {
        new MyAsyncTask(list,adapter).execute(path);
    }

}

接下来是碎片二

首先是碎片二的布局xml 哒!
<FrameLayout 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.SecondFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textColor="#000"
        android:textSize="25sp"
        android:gravity="center"
        android:id="@+id/text_second"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="内容" />

</FrameLayout>
其次是碎片二的功能代码

然而什么都没有

public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_second, container, false);
        return inflate;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值