起始页+Fragment +List View MyAdapter2 自定义适配器 适配器书写方法 主页面的书写 主页面的 核心代码 页面中嵌套 tablayout 页面中嵌套 tablayout 页面书写(注意 : 要导入依赖) 普通 Frgment 页面随意书写 每一个 frgment对应一个类 fegment中 如何书写 ListView ListView需要适配器才可以 显示内容 适配器 书写
起始页+Fragment 简单综合项目
1.首选确认需求 再进行书写
(1) 起始页 +页面跳转到主页面 // 首选前端页面控件 获取到 后台 private ViewPager vpPaget2; private TextView tvText; // 适配器 Fragment的集合 在里面存入 想要在 起始页显示的 Fragment private ArrayList<Fragment> totalist=new ArrayList<>(); // MyAdapter2 自定义适配器 private MyAdapter2 myAdapter2; private FragmentTransaction transaction; private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcom); vpPaget2 = findViewById(R.id.vp_paget2); tvText = findViewById(R.id.tv_text); totalist.add(new MyFragmentChildOne()); totalist.add(new MyFragmentChildTwo()); totalist.add(new MyFragmentChildThree()); myAdapter2=new MyAdapter2(getSupportFragmentManager(),totalist); vpPaget2.setAdapter(myAdapter2); vpPaget2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position==totalist.size()-1){ // 使用CountDownTimer 进行计时并且 new CountDownTimer(3000,1000) { @SuppressLint("SetTextI18n") @Override public void onTick(long l) { // 当CountDownTimer 执行一次 进行一次 方法 tvText.setText("倒计时"+l/1000+"秒"); } @Override public void onFinish() { // 当三次读秒结束 进行页面跳转 Intent intent = new Intent(WelcomActivity.this, MainActivity.class); startActivity(intent); // 最后 finish 关闭当前 页面 finish(); } }.start(); } } @Override public void onPageScrollStateChanged(int state) { } }); } }
public class MyAdapter2 extends FragmentPagerAdapter { //声明集合 通过构造赋值 private ArrayList<Fragment> totalist; //FragmentManager 调用getSupportFragmentManager() 进行赋值 public MyAdapter2(@NonNull FragmentManager fm, ArrayList<Fragment> totalist) { super(fm); this.totalist = totalist; } //返回 postion位置的 Fegment @NonNull @Override public Fragment getItem(int position) { return totalist.get(position); } //返回 feagment的总数 @Override public int getCount() { return totalist.size(); } }
<?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"> <FrameLayout android:id="@+id/myFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> <RadioGroup android:id="@+id/radio_button" android:layout_width="match_parent" android:layout_height="70dp" android:orientation="horizontal" android:layout_alignParentBottom="true" > <RadioButton android:id="@+id/index" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:text="首页" android:gravity="center" android:checked="true" android:drawableTop="@mipmap/ye" android:paddingTop="5dp" /> <RadioButton android:id="@+id/photos" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:text="图库" android:gravity="center" android:checked="true" android:drawableTop="@mipmap/hm" android:paddingTop="5dp" /> <RadioButton android:id="@+id/finds" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:text="首页" android:gravity="center" android:checked="true" android:drawableTop="@mipmap/search" android:paddingTop="5dp" /> <RadioButton android:id="@+id/linkman" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:text="消息" android:gravity="center" android:drawableTop="@mipmap/wechat" android:paddingTop="5dp" /> <RadioButton android:id="@+id/discover" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:text="我的" android:gravity="center" android:drawableTop="@mipmap/post" android:paddingTop="5dp" /> </RadioGroup> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="20sp" android:textColor="#ffffff" android:layout_above="@+id/radio_button" /> </RelativeLayout>
// 五个页面的 显示转换 private void initView() { myFragment = findViewById(R.id.myFragment); radioButton = findViewById(R.id.radio_button); index = findViewById(R.id.index); photos = findViewById(R.id.photos); finds = findViewById(R.id.finds); linkman = findViewById(R.id.linkman); discover = findViewById(R.id.discover); textView = findViewById(R.id.text_view); one=new MyFragmentOne(); two=new MyFragmentTwo(); three=new MyFragmentThree(); four=new MyFragmentFour(); find=new MyFragmentFind(); getSupportFragmentManager() .beginTransaction() .add(R.id.myFragment,one) .add(R.id.myFragment,two) .add(R.id.myFragment,three) .add(R.id.myFragment,four) .add(R.id.myFragment,find) .commit(); radioButton.setOnCheckedChangeListener ( new RadioGroup.OnCheckedChangeListener ( ) { @Override public void onCheckedChanged(RadioGroup group , int checkedId) { switch (checkedId) { case R.id.index: getSupportFragmentManager() .beginTransaction() .replace(R.id.myFragment,find).commit(); break; case R.id.finds: getSupportFragmentManager () .beginTransaction () .replace(R.id.myFragment,one) .commit (); break; case R.id.linkman: getSupportFragmentManager () .beginTransaction () .replace(R.id.myFragment,two) .commit (); break; case R.id.discover: getSupportFragmentManager () .beginTransaction () .replace(R.id.myFragment,three) .commit (); break; case R.id.photos: getSupportFragmentManager() .beginTransaction() .replace(R.id.myFragment,four).commit(); break; } } } ); } }
public class MyFragmentFind extends Fragment { private TabLayout tlLayout; private ViewPager vpPager; private ArrayList<String> titles=new ArrayList<>(); private ArrayList<Fragment> totalist=new ArrayList<>(); private MyFragmentChildOne one=new MyFragmentChildOne(); private MyFragmentChildTwo two=new MyFragmentChildTwo(); private MyFragmentChildThree three=new MyFragmentChildThree(); private MyFragmentList list=new MyFragmentList(); private MyFragmentParger parger; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_tab,null); tlLayout =view.findViewById(R.id.tl_layout); vpPager = view.findViewById(R.id.vp_pager); initDatas(); return view; } private void initDatas() { titles.add("推荐"); titles.add("男士发型打理"); titles.add("热发型"); titles.add("编发教程"); totalist.add(list); totalist.add(one); totalist.add(two); totalist.add(three); parger=new MyFragmentParger(getChildFragmentManager(),totalist,titles); vpPager.setAdapter(parger); tlLayout.setupWithViewPager(vpPager); } }
依赖:
implementation ‘com.android.support:design:28.0.0’<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.google.android.material.tabs.TabLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:id="@+id/tl_layout"/> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:id="@+id/vp_pager"/> </LinearLayout>
每个对应类 需要继承 Frgment 并且重写方法 public class MyFragmentOne extends Fragment { private RadioGroup radioButton02; private MyFragmentChildOne childOne; private MyFragmentChildTwo childTwo; private MyFragmentChildThree childThree; private MyFragmentChildFour childFour; private MyFragmentChildFive childFive; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater , @Nullable ViewGroup container , @Nullable Bundle savedInstanceState) { View view = inflater.inflate ( R.layout.fragment_view01 , null ); childOne = new MyFragmentChildOne ( ); childTwo = new MyFragmentChildTwo ( ); childThree = new MyFragmentChildThree ( ); childFour = new MyFragmentChildFour ( ); childFive = new MyFragmentChildFive ( ); // childOne.setMessage ( childThree ); MainActivity activity = (MainActivity) getActivity ( ); //childThree.setMessage (activity.three ); getChildFragmentManager ( ) .beginTransaction ( ) .add ( R.id.myFragment_02 , childTwo ) .add ( R.id.myFragment_02 , childThree ) .add ( R.id.myFragment_02 , childFour ) .add ( R.id.myFragment_02 , childFive ) .add ( R.id.myFragment_02 , childOne ) .hide ( childTwo ) .hide ( childThree ) .hide ( childFour ) .hide ( childFive ) .show ( childOne ) .commit ( ); radioButton02 = (RadioGroup) view.findViewById ( R.id.radio_button_02 ); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated ( savedInstanceState ); // final MainActivity activity = (MainActivity) getActivity ( ); radioButton02.setOnCheckedChangeListener ( new RadioGroup.OnCheckedChangeListener ( ) { @Override public void onCheckedChanged(RadioGroup group , int checkedId) { switch (checkedId) { case R.id.button_all: getChildFragmentManager ( ) .beginTransaction ( ) .hide ( childTwo ) .hide ( childThree ) .hide ( childFour ) .hide ( childFive ) .show ( childOne ) .commit ( ); break; case R.id.button_video: getChildFragmentManager ( ) .beginTransaction ( ) .hide ( childOne ) .hide ( childThree ) .hide ( childFour ) .hide ( childFive ) .show ( childTwo ) .commit ( ); break; case R.id.button_pic: getChildFragmentManager ( ) .beginTransaction ( ) .hide ( childOne ) .hide ( childTwo ) .hide ( childThree ) .hide ( childFive ) .show ( childFour) .commit ( ); break; case R.id.button_voice: getChildFragmentManager ( ) .beginTransaction ( ) .hide ( childOne ) .hide ( childTwo ) .hide ( childFour ) .hide ( childFive ) .show ( childThree) .commit ( ); break; case R.id.button_dz: getChildFragmentManager ( ) .beginTransaction ( ) .hide ( childOne ) .hide ( childTwo ) .hide ( childThree ) .hide ( childFour ) .show (childFive ) .commit ( ); break; } } } ); } }
public class MyFragmentList extends Fragment { private ListView iv_list; // 放入json 数据的 网址 private String urlString="https://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1"; private MyAdapter myAdapter; // json数据解析后的类 创建集合 private List<JavaBean.DataBean> totalist=new ArrayList<>(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_list,null); new JsonAsyntck().execute(urlString); iv_list=view.findViewById(R.id.iv_list); myAdapter=new MyAdapter(getActivity().getBaseContext(),totalist); iv_list.setAdapter(myAdapter); iv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getActivity().getBaseContext(), "短点击事件", Toast.LENGTH_SHORT).show(); } }); iv_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getActivity().getBaseContext(), "长点事件", Toast.LENGTH_SHORT).show(); return false; } }); return view; } public class JsonAsyntck extends AsyncTask<String,Void,List<JavaBean.DataBean>> { @Override protected List<JavaBean.DataBean> doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); if (conn.getResponseCode()==200){ InputStream is = conn.getInputStream(); ByteArrayOutputStream byso = new ByteArrayOutputStream(); int len=0; byte[] bs = new byte[1024]; while ((len=is.read(bs))!=-1){ byso.write(bs,0,len); } String json = byso.toString(); Gson gson = new Gson(); JavaBean javaBean = gson.fromJson(json, JavaBean.class); List<JavaBean.DataBean> data = javaBean.getData(); return data; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List<JavaBean.DataBean> dataBeans) { super.onPostExecute(dataBeans); if (dataBeans!=null){ totalist.addAll(dataBeans); myAdapter.notifyDataSetChanged(); } } } }
public class MyAdapter extends BaseAdapter { private Context context; private List<JavaBean.DataBean> totalist; private LayoutInflater layoutInflater; public MyAdapter(Context context, List<JavaBean.DataBean> totalist) { this.context = context; this.totalist = totalist; this.layoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return totalist.size(); } @Override public Object getItem(int position) { return totalist.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHoder viewHoder=null; if (convertView==null){ viewHoder=new ViewHoder(); convertView=layoutInflater.inflate(R.layout.item_list,null); viewHoder.iv_item=convertView.findViewById(R.id.iv_item); viewHoder.tv_item=convertView.findViewById(R.id.tv_item); convertView.setTag(viewHoder); }else { viewHoder= (ViewHoder) convertView.getTag(); } JavaBean.DataBean dataBean = totalist.get(position); String title = dataBean.getTitle(); String pic = dataBean.getPic(); viewHoder.tv_item.setText(title); Picasso.with(context).load(pic).into(viewHoder.iv_item); return convertView; } class ViewHoder{ private ImageView iv_item; private TextView tv_item; } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算