学习fragment遇到的问题

龙龙back 发布于 2016/04/18 21:24
阅读 100
收藏 0
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类和其中一个Fragment类

下面是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>



下面是报错:




加载中
0
飞翔的大骆驼
飞翔的大骆驼

你的写法好奇怪啊,fragment不应该都是java文件实例的吗,你的为什么是通过id从xml里面找出来的fragment标签

龙龙back
fragment有两种加载方式。一种是代码动态加载,一种是xml布局静态加载。你说的应该是动态加载方式。问题已经处理了,不过感谢您的关注!
返回顶部
顶部