本文介绍了ListFragment和onListItemClick不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的选项卡,在一个选项卡有一个 ListFragment onListItemClick 。当你点击一个项目另一个 ListFragment 显示所以这个工作正常。同样 onListItemClick 作为一个在第一次 ListFragment 的实施是为第二个 ListFragment

编辑:这是我试图改变CustomTabListener:

 公共类CustomTabListener<吨延伸片断>实现TabListener {私人片段mFragment;
私人最终活动mActivity;
私人最终字符串MTAG;
私人最终级< T> mClass;
私人最终捆绑mArgs;
公共CustomTabListener(活动活动,字符串变量,类< T> CLZ){
    这个(活动,吊牌,CLZ,NULL);
}公共CustomTabListener(活动活动,字符串变量,类< T> CLZ,捆绑参数){
    mActivity =活动;
    MTAG =标签;
    mClass = CLZ;
    mArgs = ARGS;
    //检查,看看我们是否已经为这个标签片段,可能
    //从previously保存的状态。如果是这样,禁用它,因为我们
    //初始状态是一个选项卡不显示。
    mFragment = mActivity.getFragmentManager()findFragmentById(android.R.id.content)。
    如果(mFragment =空&放大器;!&放大器;!mFragment.isDetached()){
        FragmentTransaction英尺= mActivity.getFragmentManager()调用BeginTransaction()。
        ft.hide(mFragment);
        ft.commit();
    }
}@覆盖
公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
}@覆盖
公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){    如果(mFragment == NULL){
        mFragment = Fragment.instantiate(mActivity,mClass.getName(),mArgs);
        ft.add(android.R.id.content,mFragment,MTAG);
    }其他{
        ft.show(mFragment);
    }
}@覆盖
公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
。mActivity.getFragmentManager()findFragmentByTag(MTAG);    如果(mFragment!= NULL){
        ft.hide(mFragment);
        }
    }
}

现在应用程序崩溃,如果我在第一次单击列表上的项目。 LogCat中说:

  04-24 16:00:37.161:E / AndroidRuntime(9421):致命异常:主要
04-24 16:00:37.161:E / AndroidRuntime(9421):java.lang.ClassCastException:com.example.easysave.InfoFragment不能转换为android.app.ListFragment
04-24 16:00:37.161:E / AndroidRuntime(9421):在com.example.easysave.KategorierFragment.onListItemClick(KategorierFragment.java:66)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.app.ListFragment $ 2.onItemClick(ListFragment.java:160)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.widget.AbsListView.performItemClick(AbsListView.java:1100)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2749)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.widget.AbsListView $ 1.run(AbsListView.java:3423)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.os.Handler.handleCallback(Handler.java:725)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.os.Handler.dispatchMessage(Handler.java:92)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.os.Looper.loop(Looper.java:137)
04-24 16:00:37.161:E / AndroidRuntime(9421):在android.app.ActivityThread.main(ActivityThread.java:5041)
04-24 16:00:37.161:E / AndroidRuntime(9421):在java.lang.reflect.Method.invokeNative(本机方法)
04-24 16:00:37.161:E / AndroidRuntime(9421):在java.lang.reflect.Method.invoke(Method.java:511)
04-24 16:00:37.161:E / AndroidRuntime(9421):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 16:00:37.161:E / AndroidRuntime(9421):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 16:00:37.161:E / AndroidRuntime(9421):在dalvik.system.NativeStart.main(本机方法)


我有两个问题:


  1. 当我点击了一个项目的第一个 ListFragment 显示,然后单击另一个选项卡,列表不会隐藏。


  2. (解决)当我点击了一个项目的第一个 ListFragment 显示,然后点击其他 ListFragment 应用程序崩溃。


我想什么时候有第二个 ListFragment 点击是简单的文本片段在里面,不是一个列表。

难道我这个做了错误的方式?谁知道?

这是解决问题2:

在主发表意见的setContentView

改变所有的ListFragment电流=(ListFragment)fm.findFragmentByTag(getTag());和ft.add(R.id.Frame,mFragment);到Listfragment电流=(ListFragment)fm.findFragmentById(android.R.id.content);和ft.add(android.R.id.content,mFragment);

