MianActivity
package com.example.lenovo.mnyk01;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final int FLAG = 123;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==FLAG){
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = findViewById(R.id.text);
handler.sendEmptyMessageDelayed(FLAG,3000);
}
}
ShowActivity
package com.example.lenovo.mnyk01;
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 android.widget.TextView;
import com.example.lenovo.mnyk01.fragment.FinishFragment;
import com.example.lenovo.mnyk01.fragment.ShowFragment;
public class ShowActivity extends AppCompatActivity {
private static final String url ="http://172.17.8.100/mobile/exam/findNewList";
private ShowFragment showFragment;
private FinishFragment finishFragment;
private FragmentManager manager;
private FragmentTransaction begin;
private TextView tiao;
private int page = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
tiao = findViewById(R.id.tiao);
showFragment = new ShowFragment();
finishFragment = new FinishFragment();
manager = getSupportFragmentManager();
begin = manager.beginTransaction();
begin.add(R.id.content,showFragment)
.add(R.id.content,finishFragment)
.hide(finishFragment).commit();
tiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(page ==0){
page = 1;
manager.beginTransaction()
.hide(showFragment).show(finishFragment).commit();
}else{
page = 0;
manager.beginTransaction()
.show(showFragment).hide(finishFragment).commit();
}
}
});
}
}
//showLayout
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="170dp"
android:text="首页"
android:textSize="25dp" />
<TextView
android:id="@+id/tiao"
android:paddingLeft="90dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击" />
</LinearLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
FirstFragment
package com.example.lenovo.mnyk01.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.lenovo.mnyk01.HttpUtils;
import com.example.lenovo.mnyk01.Newss;
import com.example.lenovo.mnyk01.NewssAdapter;
import com.example.lenovo.mnyk01.R;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class FinishFragment extends Fragment{
private ListView xlvNews;
private List<Newss.BodyBean.ResultBean> list;
private static String url = "https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
private NewssAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.finishfragment,null,false);
xlvNews = v.findViewById(R.id.xlv);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = new ArrayList<>();
adapter = new NewssAdapter(getActivity(),list);
xlvNews.setAdapter(adapter);
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
String result = HttpUtils.getFromHttpUrlConnection(strings[0]);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Newss newss = gson.fromJson(s, Newss.class);
list.clear();
list.addAll(newss.getBody().getResult());
adapter.notifyDataSetChanged();
}
}.execute(url);
}
}
ShowFragment
package com.example.lenovo.mnyk01.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.example.lenovo.mnyk01.HttpUtils;
import com.example.lenovo.mnyk01.News;
import com.example.lenovo.mnyk01.NewsAdapter;
import com.example.lenovo.mnyk01.R;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class ShowFragment extends Fragment{
private static final String url ="http://172.17.8.100/mobile/exam/findNewList";
private TextView tiao;
private ListView lvNews;
private List<News.ResultBean.DataBean> list;
private NewsAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.showfragment,null,false);
lvNews = v.findViewById(R.id.lv_news);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list= new ArrayList<>();
adapter = new NewsAdapter(getContext(),list);
lvNews.setAdapter(adapter);
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
String result = HttpUtils.getFromHttpUrlConnection(strings[0]);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
News news = gson.fromJson(s, News.class);
list.clear();
list.addAll(news.getResult().getData());
adapter.notifyDataSetChanged();
}
}.execute(url);
}
}
本文介绍了一个基于Android的应用启动流程实现,通过Handler延迟发送消息启动ShowActivity,并在ShowActivity中实现了Fragment之间的切换,包括ShowFragment和FinishFragment,展示了如何使用FragmentManager进行Fragment的显示和隐藏操作。
286

被折叠的 条评论
为什么被折叠?



