fragment练习

这篇博客介绍了Android应用程序中Fragment的使用和XML布局的相关内容。首先,文章讲解了Activity的部分,然后深入到Fragment的实践操作,接着展示了XML布局文件的`main`和`fragment`部分。特别提到Main2Activity作为一个欢迎页面,可以实现直接跳转的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@[toc] 引导页

activity部分.

package com.example.day1011_homework;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.day1011_homework.fragment.WelcomeFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private TextView text;
    private ViewPager viewPager;
    private Button but;
    private Timer timer;
    private Timer timer_time;
    private int index = 0;
    private int number = 5;
    private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
    private List<Food.DataBean> dataBeans = new ArrayList<>();

    private String[] pics = {"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg",
            "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg",
            "http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg"
    };

    private static final String TAG = "MainActivity";

    private List<WelcomeFragment> lists = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    viewPager.setCurrentItem(index);
                    index++;
                    if (index == lists.size()) {
                        index = 0;
                        text.setVisibility(View.VISIBLE);
                        but.setVisibility(View.VISIBLE);
                        timer.cancel();
                        timeget();
                    } else {
                        text.setVisibility(View.GONE);
                        but.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    number--;
                    text.setText("倒计时:" + number + "秒");
                    if (number == 0) {
                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                        startActivity(intent);
                        timer_time.cancel();
                        finish();
                    }
                    break;
            }

        }
    };

    private void timeget() {


        timer_time = new Timer();
        timer_time.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(2);
            }
        }, 0, 1000);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.text);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        but = (Button) findViewById(R.id.but);



        for (int i = 0; i < 3; i++) {
            WelcomeFragment fragment = new WelcomeFragment();
            Bundle bundle = new Bundle();
            bundle.putString("msg", "这是第" + i + "个FragMent");
            bundle.putString("pic",pics[i]);
//            Log.i(TAG, "onCreate: 这是i的值"+i);
            fragment.setArguments(bundle);
            lists.add(fragment);
        }


        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                timer_time.cancel();
                finish();
            }
        });


        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return lists.get(i);
            }

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


        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 0, 1000);
//        new MyAsyncTask(dataBeans, bundle).execute(path);

    }


}

fragment 部分

package com.example.day1011_homework.fragment;


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 android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day1011_homework.Food;
import com.example.day1011_homework.Http_Utils;
import com.example.day1011_homework.MainActivity;
import com.example.day1011_homework.MyAsyncTask;
import com.example.day1011_homework.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

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


    private static final String TAG = "WelcomeFragment";



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


        TextView textView = inflate.findViewById(R.id.fm_Text);
        ImageView imageView = inflate.findViewById(R.id.fm_image);
        Bundle bundle = getArguments();


        if (bundle != null) {
            String msg = bundle.getString("msg");
            String pic = bundle.getString("pic");
            textView.setText(msg);
            Glide.with(getActivity()).load(pic).into(imageView);

        }
//        Log.i(TAG, "onCreateView: 阿斯顿法国红酒巨化股份很过分的时尚的风格的身上的");

        return inflate;
    }

}

xml 部分

main

<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">

    <TextView
        android:id="@+id/text"
        android:visibility="gone"
        android:text="倒计时:5秒"
        android:textSize="30sp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="40dp"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <Button
        android:visibility="gone"
        android:id="@+id/but"
        android:text="立即体验"
        android:textSize="30sp"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="20dp"
        android:layout_height="wrap_content" />



</RelativeLayout>
fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".fragment.WelcomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fm_Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="30sp"
        android:text="这是提示" />

    <ImageView
        android:id="@+id/fm_image"
        android:layout_marginTop="100dp"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>

Main2Activity 是一个欢迎页面,可以直接跳转

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值