package com.activities; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; import com.fragments.MyListFragment; import com.fragments.MyListFragment.OnListListener; import android.annotation.SuppressLint; import android.app.Activity; import android.app.FragmentManager; import android.os.Bundle; import android.widget.ArrayAdapter; @SuppressLint("NewApi") public class MainActivity extends Activity implements OnListListener{ private ArrayAdapter aa; private ArrayList<String> items; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getFragmentManager(); MyListFragment mf = (MyListFragment) fm.findFragmentById(R.id.MyListFragment); //创建ArrayList items = new ArrayList<String>(); //创建ArrayAdapter用来将数组绑定到listview上 aa = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, items); //将ArrayAdapter绑定到ListView上 mf.setListAdapter(aa); } @Override public void onListListener(String list) { // TODO Auto-generated method stub items.add(list); aa.notifyDataSetChanged(); } @Override public boolean isDestroyed() { // TODO Auto-generated method stub return false; } @Override public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { // TODO Auto-generated method stub } }
package com.fragments; import com.activities.R; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @SuppressLint("NewApi") public class MyListFragment extends ListFragment { private OnListListener mOnListListener; public interface OnListListener{ public void onListListener(String list); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try{ mOnListListener = (OnListListener)getActivity(); }catch(ClassCastException e){ throw new ClassCastException(activity.toString()+"must implements OnListListener."); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.list_fragment, container, false); } }
下面是activity的布局xml:activity_main_xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_container_layout" android:orientation="vertical"> <fragment android:name="com.fragments.DetailsFragment" android:id="@+id/DetailsFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /> <fragment android:name="com.fragments.MyListFragment" android:id="@+id/MyListFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
你的写法好奇怪啊,fragment不应该都是java文件实例的吗,你的为什么是通过id从xml里面找出来的fragment标签
fragment有两种加载方式。一种是代码动态加载,一种是xml布局静态加载。你说的应该是动态加载方式。问题已经处理了,不过感谢您的关注!版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。