为什么片段交易不起作用?

时间:2016-02-21 08:07:00

标签: java android android-activity fragment

我正在编写一个简单的程序,其中使用了两个片段和一个活动。在活动的帧布局中显示两个片段(一次一个)。第一个片段是列出用户可以选择的项目的列表视图,然后主要活动应该根据第一个片段内的监听器确定的项目位置将第一个片段与细节片段交换。麻烦的是,我的程序实际上不会开始交换。这是活动的代码:

package com.example.user.monkeys;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

public class ListActivity extends FragmentActivity implements       
monkeyListFragment.OnMonkeySelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("Activity", "onCreate Pre-Fragment 1");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monkey_list_frame);


    if (findViewById(R.id.fragment_container) != null) {

        if (savedInstanceState != null) {
            return;
        }


        monkeyListFragment monkeyList = new monkeyListFragment();


        monkeyList.setArguments(getIntent().getExtras());

        // Add the fragment to the container
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, monkeyList);

        Log.i("Activity", "made it end onCreate");
    }
}

@Override
public void onMonkeyItemSelected(int position) {
    Log.i("From Activity", "onMonkeyItemSelected");
    monkeyDetailsFragment newDetailFrag = new monkeyDetailsFragment();
    Bundle args = new Bundle();
    args.putInt("itemPosition", position);
    newDetailFrag.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment_container, newDetailFrag);
    transaction.addToBackStack(null);

    transaction.commit();
}
}

listview片段的代码:

package com.example.user.monkeys;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class monkeyListFragment extends Fragment{

OnMonkeySelectedListener monkeyCallBack;
private ListView monkeyLV;
private String[] monkeyStrings;

public interface OnMonkeySelectedListener {
    public void onMonkeyItemSelected(int position);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i("Fragment 1", "Made it to onCreateView");
    View view = inflater.inflate(R.layout.list_fragment, container, false);

    monkeyLV = (ListView) view.findViewById(R.id.monkeyListView);
    monkeyStrings = getResources().getStringArray(R.array.monkey_data_list);

    ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, monkeyStrings);
    monkeyLV.setAdapter(objAdapter);

    AdapterView.OnItemClickListener monkeyListen = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("ListFragment-ClckLstnr", "Made it");
            monkeyCallBack.onMonkeyItemSelected(position);

        }
    };

    Log.i("Fragment 1", "Made it past OnItemClick Listener");
    return view;

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        monkeyCallBack = (OnMonkeySelectedListener) activity;
    }
    catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}
}

最后是详细信息片段的代码(这还没有完全充实,但我相信它仍应该交换)。

package com.example.user.monkeys;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class monkeyDetailsFragment extends Fragment {

ImageView monkeyPicture;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    Log.i("From Detail Fragment", "Got here");
    View view = inflater.inflate(R.layout.monkey_detail_fragment, container,      
false);

    return view;
}


}

1 个答案:

答案 0 :(得分:0)

我找到了listview片段的缺失代码行:

monkeyLV.setOnItemClickListener(monkeyListen)