终于改变这一在PlaneraFragment:

 公共类PlaneraFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    / **充气布局country_details_fragment_layout视图对象V * /
    视图V = inflater.inflate(R.layout.planera_fragment,NULL);    / **获取布局的TextView的对象来设置的细节* /
    TextView的电视=(TextView中)v.findViewById(R.id.text_planera);    / **获取点击的项目的位置,并在详细片段的TextView的设置相应的细节* /
    tv.setText(HeJ小鼠);    返回伏;
    }}

我的code:

主要

 包com.example.easysave;进口android.os.Bundle;
进口android.app.ActionBar;
进口android.app.Activity;
进口android.app.ActionBar.Tab;公共类MainActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    actionBar.setDisplayShowTitleEnabled(真);    TAB键的话= actionBar.newTab()的setText(Kategorier)
        .setTabListener(新CustomTabListener< KategorierFragment>(这一点,kategorier,KategorierFragment.class));
    actionBar.addTab(标签);    标签= actionBar.newTab()的setText(PROFIL)
        .setTabListener(新CustomTabListener< ProfilFragment>(这一点,PROFIL,ProfilFragment.class));
    actionBar.addTab(标签);    标签= actionBar.newTab()的setText(信息)
        .setTabListener(新CustomTabListener< InfoFragment>(这一点,信息,InfoFragment.class));
    actionBar.addTab(标签);
    }
}

KategorierFragment

 包com.example.easysave;进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.app.FragmentTransaction;
进口android.app.ListFragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类KategorierFragment扩展ListFragment {字符串kategorier [] =新的String [] {
         垫,厄尔尼诺,里索,Aktiviteter,Annat};
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity()getBaseContext(),android.R.layout.simple_selectable_list_item,kategorier);    setListAdapter(适配器);
返回super.onCreateView(充气器,容器,savedInstanceState);
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    // TODO自动生成方法存根
    super.onListItemClick(L,V,位置ID);    片段mFragment =新KategorierFragment();
    开关(位置){
    情况下0:
        mFragment =新MatFragment();
        打破;
    情况1:
        mFragment =新ElFragment();
        打破;
    案例2:
        mFragment =新ResorFragment();
        打破;
    案例3:
        mFragment =新AktiviteterFragment();
        打破;
    情况4:
        mFragment =新AnnatFragment();
        打破;    }    FragmentManager FM = getFragmentManager();
    FragmentTransaction英尺= fm.beginTransaction();
    ListFragment电流=(ListFragment)fm.findFragmentByTag(getTag());    ft.add(R.id.Frame,mFragment);
    ft.addToBackStack(NULL);
    ft.hide(电流);
    ft.commit();
}@覆盖
公共无效调用onStart(){
    super.onStart();
    。getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

MatFragment

 包com.example.easysave;进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.app.FragmentTransaction;
进口android.app.ListFragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类MatFragment扩展ListFragment {字符串垫[] = {Planera,Storkok};@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根     ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity()getBaseContext(),android.R.layout.simple_selectable_list_item,垫);
     setListAdapter(适配器);
    返回super.onCreateView(充气器,容器,savedInstanceState);
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    // TODO自动生成方法存根
    super.onListItemClick(L,V,位置ID);    片段mFragment =新MatFragment();
    开关(位置){
    情况下0:
        mFragment =新PlaneraFragment();
        打破;
    情况1:
        mFragment =新StorkokFragment();
        打破;
    }    FragmentManager FM = getFragmentManager();
    FragmentTransaction英尺= fm.beginTransaction();
    ListFragment电流=(ListFragment)fm.findFragmentByTag(getTag());
    如果(当前== NULL)
    ft.add(R.id.Frame,mFragment);
    ft.addToBackStack(NULL);
    ft.hide(电流);
    ft.commit();
}@覆盖
公共无效调用onStart(){
    super.onStart();
    。getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

CustonTabListener

 包com.example.easysave;进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.app.ActionBar.Tab;
进口android.app.ActionBar.TabListener;公共类CustomTabListener<吨延伸片断>实现TabListener {// deklarerar variabler毫米
私人片段mFragment;
私人最终活动mActivity;
私人最终字符串MTAG;
私人最终级< T> mClass;
公共CustomTabListener(活动活动,字符串变量,类< T> CLZ){
    mActivity =活动;
    MTAG =标签;
    mClass = CLZ;
}@覆盖
公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
}@覆盖
公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
    如果(mFragment == NULL){
        mFragment = Fragment.instantiate(mActivity,mClass.getName());
        ft.add(R.id.Frame,mFragment,MTAG);
    }其他{
        ft.attach(mFragment);
    }
}@覆盖
公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){    如果(mFragment!= NULL)
        ft.detach(mFragment);
    }
}

