0

我有标签,在一个标签中有一个ListFragment带有onListItemClick. 当您单击一个项目时ListFragment,会显示另一个项目,因此可以正常工作。onListItemClick与第一个相同的ListFragment执行到第二个ListFragment

编辑:这就是我尝试更改 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);
        }
    }
}

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

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)

我有两个问题:

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

  2. (已解决)当我单击第一个ListFragment显示的项目然后单击另一个ListFragment时,应用程序崩溃。

单击第二个时我想要的ListFragment是一个带有简单文本的片段,而不是列表。

我这样做是错误的吗?有谁知道吗?

问题 2 通过以下方式解决:

在 main 中注释 setContentView

更改所有 ListFragment current = (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 中改为:

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;
    }

}

我的代码:

主要的

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);
    }
}

类别片段

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);
    }    
}

自定义标签监听器

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);
    }
}

平面碎片

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;
    }

}

平面片段

<?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>
4

1 回答 1

0

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

您可以分离所有当前片段,然后附加新片段,或者将选项卡侦听器更改为 .show() 和 .hide() 。

编辑:根据此答案中的最后一条评论,有一个很好的例子,可以在标签之间切换时保留标签历史记录。

TabListener 的以下实现很简单,但不会保持您的后台堆栈完好无损。切换到另一个选项卡时,您没有选择要隐藏的选项卡。这只是在它上面涂上釉,然后把整个东西分开。

@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);
    }
}
于 2013-04-24T15:03:57.170 回答