UI框架结构图
相互传递数据的时候用这张图看,思路会比较清晰
1. 侧边栏布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >//背景黑色
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//没有点击时的那个效果,变成透明的了,写@null也行
android:listSelector="@android:color/transparent"
//分割条透明
android:divider="@android:color/transparent"
- //上边远点,要不然不好看
android:layout_marginTop="40dp" />
</RelativeLayout>
listview的子项布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/btn_menu_selector"
android:drawablePadding="5dp"
android:enabled="false"
android:text="新闻"
android:textColor="@drawable/text_menu_selector"
android:textSize="25sp" />
</LinearLayout>
文字旁边的状态选择器,写成enable,在代码中如何使用,看后面
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/menu_arr_select"/>
<item android:drawable="@drawable/menu_arr_normal"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#f00"/>
<item android:color="@android:color/white"/>
</selector>
2.侧边栏
public class LeftMenuFragment extends BaseFragment {
@ViewInject(R.id.lv_list)//注解的使用
private ListView lvList;
private ArrayList<NewsMenuData> mMenuList;//这在
分装JSON数据的分装类中private int mCurrentPos;// 当前被点击的菜单项
private MenuAdapter mAdapter;
@Override
public View initViews() {
View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
ViewUtils.inject(this, view);
return view;
}
@Override
public void initData() {
lvList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mCurrentPos = position;
mAdapter.notifyDataSetChanged();//刷新,然后会调用adapter的GetView方法
setCurrentMenuDetailPager(position);//
设置当前菜单详情页toggleSlidingMenu();// 隐藏
}
});
}
/**
* 切换SlidingMenu的状态
*
* @param b
*/
protected void toggleSlidingMenu() {
MainActivity mainUi = (MainActivity) mActivity;//拿到mainactivity
SlidingMenu slidingMenu = mainUi.getSlidingMenu();//拿到slidingmenu对象
slidingMenu.toggle();// 切换状态, 显示时隐藏, 隐藏时显示
}
/**
* 设置当前菜单详情页
*
* @param position
*/
protected void setCurrentMenuDetailPager(int position) {
MainActivity mainUi = (MainActivity) mActivity;
ContentFragment fragment = mainUi.getContentFragment();// 获取主页面fragment,所以main需要这个方法
// 获取侧边栏fragment
public LeftMenuFragment getLeftMenuFragment() {
FragmentManager fm = getSupportFragmentManager();
LeftMenuFragment fragment = (LeftMenuFragment) fm
.findFragmentByTag(FRAGMENT_LEFT_MENU);
return fragment;
}
// 获取主页面fragment
public ContentFragment getContentFragment() {
FragmentManager fm = getSupportFragmentManager();
ContentFragment fragment = (ContentFragment) fm
.findFragmentByTag(FRAGMENT_CONTENT);
return fragment;
}
NewsCenterPager pager = fragment.getNewsCenterPager();// 获取新闻中心页面,所以在contentFargment也需要这个方法,这样一步一步的就拿个了
NewsCenterPager 的方法
// 获取新闻中心页面
public NewsCenterPager getNewsCenterPager() {
return (NewsCenterPager) mPagerList.get(1);
}
pager.setCurrentMenuDetailPager(position);// 设置当前菜单详情页
}
// 设置网络数据
public void setMenuData(NewsData data) {
// System.out.println("侧边栏拿到数据啦:" + data);
mMenuList = data.data;//这个方法在新闻中心调用了,给侧便栏设置页面
mAdapter = new MenuAdapter();
lvList.setAdapter(mAdapter);
}
/**
* 侧边栏数据适配器
*/
class MenuAdapter extends BaseAdapter {
@Override
public int getCount() {
return mMenuList.size();
}
@Override
public NewsMenuData getItem(int position) {
return mMenuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(mActivity, R.layout.list_menu_item, null);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
- //下面可以这样写,不过前提是让getItem方法返回NewsMenuData,也可以正常的写mMenuList.get(position)
NewsMenuData newsMenuData = getItem(position);
tvTitle.setText(newsMenuData.title);
if (mCurrentPos == position) {// 判断当前绘制的view是否被选中
// 显示红色
tvTitle.setEnabled(true);
} else {
// 显示白色
tvTitle.setEnabled(false);
}
return view;
}
}
}
时间: 2024-11-11 06:47:30