PlaneraFragment

 包com.example.easysave;进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;公共类PlaneraFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根
    查看查看= inflater.inflate(R.layout.planera_fragment,集装箱,FALSE);    TextView的文本=(TextView的)getActivity()findViewById(R.id.text_planera)。
    text.setText(你好!);    返回视图。
    }}

planera_fragment

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直><的TextView
    机器人:textIsSelectable =假
    机器人:ID =@ + ID / text_planera
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直/>< / LinearLayout中>


解决方案

您需要弄清楚你应该如何处理你的标签。您要附加在你的TabListener分离,但显示和隐藏通过你的ListView点击时。

您要么脱离当前所有的碎片,然后装上新的,或更改标签听众.show()和.hide()代替。

编辑:根据最后的评论,有一个很好的例子保持你的标签,而历史标签之间的切换。

TabListener的下面的实现很简单,但不会让你的背部栈完好无损。当切换到另一个选项卡,你不选择一个藏身。这只是釉在它和分离了整个事情。

  @覆盖
公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
     mFragment = Fragment.instantiate(mActivity,mClass.getName());
     ft.replace(android.R.id.content,mFragment,MTAG);
}@覆盖
公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
    如果(mFragment!= NULL)
        ft.detach(mFragment);
    }
}

I have tabs, in one tab there is a ListFragment with an onListItemClick. When you click one item another ListFragment is displayed so this works fine. The same onListItemClick as the one for the first ListFragment is implemented to the second ListFragment.

EDIT: this is how i tried to change the CustomTabListener:

public class CustomTabListener<T extends Fragment> implements TabListener {

private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;


public CustomTabListener(Activity activity, String tag, Class<T> clz) {
    this(activity, tag, clz, null);
}

public CustomTabListener(Activity activity, String tag, Class<T> clz, Bundle args){
    mActivity = activity;
    mTag = tag;
    mClass = clz;
    mArgs = args;


    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state.  If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    mFragment = mActivity.getFragmentManager().findFragmentById(android.R.id.content);
    if (mFragment != null && !mFragment.isDetached()) {
        FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
        ft.hide(mFragment);
        ft.commit();
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

    if(mFragment==null){
        mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
        ft.add(android.R.id.content, mFragment, mTag);
    }else{
        ft.show(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
mActivity.getFragmentManager().findFragmentByTag(mTag);

    if(mFragment!=null){
        ft.hide(mFragment);
        }
    }
}

Now the app crashes if I click on an item in the first list. LogCat says:

04-24 16:00:37.161: E/AndroidRuntime(9421): FATAL EXCEPTION: main
04-24 16:00:37.161: E/AndroidRuntime(9421): java.lang.ClassCastException: com.example.easysave.InfoFragment cannot be cast to android.app.ListFragment
04-24 16:00:37.161: E/AndroidRuntime(9421):     at com.example.easysave.KategorierFragment.onListItemClick(KategorierFragment.java:66)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.os.Handler.handleCallback(Handler.java:725)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.os.Looper.loop(Looper.java:137)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 16:00:37.161: E/AndroidRuntime(9421):     at dalvik.system.NativeStart.main(Native Method)


I have two problems:

  1. When I have clicked one item in the first ListFragment being displayed and then click another tab, the list doesn't hide.

  2. (Solved) When I have clicked one item in the first ListFragment being displayed and then click on the other ListFragment the app crashes.

What I would like to have when second ListFragment is clicked is a fragment with simple text in it, not a list.

Am I doing this the wrong way? Anyone who knows?

Problem 2 solved by:

comment the setContentView in main

change all the ListFragment current = (ListFragment) fm.findFragmentByTag(getTag()); and ft.add(R.id.Frame, mFragment); to Listfragment current = (ListFragment) fm.findFragmentById(android.R.id.content); and ft.add(android.R.id.content, mFragment);

and finally changing to this in PlaneraFragment:

public class PlaneraFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    /** Inflating the layout country_details_fragment_layout to the view object v */
    View v = inflater.inflate(R.layout.planera_fragment, null);

    /** Getting the textview object of the layout to set the details */
    TextView tv = (TextView) v.findViewById(R.id.text_planera);

    /** Getting the clicked item's position and setting corresponding details in the textview of the detailed fragment */
    tv.setText("Hej ");

    return v;
    }

}

My code:

Main

package com.example.easysave;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.Tab;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.setDisplayShowTitleEnabled(true);

