Fragment界面切换之Fragment功能的实现

Fragment1.java:

package com.example.fragment;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment1 extends ListFragment {
 
 LayoutInflater mInflater = null; 
 String[] s1 = { "ͼ1", "ͼ2", "ͼ3", "ͼ4", "ͼ5", "ͼ6", "ͼ7", "ͼ8", "ͼ9", "ͼ10" };
 String[] s2 = { "֢ˇͼ1", "֢ˇͼ2", "֢ˇͼ3", "֢ˇͼ4", "֢ˇͼ5", "֢ˇͼ6", "֢ˇͼ7",
   "֢ˇͼ8", "֢ˇͼ9", "֢ˇͼ10" };

 int[] i = { R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher }; 
 MyAdapter adapter=null;
@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) { 
  View x=inflater.inflate(R.layout.fragment1, null);
  adapter=new MyAdapter(getActivity());
  setListAdapter(adapter);   
  return x;}
 //֣ܷ˂ݾ
 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
  System.out.println(l.getOnItemClickListener());
//  if(l.getId()==position){
//   Toast.makeText(getActivity(), "ţ֣‹ͼ"+l.getId(), 1000).show();
//  }
  int i=(int) l.getItemIdAtPosition(position);
  if(position==i){
   Toast.makeText(getActivity(), "ţ֣‹ͼ"+(i+1), 500).show();
  }
//  switch (position) {
//  case i:
//   Toast.makeText(getActivity(), "ţ֣‹ͼ1", 500).show();
//   break;
//  case 1:
//   Toast.makeText(getActivity(), "ţ֣‹ͼ2", 500).show();
//   break;
//  }
  super.onListItemClick(l, v, position, id);
 }
//
 // ˊƤǷ
 class MyAdapter extends BaseAdapter {

  
   public MyAdapter(Context context){ 
          super(); 
          mInflater = (LayoutInflater) context 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 

  @Override
  public int getCount() {
   return s1.length;
  }

  @Override
  public Object getItem(int arg0) {
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   return arg0;
  }

  @Override
  public View getView(int arg0, View arg1, ViewGroup arg2) {
   HolderView holder=null;
   if(arg1==null||arg1.getTag()==null){
    arg1 = mInflater.inflate(R.layout.demo1, null);
    holder = new HolderView(arg1);
    arg1.setTag(holder);
   }else{
    holder=(HolderView) arg1.getTag();
   }
   
   holder.im.setImageResource(i[arg0]);
   holder.tv1.setText(s1[arg0].toString());
   holder.tv2.setText(s2[arg0].toString());
   
   
   return arg1;
  }
  class HolderView{
   ImageView im;
   TextView tv1,tv2;
   public HolderView(View v){
    im=(ImageView) v.findViewById(R.id.iv_people);
    tv1=(TextView) v.findViewById(R.id.tv_people);
    tv2=(TextView) v.findViewById(R.id.tv_foodname);
   }
  }

 }
}

Fragment2.java:

package com.example.fragment;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragment2 extends ListFragment {

 LayoutInflater mInflater = null;
 String[] s1 = { "店1", "店2", "店3", "店4", "店5", "店6", "店7", "店8", "店9", "店10" };
 String[] s2 = { "人物1", "人物2", "人物3", "人物4", "人物5", "人物6", "人物7", "人物8",
   "人物9", "人物10" };
 String[] s3 = { "这是店1", "这是店2", "这是店3", "这是店4", "这是店5", "这是店6", "这是店7",
   "这是店8", "这是店9", "这是店10" };

 int[] i = { R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher };
 MyAdapter adapter = null;

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

  adapter = new MyAdapter();
  setListAdapter(adapter);
  return view;
 }

 class MyAdapter extends BaseAdapter {

  @Override
  public int getCount() {
   // TODO 自动生成的方法存根
   return s1.length;
  }

  @Override
  public Object getItem(int arg0) {
   // TODO 自动生成的方法存根
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   // TODO 自动生成的方法存根
   return arg0;
  }

  @Override
  public View getView(int arg0, View arg1, ViewGroup arg2) {
   HolderView holder = null;
   if (arg1 == null || arg1.getTag() == null) {
    arg1 = LayoutInflater.from(getActivity()).inflate(
      R.layout.demo2, null);
    holder = new HolderView(arg1);
    arg1.setTag(holder);
   } else {
    holder = (HolderView) arg1.getTag();
   }
   holder.people.setImageResource(i[arg0]);
   holder.photo.setImageResource(i[arg0]);
   holder.tv_foodname.setText("【" + "小店" + "】" + s1[arg0].toString());
   holder.tv_info.setText(s3[arg0].toString());
   holder.tv_people.setText(s2[arg0].toString());
   holder.tv_photo.setText(s1[arg0].toString());

   return arg1;
  }

  class HolderView {
   ImageView people, photo;
   TextView tv_people, tv_photo, tv_foodname, tv_info;

   public HolderView(View v) {
    people = (ImageView) v.findViewById(R.id.image_people);
    photo = (ImageView) v.findViewById(R.id.image_photo);
    tv_foodname = (TextView) v.findViewById(R.id.tv_foodname);
    tv_photo = (TextView) v.findViewById(R.id.tv_photoname);
    tv_people = (TextView) v.findViewById(R.id.tv_peoplename);
    tv_info = (TextView) v.findViewById(R.id.tv_foodinfo);
   }

  }

 }
}

Fragment3.java:

package com.example.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Fragment3  extends Fragment {
 private Spinner  city;
 TextView tv;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  View v=inflater.inflate(R.layout.fragment3, container, false);
  city=(Spinner)v.findViewById(R.id.city);
  tv=(TextView) v.findViewById(R.id.tv_spinner);
  
  ArrayAdapter<CharSequence> adapter =  ArrayAdapter.createFromResource(getActivity(),
  R.array.city, android.R.layout.simple_spinner_item);
  
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
  
  city.setAdapter(adapter); 
  city.setOnItemSelectedListener(listener);     
  return v;
 }
 
 OnItemSelectedListener listener =new OnItemSelectedListener() {

  @Override
  public void onItemSelected(AdapterView<?> parent, View view,
    int position, long id) {
   Spinner spinner = (Spinner)parent;
   String pro = spinner.getSelectedItem().toString();
   
   if(pro.equals("武汉"))
   {
//    System.out.println("你点了武汉");
    tv.setText("你点了武汉");
   }
   else if(pro.equals("南阳"))
   {
//    System.out.println("你点了南阳");
    tv.setText("你点了南阳"); 
   }
   if(pro.equals("郑州"))
   {
//    System.out.println("你点了郑州");
    tv.setText("你点了郑州");
   }
   else if(pro.equals("杭州"))
   {
//    System.out.println("你点了杭州");
    tv.setText("你点了杭州");    
   }
   }
   
  

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO 自动生成的方法存根
  }
 };
}

Fragment4.java:

package com.example.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Fragment4 extends Fragment implements OnKeyListener{
 WebView webView;

 @SuppressLint("SetJavaScriptEnabled")
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  
  View v = inflater.inflate(R.layout.fragment4, container, false);
  webView = (WebView) v.findViewById(R.id.webView1);
  webView.getSettings().setJavaScriptEnabled(true);

  webView.loadUrl("http://www.mabaoo.com/modoer_qj/item.php?act=mobile&do=category");

  // 为了避免弹出系统默认的Browser,并且为了能够继续在WebView中浏览,要用到shouldOverrideUrlLoading方法
  webView.setWebViewClient(new MyWebViewClient());
  
  return v;
 }

 class MyWebViewClient extends WebViewClient {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadUrl(url);
   return true;
  }
 }

 @Override
 public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
  if(arg1==KeyEvent.KEYCODE_BACK&&webView.canGoBack()){
   webView.goBack();
   return true;
  }
  return true;
 }
}

你可能感兴趣的:(Fragment界面切换之Fragment功能的实现)