一直对Activity viewpager Fragment之间的应用有些问题,今天整理个小而简陋的demo记录下,希望看到的人有所收获,也很希望懂得的朋友给予些好的指点,优化,先谢过!
1.调用FragmentManager fm=getSupportFragmentManager();报错
原因:getSupportFragmentManager()是android.support.v4.app.FragmentActivity的
解决方案:
Activity继承android.support.v4.app.FragmentActivity即可
2.在Activity里有个按钮入点击后Fragment的数据进行排序:之前我走入了歧途在fragment中通过getActivity().findViewById(R.id.btpaixu),然后设置监听,进行相关排序操作
结果当前fragment不起作用,最后一个fragment排了序,
原因不详
解决方案:
在activity中获得按钮控件设置点击监听,在监听方法中获得当前fragment,调用fragment的数据排序方法
代码见下:
3.activity_main.xml
<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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bt_more" >
</android.support.v4.view.ViewPager>
<Button
android:id="@+id/bt_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="加载更多" />
<Button
android:id="@+id/bt_paixu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:text="排序" />
</RelativeLayout>
4.MainActivity.java
package com.example.zfragment;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
ViewPager pager;
ArrayList<ItemFragment> fragments;
private Button btMore;
protected int currentpostion;
private Button btPaiXu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager=(ViewPager)findViewById(R.id.pager);
initViews();
FragmentManager fm=getSupportFragmentManager();
MyViewAdapter adapter=new MyViewAdapter(fm);
pager.setOffscreenPageLimit(fragments.size());
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
currentpostion=arg0;// TODO 保存当前索引
if(fragments.get(arg0)==null){
ItemFragment f=new ItemFragment();
Bundle args = new Bundle();
args.putInt("type",arg0);
f.setArguments(args);
fragments.add(f);
}else{
ItemFragment f=fragments.get(arg0);
// TODO 如果是切换再去加载数据可以在此处调用fragment的加载数据的方法
//f.loadData();
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// TODO 设置MainActivity监听,fragment进行相关的操作
setMoreListener();
}
private void setMoreListener() {
btMore= (Button) findViewById(R.id.bt_more);
btMore.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "过几秒向上滑动可以看到数据", 5000).show();
fragments.get(currentpostion).loadMore();//加载更多的方法
}
});
btPaiXu= (Button) findViewById(R.id.bt_paixu);
btPaiXu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
fragments.get(currentpostion).paixu();//调用fragment的排序方法
}
});
}
private void initViews() {
fragments=new ArrayList<ItemFragment>();
for (int i = 0; i < 6; i++) {
ItemFragment f=new ItemFragment();
Bundle args = new Bundle();
args.putInt("type",i);
f.setArguments(args);
fragments.add(f);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyViewAdapter extends FragmentPagerAdapter{
public MyViewAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
@Override
public int getCount() {
return fragments.size();
}
}
}
5.item_fragment.xml
<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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
6.ItemFragment.java
package com.example.zfragment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ItemFragment extends Fragment{
private View view;
private ListView listView;
private MyAdapter adapter;
private int type;
protected ArrayList<String> datas;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.item_fragment, container,false);
listView=(ListView) view.findViewById(R.id.listView1);
this.type=getArguments().getInt("type");
return view;
}
protected void loadMore() {
final Handler h=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 2:
ArrayList<String> tmp=(ArrayList<String>) msg.obj;
datas.addAll(datas.size(),tmp);
adapter.notifyDataSetChanged(datas);
break;
default:
break;
}
}
};
new Thread(){
public void run() {
ArrayList<String> datas=new ArrayList<String>();
for (int i = 0; i < 20; i++) {
datas.add("更多"+"--"+type+"--"+i);
}
try {
sleep(3000);
Message msg=Message.obtain(h, 2);
msg.obj=datas;
msg.sendToTarget();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Handler handle=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
datas=(ArrayList<String>) msg.obj;
adapter=new MyAdapter(datas);
listView.setAdapter(adapter);
}
}
};
new Thread(){
public void run() {
ArrayList<String> datas=new ArrayList<String>();
for (int i = 0; i <50; i++) {
datas.add("item"+"--"+type+"--"+i);
}
try {
sleep(3000);
Message msg=Message.obtain(handle, 1);
msg.obj=datas;
msg.sendToTarget();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
class MyAdapter extends BaseAdapter{
ArrayList<String>datas;
public MyAdapter(ArrayList<String>datas){
this.datas=datas;
}
public void notifyDataSetChanged(ArrayList<String> datas2) {
this.datas=datas2;
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return datas.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
TextView tv=new TextView(getActivity());
tv.setText(datas.get(arg0));
return tv;
}
}
public void paixu() {
Collections.sort(datas,new Comparator<String>() {
@Override
public int compare(String arg0, String arg1) {
return arg0.length()-arg1.length();
}
});
adapter.notifyDataSetChanged(datas);
}
}
Activity ViewPager Fragment错误笔记与简单demo
原文地址:http://blog.csdn.net/u013042707/article/details/41979637