接口回调
来源:互联网 发布:crossover破解版mac 编辑:程序博客网 时间:2024/05/20 04:10
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <fragment android:id="@+id/fragment1" android:name="zhengshuai.baway.com.myapplicationabc.Fragment1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/holo_red_light"></fragment> <fragment android:id="@+id/fragment2" android:name="zhengshuai.baway.com.myapplicationabc.Fragment2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/holo_green_light"></fragment></LinearLayout>
<ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
public class Fragment1 extends Fragment { private ListView lv; private List<String> list = new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, null); getData(); //找控件 initView(view); return view; } private void getData() { for (int i = 0; i < 10; i++) { list.add("条目:" + i); } } private void initView(View view) { lv = (ListView) view.findViewById(R.id.lv); ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (listener != null) { listener.onFragmentClick(list.get(position)); } } }); } //声明一个接口 private OnFragmentClickListener listener; //定义接口 方法 public interface OnFragmentClickListener { void onFragmentClick(String item); } //对外提供一个访问的方法 public void setOnFragmentClickListener(OnFragmentClickListener listener) { this.listener = listener; }}
public class Fragment2 extends Fragment { private TextView tv; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, null); //找控件 initView(view); return view; } private void initView(View view) { tv = (TextView) view.findViewById(R.id.tv); //fragment管理器 FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); //得到Fragment1 Fragment1 fragment1 = (Fragment1) fragmentManager.findFragmentById(R.id.fragment1); //实现点击事件 fragment1.setOnFragmentClickListener(new Fragment1.OnFragmentClickListener() { @Override public void onFragmentClick(String item) { tv.setText("接口回调:" + item); } }); }}
0 0
- 接口,接口回调
- android 接口 接口回调
- 接口与接口回调
- 接口与实现:接口回调与接口多态
- Java 接口参数,接口回调.
- Fragment接口回调 三 使用接口
- 接口回调
- Java:接口回调
- 接口回调
- 什么是接口回调?
- 接口回调
- java 回调接口
- 接口回调
- 接口回调
- 回调接口
- java接口回调
- 什么是接口回调?
- 接口回调
- 机器学习各算法简介
- pdf编辑器中添加贝茨编码
- 移动APP测试实用指南
- sql server2008R2安装记录
- CSDN停更,请移步https://zh1995.github.io/
- 接口回调
- 平衡二叉树原理
- 子串和
- Unity调用c++动态链接库注意事项
- img在div内水平垂直居中
- layer tips 宽和高自定义
- java函 数
- 桥接模式
- 【Java学习笔记】get(),set()方法