首页 > 代码库 > Android Fragment 参数传递与动态布局

Android Fragment 参数传递与动态布局

 1 package org.metrox.drawlayoutdemo; 2  3 import android.os.Bundle; 4 import android.support.v4.app.FragmentManager; 5 import android.support.v4.widget.DrawerLayout; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.View; 8 import android.widget.AdapterView; 9 import android.widget.ArrayAdapter;10 import android.widget.ListView;11 12 import java.util.ArrayList;13 import java.util.Collections;14 import java.util.List;15 16 public class MainActivity extends AppCompatActivity {17 18     private String[] mPlanetTitles;19     private DrawerLayout mDrawerLayout;20     private ListView mDrawerList;21     private CharSequence mTitle;22     private List<String> BackStackTitles;23     FragmentManager fragmentManager;24     @Override25     protected void onCreate(Bundle savedInstanceState) {26         super.onCreate(savedInstanceState);27         setContentView(R.layout.drawlayout);28         BackStackTitles = new ArrayList<String>();29         BackStackTitles.add(getSupportActionBar().getTitle().toString());30         //得到标题集合31         mPlanetTitles = getResources().getStringArray(R.array.planets_array);32         //得到侧滑菜单布局33         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);34         //得到列表项布局35         mDrawerList = (ListView) findViewById(R.id.left_drawer);36 37         //添加列表数据适配器38         mDrawerList.setAdapter(new ArrayAdapter<String>(this,39                 android.R.layout.simple_list_item_1, mPlanetTitles));40         //添加列表项单击事件监听41         mDrawerList.setOnItemClickListener(new DrawerItemClickListener());42 43     }44 45     /*46     *   列表项事件监听47     */48     private class DrawerItemClickListener implements ListView.OnItemClickListener {49         @Override50         public void onItemClick(AdapterView parent, View view, int position, long id) {51             selectItem(position);52         }53     }54 55     /*自定义点击事件*/56     private void selectItem(int position) {57         //保存标题58         mTitle =  mPlanetTitles[position] ;59         //生成Fragment并替换已存在的Fragment60         CustomeFragment frg = new CustomeFragment();61         List<String> fields =  new ArrayList<String>();62         Collections.addAll(fields,getResources().getStringArray(R.array.class_array));63         fragmentManager = getSupportFragmentManager();64         fragmentManager.beginTransaction()65                 .addToBackStack(mTitle.toString())66                 .replace(R.id.content_frame, CustomeFragment.newInstance(fields))67                 .commit();68 69         BackStackTitles.add(mTitle.toString());70         //高亮显示选中项71         mDrawerList.setItemChecked(position, true);72         //改变标题73         setTitle(mTitle.toString());74         //关闭侧滑菜单75         mDrawerLayout.closeDrawer(mDrawerList);76     }77 78     /*修改标题*/79     @Override80     public void setTitle(CharSequence title) {81         mTitle = title;82         //得到标题栏,并修改标题83         getSupportActionBar().setTitle(mTitle);84     }85 86     //处理后退键事件87     @Override88     public void onBackPressed() {89         super.onBackPressed();90         //得到暂时存放FRAGMENT的回退栈的数量91         int count = getSupportFragmentManager().getBackStackEntryCount();92         //因为栈是先进后出的原理,所以当前数量就是现在后退的那个FRAGMENT93         setTitle(BackStackTitles.get(count));94     }95 }
 1 package org.metrox.drawlayoutdemo; 2  3 import android.graphics.Color; 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.support.v4.app.Fragment; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup;10 import android.widget.ArrayAdapter;11 import android.widget.Button;12 import android.widget.ListView;13 import android.widget.Toast;14 15 import java.util.ArrayList;16 import java.util.List;17 public class CustomeFragment extends Fragment {18 19     ArrayAdapter adapter;20     //实例化带参数Fragment21     public static CustomeFragment newInstance(List<String> fields) {22         CustomeFragment fragment = new CustomeFragment();23         Bundle args = new Bundle();24         args.putStringArrayList("fields", (ArrayList<String>) fields);25         fragment.setArguments(args);26         return fragment;27     }28 29     @Override30     public void onCreate(@Nullable Bundle savedInstanceState) {31         super.onCreate(savedInstanceState);32         if(getArguments() != null){33             List<String> fields = getArguments().getStringArrayList("fields");34             adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,fields);35 36         }37     }38 39     @Nullable40     @Override41     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {42         //inflate第二个参数为容器,如果布局文件中已有FrameLayout,此处应为null43         //不然会报RemoveView错误44         View v =   inflater.inflate(R.layout.basefragment,null);45         ListView  lv = (ListView) v.findViewById(R.id.lv);46         lv.setAdapter(adapter);47         return v;48     }49 50 51     private  List<View> initControls(String title) {52         List<View> views = new ArrayList<View>();53         Button btn = new Button(getActivity());54         btn.setText(title);55         btn.setTextColor(Color.RED);56         btn.setBackgroundColor(Color.YELLOW);57         btn.setOnClickListener(new View.OnClickListener() {58             @Override59             public void onClick(View v) {60                 Toast.makeText(v.getContext(),61                         ((Button)v).getText(), Toast.LENGTH_SHORT).show();62             }63         });64         views.add(btn);65         return views;66     }67 68 69     @Override70     public void onDestroy() {71         super.onDestroy();72     }73 74     @Override75     public void onDestroyView() {76         super.onDestroyView();77     }78 79 }

技术分享

技术分享

Android Fragment 参数传递与动态布局