Android - Fragment
MeituanListAdapter.java
1 package com.zy.project.android.adapter; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.graphics.Color; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.TextView; 11 12 public class MeituanListAdapter extends BaseAdapter{ 13 14 Context context; 15 List<String> list; 16 int selectedPosition = -1; 17 public MeituanListAdapter(Context context,List<String> list) { 18 // TODO Auto-generated constructor stub 19 this.context= context; 20 this.list = list; 21 } 22 @Override 23 public int getCount() { 24 // TODO Auto-generated method stub 25 return list==null?0:list.size(); 26 } 27 28 @Override 29 public Object getItem(int position) { 30 // TODO Auto-generated method stub 31 return list.get(position); 32 } 33 34 @Override 35 public long getItemId(int position) { 36 // TODO Auto-generated method stub 37 return 0; 38 } 39 40 public void setSelectedPosition(int selected){ 41 selectedPosition = selected; 42 notifyDataSetChanged(); 43 } 44 public void refresh(List<String> list){ 45 this.list = list; 46 notifyDataSetChanged(); 47 } 48 @Override 49 public View getView(int position, View convertView, ViewGroup parent) { 50 // TODO Auto-generated method stub 51 TextView tv = new TextView(context); 52 tv.setText(list.get(position)); 53 tv.setTextSize(20); 54 tv.setPadding(40, 40, 40, 40); 55 if(selectedPosition == position){ 56 tv.setBackgroundColor(Color.RED); 57 }else{ 58 tv.setBackgroundColor(Color.WHITE); 59 } 60 return tv; 61 } 62 63 }
MeiTuanFragment.java
1 package com.zy.project.android.fragment; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.List; 6 7 import com.zy.project.android.R; 8 import com.zy.project.android.adapter.MeituanListAdapter; 9 10 import android.content.Context; 11 import android.os.Bundle; 12 import android.support.v4.app.ListFragment; 13 import android.view.LayoutInflater; 14 import android.view.View; 15 import android.view.ViewGroup; 16 import android.widget.ListView; 17 18 public class MeiTuanFragment extends ListFragment { 19 20 MeituanListAdapter adapter; 21 Context context; 22 List<String> list; 23 int level = 1; 24 int type = R.array.all_types; 25 OnListItemClickCallBack callBack; 26 27 public void setLevel(int level) { 28 this.level = level; 29 } 30 31 public void setArrayType(int arrayRes) { 32 this.type = arrayRes; 33 if (adapter != null) { 34 adapter.setSelectedPosition(-1); 35 addList(); 36 adapter.refresh(list); 37 } 38 } 39 40 public void setOnListItemClickCallBack(OnListItemClickCallBack back) { 41 callBack = back; 42 } 43 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 // TODO Auto-generated method stub 47 super.onCreate(savedInstanceState); 48 context = getActivity(); 49 } 50 51 private void addList() { 52 if(type == -1){ 53 list = new ArrayList<String>(); 54 }else{ 55 String[] arr = getResources().getStringArray(type); 56 list = Arrays.asList(arr); 57 } 58 } 59 60 @Override 61 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 62 // TODO Auto-generated method stub 63 addList(); 64 adapter = new MeituanListAdapter(context, list); 65 setListAdapter(adapter); 66 67 return super.onCreateView(inflater, container, savedInstanceState); 68 } 69 70 // item 点击事件 71 @Override 72 public void onListItemClick(ListView l, View v, int position, long id) { 73 // TODO Auto-generated method stub 74 super.onListItemClick(l, v, position, id); 75 String title = (String) l.getItemAtPosition(position); 76 int arrRes = 0; 77 switch (position) { 78 case 0: 79 arrRes = R.array.city_array; 80 break; 81 case 1: 82 arrRes = R.array.type_food; 83 break; 84 default: 85 arrRes = R.array.type_hotel; 86 break; 87 } 88 adapter.setSelectedPosition(position); 89 if (callBack != null) { 90 callBack.itemClick(level, title, arrRes); 91 } 92 } 93 94 public static interface OnListItemClickCallBack { 95 /** 96 * 97 * @param level 98 * 当前Fragment的等级 99 * @param title 100 * 当前Fragment显示的信息 101 * @param arrRes 102 * 下一级 Fragment需要显示的 数据 103 */ 104 public void itemClick(int level, String title, int arrRes); 105 } 106 }
FragmentActivity.java
1 package com.zy.project.android.activity; 2 3 import com.zy.project.android.R; 4 import com.zy.project.android.fragment.MeiTuanFragment; 5 import com.zy.project.android.fragment.MeiTuanFragment.OnListItemClickCallBack; 6 7 import android.os.Bundle; 8 import android.support.v4.app.FragmentActivity; 9 import android.support.v4.app.FragmentManager; 10 import android.support.v4.app.FragmentTransaction; 11 import android.view.View; 12 import android.widget.LinearLayout; 13 import android.widget.TextView; 14 15 public class BusinessActivity extends FragmentActivity implements OnListItemClickCallBack{ 16 17 TextView tvResult;//显示用户选择的结果 18 LinearLayout layout;//需要显示隐藏的容器 19 FragmentManager manager; 20 FragmentTransaction tran; 21 MeiTuanFragment fragmentLevel1; 22 MeiTuanFragment fragmentLevel2; 23 24 @Override 25 protected void onCreate(Bundle arg0) { 26 // TODO Auto-generated method stub 27 super.onCreate(arg0); 28 setContentView(R.layout.business_layout); 29 tvResult = (TextView) findViewById(R.id.tvResult); 30 layout = (LinearLayout) findViewById(R.id.layout); 31 manager = getSupportFragmentManager(); 32 } 33 //显示Fragment 34 public void show(View view){ 35 layout.setVisibility(View.VISIBLE); 36 if(fragmentLevel1==null){ 37 fragmentLevel1 = new MeiTuanFragment(); 38 addFragment(fragmentLevel1, R.id.ly1, 1); 39 40 } 41 //清空二级菜单 42 if(fragmentLevel2!=null){ 43 fragmentLevel1.setArrayType(R.array.all_types); 44 fragmentLevel2.setArrayType(-1); 45 } 46 } 47 private void addFragment(MeiTuanFragment fragment,int id,int level){ 48 fragment.setLevel(level); 49 fragment.setOnListItemClickCallBack(this); 50 tran = manager.beginTransaction(); 51 tran.add(id, fragment); 52 tran.commit(); 53 } 54 String resuslt; 55 @Override 56 public void itemClick(int level, String title, int arrRes) { 57 // TODO Auto-generated method stub 58 if(level== 1){ 59 if(fragmentLevel2 ==null){ 60 fragmentLevel2 = new MeiTuanFragment(); 61 addFragment(fragmentLevel2, R.id.ly2, 2); 62 } 63 fragmentLevel2.setArrayType(arrRes); 64 resuslt = title; 65 }else if(level ==2){ 66 layout.setVisibility(View.GONE); 67 resuslt +=title; 68 tvResult.setText(resuslt); 69 } 70 } 71 }