    Tab tab = actionBar.newTab().setText("Kategorier")
        .setTabListener(new CustomTabListener<KategorierFragment>(this, "kategorier", KategorierFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Profil")
        .setTabListener(new CustomTabListener<ProfilFragment>(this, "profil", ProfilFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Info")
        .setTabListener(new CustomTabListener<InfoFragment>(this, "info", InfoFragment.class));
    actionBar.addTab(tab);
    }
}

KategorierFragment

package com.example.easysave;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class KategorierFragment extends ListFragment {

String kategorier[] = new String[]{
         "Mat","El","Resor","Aktiviteter","Annat"};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_selectable_list_item, kategorier);

    setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Fragment mFragment = new KategorierFragment();
    switch(position){
    case 0:
        mFragment = new MatFragment();
        break;
    case 1:
        mFragment = new ElFragment();
        break;
    case 2:
        mFragment = new ResorFragment();
        break;
    case 3:
        mFragment = new AktiviteterFragment();
        break;
    case 4:
        mFragment = new AnnatFragment();
        break;

    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ListFragment current = (ListFragment) fm.findFragmentByTag(getTag());

    ft.add(R.id.Frame, mFragment);
    ft.addToBackStack(null);
    ft.hide(current);
    ft.commit();
}

@Override
public void onStart() {
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

MatFragment

package com.example.easysave;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MatFragment extends ListFragment{

String mat[] = {"Planera", "Storkok"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_selectable_list_item, mat);
     setListAdapter(adapter);
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Fragment mFragment = new MatFragment();
    switch(position){
    case 0:
        mFragment = new PlaneraFragment();
        break;
    case 1:
        mFragment = new StorkokFragment();
        break;
    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ListFragment current = (ListFragment) fm.findFragmentByTag(getTag());
    if(current == null)
    ft.add(R.id.Frame, mFragment);
    ft.addToBackStack(null);
    ft.hide(current);
    ft.commit();
}

@Override
public void onStart() {
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

CustonTabListener

package com.example.easysave;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;

public class CustomTabListener<T extends Fragment> implements TabListener {

//deklarerar variabler mm
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;


public CustomTabListener(Activity activity, String tag, Class<T> clz){
    mActivity = activity;
    mTag = tag;
    mClass = clz;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if(mFragment==null){
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(R.id.Frame, mFragment, mTag);
    }else{
        ft.attach(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    if(mFragment!=null)
        ft.detach(mFragment);
    }
}

PlaneraFragment

package com.example.easysave;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class PlaneraFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.planera_fragment, container, false);

    TextView text = (TextView) getActivity().findViewById(R.id.text_planera);
    text.setText("Hello there!");

    return view;
    }

}

planera_fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:textIsSelectable="false"
    android:id="@+id/text_planera"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

</LinearLayout>
解决方案

You need to figure out how you should handle your tabs. You're attaching and detaching in your TabListener, but showing and hiding when clicking through your ListView.

You either detach all your current fragments and then attach the new one, or change your tab listeners to .show() and .hide() instead.

EDIT: According to the last comment in this answer, there's a good example of keeping your tab history while switching between tabs.

The following implementation of TabListener is simple but will not keep your back stack intact. When switching to another tab, you're not selecting one to hide. This just glazes over it and detach the whole thing.

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
     mFragment = Fragment.instantiate(mActivity, mClass.getName());
     ft.replace(android.R.id.content, mFragment, mTag);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null)
        ft.detach(mFragment);
    }
}

这篇关于ListFragment和onListItemClick不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:15