android回调接口笔记

上图:




布局文件:

<LinearLayout 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:orientation="horizontal"
    android:id="@+id/main"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/type"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7" >
    </RelativeLayout>

</LinearLayout>

加载fragment:

//把fragment加载进来
	private void initFrag() {
		fragmentManager = getFragmentManager();
		enSureTransition();
		contentFrag = new ContentFrag();
		typeFrag = new TypeFrag();
		//这里是最关键的一步,将左侧菜单的一个 onTypeChangeListener设置为右侧列表
		//也就是说左侧菜单发生变化的时候,右侧列表也会相应变化
		typeFrag.setOnTypeChangeListener(contentFrag);
		//添加两个fragment
		beginTransaction.add(R.id.content, contentFrag);
		beginTransaction.add(R.id.type, typeFrag);
		beginTransaction.commit();
	}

第一个fragment:

public class TypeFrag extends Fragment implements OnItemClickListener {
	private View view;
	private int resource = R.layout.type_frag;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	//定义一个接口的对象,作为成员变量
	private OnTypeChangeListener onTypeChangeListener;

	public OnTypeChangeListener getOnTypeChangeListener() {
		return onTypeChangeListener;
	}

	public void setOnTypeChangeListener(OnTypeChangeListener listener) {
		this.onTypeChangeListener = listener;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(resource, null);
		findViewsByIds();
		initListView();
		return view;
	}

	private void findViewsByIds() {
		listView = (ListView) view.findViewById(R.id.type_list_view);
	}

	//因为左侧的数据源不会变动,所以这里直接用数组作为菜单内容
	private void initListView() {
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1,
				AppConstant.ListViewHelper.types);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	//内部定义一个接口,当左侧菜单被点击,也就是type发生变化的时候,会调用这个接口里的方法
	//方便右侧列表的数据源修改,这里的参数是整型
	public interface OnTypeChangeListener {
		public void onTypeChange(int type);
	}

	//当左侧菜单被点击
	//接口(也就是右侧的列表)会调用onTypeChange方法,传递的参数为菜单的index(或者说order)
	//至于这个方法的具体内容,在这实现了这个OnTypeChangeListener接口的类中实现,达到了回调的目的
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent.equals(listView)) {
			onTypeChangeListener.onTypeChange(AppConstant.ListViewHelper.intTypes[position]);
		}
	}
}

第二个fragment:

//实现OnTypeChangeListener这个接口
public class ContentFrag extends Fragment implements OnTypeChangeListener {
	private View view;
	private int resource = R.layout.content_frag;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	private List<String> contents = new ArrayList<String>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(resource, null);
		findViewsByIds();
		initListView();
		return view;
	}
	
	//数据源都在AppConstant这个静态类中
	//这里将数据源绑定在一个从数组转换过来的List中,方便修改数据源。
	private void initListView() {
		for (int i = 0; i < AppConstant.ListViewHelper.moblies.length; i++) {
			contents.add(AppConstant.ListViewHelper.moblies[i]);
		}
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, contents);
		listView.setAdapter(adapter);
	}

	//响应onTypeChange方法的时候,设置列表的数据源
	private void setListViewContent(int types) {
		contents.clear();
		switch (types) {
		case AppConstant.ListViewHelper.MOBLIE:
			for (int i = 0; i < AppConstant.ListViewHelper.moblies.length; i++) {
				contents.add(AppConstant.ListViewHelper.moblies[i]);
			}
			break;
		case AppConstant.ListViewHelper.LANGUAGE:
			for (int i = 0; i < AppConstant.ListViewHelper.languages.length; i++) {
				contents.add(AppConstant.ListViewHelper.languages[i]);
			}
			break;
		default:
			break;
		}
		adapter.notifyDataSetChanged();
	}

	private void findViewsByIds() {
		listView = (ListView) view.findViewById(R.id.content_list_view);
	}

	//这是实现OnTypeChangeListener接口覆写的方法
	public void onTypeChange(int type) {
		this.setListViewContent(type);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值