fragment_content.xml代码:
item_list.xml代码:
activity_main.xml代码:
package com.itcast.a4_6;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
private FragmentTransaction beginTransaction;
//设置文字
private String[] settingText = {"" +
"1.将鸡蛋清和淀粉调料调匀成糊,涂抹在肉片上。\n" +
"2.将花椒、干辣椒慢火炸,待辣椒呈金黄色捞出切成细末。\n" +
"3.用锅中油爆炒豆瓣辣酱,然后将白菜叶,调料放入。\n" +
"4.随即放入 肉片,再炖几分钟,肉片熟后,将肉片盛起,将辣椒、花椒末撒上。\n" +
"5.用植物油烧开,淋在肉片上,即可使麻、辣、浓香四溢。",
"1、豆腐切丁,香葱、生姜、大蒜、干辣椒切细末备用。\n" +
"2、锅内放入油烧热, 先爆香葱末、生姜末、大蒜末、干辣椒末和豆瓣酱,再放入猪肉馅炒熟。\n" +
"3、加入适量水,煮开后加入豆腐丁、酱油、白糖煮3分钟。\n" +
"4、再用水淀粉勾芡后盛入盘中。\n" +
"5、烧热香油,爆香花椒,将花椒油淋在豆腐上即可。\n"};
//设置图标
private int[] settingicons = { R.drawable.boiledmeat,R.drawable.mapoytofu};
private String[] foodNames = {"水煮肉片","麻婆豆腐"};
//获取图标数组的方法
public int[] getIcons() {
return settingicons;
}
//获取设置菜品名称的方法
public String[] getNames() {
return foodNames;
}
//获取设置文字的方法
public String[] getSettingText() {
return settingText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建Fragment
ContentFragment contentFragment = new ContentFragment();
MenuFragment menuFragment = new MenuFragment();
//获取事务
beginTransaction = getFragmentManager().beginTransaction();
//添加Fragment
beginTransaction.replace(R.id.foodcontent, contentFragment);
beginTransaction.replace(R.id.menu, menuFragment);
//提交事务
beginTransaction.commit();
}
}
ContentFragment.java代码:
package com.itcast.a4_6;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ContentFragment extends Fragment {
private View view;
private TextView mContent;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//将布局文件解析出来
view = inflater.inflate(R.layout.fragment_content, container, false);
if(view != null){ //如果view不为空
initView();
}
//获取Activity中的设置文字
setText(((MainActivity)getActivity()).getSettingText()[0]);
return view;
}
public void initView(){
mContent = (TextView) view.findViewById(R.id.content);
}
public void setText(String text){
mContent.setText(text);
}
}
MenuFragment.java代码:
package com.itcast.a4_6;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MenuFragment extends Fragment {
private View view;
private int[] settingicon;
private String[] foodNames;
private String[] settingText;
private ListView mListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//解析布局
view = inflater.inflate(R.layout.fragment_menu, container, false);
//获取Acitivty实例对象
MainActivity activity = (MainActivity) getActivity();
//获取Activity中的图标数组
settingicon = activity.getIcons();
//获取Activity中定义的川菜名称
foodNames = activity.getNames();
//获取Activity中的设置文字数组
settingText = activity.getSettingText();
if (view != null) { // 如果view不为空
initView();
}
//为ListView设置条目监听
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,int position,
long id) {
//点击左侧的川菜列表item,右侧菜品做法内容相应改变逻辑
//通过Activity实例获取另一个Fragment实例
ContentFragment listFragment = (ContentFragment)((MainActivity)
getActivity()).getFragmentManager().findFragmentById(R.id.foodcontent);
//设置菜品类表点击位置对应的菜品做法的Fragment文字
listFragment.setText(settingText[position]);
}
});
return view;
}
//初始化控件的方法
private void initView() {
mListView = (ListView) view.findViewById(R.id.menulist);
if (settingicon != null) {
mListView.setAdapter(new MyAdapter());
}
}
//适配器
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return settingicon.length;
}
@Override
public Object getItem(int position) {
return settingicon[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(getActivity(), R.layout.item_list, null);
ImageView mNameTV = (ImageView) convertView
.findViewById(R.id.food_icon);
mNameTV.setBackgroundResource(settingicon[position]);
TextView mFoodName = (TextView) convertView.findViewById
(R.id.food_name);
mFoodName.setText(foodNames[position]);
return convertView;
}
}
}
MainActivity.java代码:
package com.itcast.a4_6;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
private FragmentTransaction beginTransaction;
//设置文字
private String[] settingText = {"" +
"1.将鸡蛋清和淀粉调料调匀成糊,涂抹在肉片上。\n" +
"2.将花椒、干辣椒慢火炸,待辣椒呈金黄色捞出切成细末。\n" +
"3.用锅中油爆炒豆瓣辣酱,然后将白菜叶,调料放入。\n" +
"4.随即放入 肉片,再炖几分钟,肉片熟后,将肉片盛起,将辣椒、花椒末撒上。\n" +
"5.用植物油烧开,淋在肉片上,即可使麻、辣、浓香四溢。",
"1、豆腐切丁,香葱、生姜、大蒜、干辣椒切细末备用。\n" +
"2、锅内放入油烧热, 先爆香葱末、生姜末、大蒜末、干辣椒末和豆瓣酱,再放入猪肉馅炒熟。\n" +
"3、加入适量水,煮开后加入豆腐丁、酱油、白糖煮3分钟。\n" +
"4、再用水淀粉勾芡后盛入盘中。\n" +
"5、烧热香油,爆香花椒,将花椒油淋在豆腐上即可。\n"};
//设置图标
private int[] settingicons = { R.drawable.boiledmeat,R.drawable.mapoytofu};
private String[] foodNames = {"水煮肉片","麻婆豆腐"};
//获取图标数组的方法
public int[] getIcons() {
return settingicons;
}
//获取设置菜品名称的方法
public String[] getNames() {
return foodNames;
}
//获取设置文字的方法
public String[] getSettingText() {
return settingText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建Fragment
ContentFragment contentFragment = new ContentFragment();
MenuFragment menuFragment = new MenuFragment();
//获取事务
beginTransaction = getFragmentManager().beginTransaction();
//添加Fragment
beginTransaction.replace(R.id.foodcontent, contentFragment);
beginTransaction.replace(R.id.menu, menuFragment);
//提交事务
beginTransaction.commit();
}
}
实现效果如下:
原创文章 12获赞 18访问量 404
关注
私信
展开阅读全文
作者:MrSuper王