Fragment配合viewpager的简单使用
第一部分是fragment的简单使用:
直接写代码,不废话:
1 package com.example.myfragment; 2 3 import java.util.HashMap; 4 import java.util.LinkedList; 5 import java.util.Map; 6 7 import com.example.myfragment.FriendListFragment.CallBack; 8 9 import android.os.Bundle; 10 import android.app.Activity; 11 import android.view.Menu; 12 import android.widget.AdapterView.OnItemSelectedListener; 13 14 public class MainActivity extends Activity implements CallBack{ 15 public static class Friend{ 16 public String name,message; 17 public Friend(){} 18 public Friend(String name,String message){ 19 this.name=name; 20 this.message=message; 21 } 22 public String getMessage(){ 23 return message; 24 } 25 public String getName(){ 26 return name; 27 } 28 } 29 public static class FriendsData{ 30 public static String[] str={"English","Chinese","Math","C++","Sql"}; 31 public static LinkedList<String> list=new LinkedList<String>(); 32 public static HashMap<String,Friend> friends=new HashMap<String, MainActivity.Friend>(); 33 } 34 static{ 35 for(String ele:FriendsData.str){ 36 FriendsData.list.add(ele); 37 FriendsData.friends.put(ele, new Friend(ele, ele+"'s message")); 38 } 39 } 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.activity_main); 44 } 45 @Override 46 public boolean onCreateOptionsMenu(Menu menu) { 47 // Inflate the menu; this adds items to the action bar if it is present. 48 getMenuInflater().inflate(R.menu.main, menu); 49 return true; 50 } 51 @Override 52 public void onItemSelected(int index) { 53 // TODO Auto-generated method stub 54 Bundle b=new Bundle(); 55 b.putInt("id",index); 56 FriendDetailFragment fdf=FriendDetailFragment.newIntance(index); 57 getFragmentManager().beginTransaction().replace(R.id.mainlayout_detail,fdf).commit(); 58 } 59 60 }
下面这个代码是Fragment的一种使用方式:继承ListFragment,让使用该Fragmentment的Activity实现CallBack接口,在main布局中指定大小:
1 package com.example.myfragment; 2 3 import com.example.myfragment.MainActivity.FriendsData; 4 5 import android.app.Activity; 6 import android.app.ListFragment; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class FriendListFragment extends ListFragment { 13 CallBack callbackactivity; 14 15 public interface CallBack { 16 public void onItemSelected(int index); 17 } 18 19 @Override 20 public void onAttach(Activity activity) { 21 // TODO Auto-generated method stub 22 super.onAttach(activity); 23 if (!(activity instanceof CallBack)) { 24 throw new IllegalStateException( 25 "the activity containing friendListFragment must implements Callback interface!"); 26 } 27 callbackactivity = (CallBack) activity; 28 } 29 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 // TODO Auto-generated method stub 33 super.onCreate(savedInstanceState); 34 ArrayAdapter aa = new ArrayAdapter(getActivity(), 35 android.R.layout.simple_list_item_activated_1, 36 android.R.id.text1, FriendsData.list); 37 this.setListAdapter(aa); 38 } 39 40 @Override 41 public void onDestroy() { 42 // TODO Auto-generated method stub 43 super.onDestroy(); 44 } 45 46 @Override 47 public void onListItemClick(ListView l, View v, int position, long id) { 48 // TODO Auto-generated method stub 49 super.onListItemClick(l, v, position, id); 50 callbackactivity.onItemSelected(position); 51 } 52 }
下面又是一种调用Fragment的方式:在代码中调用:
package com.example.myfragment; import com.example.myfragment.MainActivity.Friend; import com.example.myfragment.MainActivity.FriendsData; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; public class FriendDetailFragment extends Fragment { Friend fr=null; public static FriendDetailFragment newIntance(int id){ FriendDetailFragment ff=new FriendDetailFragment(); Bundle args=new Bundle(); args.putInt("id", id); ff.setArguments(args); return ff; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); if(getArguments()!=null&&getArguments().containsKey("id")){ int id=getArguments().getInt("id"); fr=FriendsData.friends.get(FriendsData.list.get(id)); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view=inflater.inflate(R.layout.mine_layout, null); if(fr!=null){ EditText text1=(EditText)view.findViewById(R.id.name),text2=(EditText)view.findViewById(R.id.message); text1.setText(fr.getName()); text2.setText(fr.getMessage()); } return view; } public void onDestroyView() { super.onDestroyView(); fr=null; } }
这个事总的布局文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 android:divider="?android:attr/dividerHorizontal" 7 android:showDividers="middle" 8 android:layout_marginLeft="8dp" 9 android:layout_marginRight="8dp" 10 tools:context=".MainActivity" > 11 <fragment 12 android:name="com.example.myfragment.FriendListFragment" 13 android:id="@+id/mainlayout_list" 14 android:layout_height="match_parent" 15 android:layout_width="0dp" 16 android:layout_weight="1.5"></fragment> 17 <FrameLayout 18 android:layout_width="0dp" 19 android:layout_height="match_parent" 20 android:id="@+id/mainlayout_detail" 21 android:layout_weight="3.0"></FrameLayout> 22 </LinearLayout>
这个事上面说的第二种方式调用的布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:layout_width="70dp" 8 android:layout_height="wrap_content" 9 android:text="Name :" /> 10 <EditText 11 android:id="@+id/name" 12 android:hint="name is ******" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"/> 15 <TextView 16 android:id="@+id/textView1" 17 android:layout_width="70dp" 18 android:layout_height="wrap_content" 19 android:text="Name :" /> 20 <EditText 21 android:id="@+id/message" 22 android:hint="Message is ******" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content"/> 25 26 </LinearLayout>
第二部分:官方提倡讲fragment和viewpager配合使用,接下来就说一下这连个东西如何连起来使用:
简单提一下适配器,适配器,顾名思义,就像是电脑的适配琦,将一个东西经过某种规则映射到另一种g事务上面。适配器中最重要的是getview函数,所以先贴一下Adapter
package adapters; import java.util.HashMap; import java.util.LinkedList; import com.example.maininterface.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ImageAdapter extends BaseAdapter { private LinkedList<HashMap<String, Object>> list; private Context context; private LayoutInflater inflater; private boolean isItem; public ImageAdapter(Context context, LinkedList<HashMap<String, Object>> list, boolean isItem) { this.list = list; this.context = context; this.isItem = isItem; inflater = LayoutInflater.from(this.context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int arg0) { return list.get(arg0); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHandler viewHandler = null; if (convertView == null) { if (isItem) { convertView = inflater.inflate(R.layout.item_images_adapter, parent, false); } else { convertView = inflater.inflate(R.layout.item_imagedir_adapter, parent, false); } viewHandler = new ViewHandler(); viewHandler.image = (ImageView) convertView .findViewById(R.id.images_adapter_image); viewHandler.textVIew = (TextView) convertView .findViewById(R.id.images_adapter_text); convertView.setTag(viewHandler); } else { viewHandler = (ViewHandler) convertView.getTag(); } Bitmap bitmap = BitmapFactory.decodeFile((String) list.get(position) .get("path")); viewHandler.image.setImageBitmap(bitmap); viewHandler.textVIew.setText((String) list.get(position).get("name")); return convertView; } private class ViewHandler { public ImageView image; public TextView textVIew; } }
这里东西主要供自己查询已学过的东西,方便记忆,说的不清各位勿怪。
然后看一下重点viewpager:
首先要是想他和fragment的联合使用也要用都适配器:
import java.util.LinkedList; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; public class FragmentAdapter extends FragmentPagerAdapter implements OnPageChangeListener { private LinkedList<Fragment> list; private int currentIndex = 0; public FragmentAdapter(FragmentManager fm, LinkedList<Fragment> list, ViewPager viewPager) { super(fm); this.list = list; viewPager.setOnPageChangeListener(this); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int position) { Animation animation = null; animation = new TranslateAnimation(currentIndex, position, 0, 0); animation.setFillAfter(true); currentIndex = position; animation.setDuration(500); } }
然后……好像没什么了……还有一个还可以直接在adapter中为viewpaget添加事件……感觉废话……对!还有,获取fragment的management,一句话就好了。在继承了activity的活动中使用getSupportFragmentManager();返回结果就是FragmentManager。然后关于viewpager还有一个很重要的方法setcurrentItem(int position),设置开始的时候使用哪个view。关于adapter还有一个常用的方法就是更新数据fragmentAdapter.notifyDataSetChanged();

浙公网安备 33010602011771号