如何链接2片段与两个列表视图

时间:2015-10-14 08:34:01

标签: android listview android-fragments

我需要你的帮助..我已经工作了2天,仍然与片段混淆..

我的目标是创建一个包含listview的片段。在单击的每个项目上,它将显示另一个具有另一个列表视图的片段(如子菜单,但显示在不同的片段中)。

我这里有我的片段代码

public class MasterFragment extends Fragment implements AdapterView.OnItemClickListener {

ListView masterlist;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.master_fragment, container, false);
    masterlist = (ListView) view.findViewById(R.id.masterlistview);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.masterlist, android.R.layout.simple_list_item_1);
    masterlist.setAdapter(adapter);
    masterlist.setOnItemClickListener(this);
    return view;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    long position = masterlist.getItemIdAtPosition(i);
    if(position == 0){
        System.out.println("Item 0");
    } else {
        System.out.println("Item 1");
    }
}

在我的onItemClick中,我如何调用片段。我有2个片段视图,但其中包含不同的数组。如果position == 0,请调用FragmentA。否则,调用FragmentB。

我在MasterFragment中创建了一个接口

public interface Communicator {
    void respond(int index);
}

然后在我的MainActivity中实现它..但我不确定如何做到这一点。

2 个答案:

答案 0 :(得分:0)

if(position == 0){
 Fragment newFragment = new FragmemtA();
 FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();      
 fragmentTransaction.replace(R.id.container, newFragment).addToBackStack(null).commit();
 }
else
{
 Fragment newFragment = new FragmemtB();
 FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
 fragmentTransaction.replace(R.id.container, newFragment).addToBackStack(null).commit();
 }

答案 1 :(得分:0)

创建回调接口。在其中调用片段的activity中实现此接口。使用回调方法在片段发送位置中单击项目。然后根据位置调用您的片段

你的片段应该是这样的

public class MasterFragment extends Fragment implements AdapterView.OnItemClickListener {

public inteface OnItemClickResponse

{
    void itemClick ( int position);
}

OnItemClickResponse itemClickResponse;

ListView masterlist;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.master_fragment, container, false);
    masterlist = (ListView) view.findViewById(R.id.masterlistview);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.masterlist, android.R.layout.simple_list_item_1);
    masterlist.setAdapter(adapter);
    masterlist.setOnItemClickListener(this);
    itemClickResponse = (OnItemClickResponse) getActivity();
    return view;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    long position = masterlist.getItemIdAtPosition(i);
    if (position == 0) {
        itemClickResponse.itemClick(position);
        System.out.println("Item 0");
    } else {
        itemClickResponse.itemClick(position);
        System.out.println("Item 1");
    }
}

和in activity(必须实现接口'OnItemClickResponse')

@Override
public void itemClick(int position) {
    if (position == 1) {
        Fragment fragment = new FirstFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, fragment).commit();
    } else {
        Fragment fragment = new SecondFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, fragment).commit();
    }
}