如何在单击listfragment上的项目时将现有的listfragment替换为另一个listfragment?

时间:2015-10-05 09:48:29

标签: android listview

我在环顾四周成功点击每个列表项目后尝试了。我想在单击列表项时将现有的listfragment(MyFragment)替换为另一个listfragment(MyFragmentB)。

这是我的代码:

for fragdesign.java

package m.d.checker;


import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;





public class fragdesign extends AppCompatActivity {



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ListView listViewr=(ListView)findViewById(R.id.list);

    final MyFragment fragment = new MyFragment();
    // fragment.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .add(R.id.fragmentContainer, fragment).commit();


}


public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            finish();
            break;
        }

        default: {
            break;
        }
    }
    return true;
}

public static class MyFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //View view=inflater.inflate(R.layout.activity_main,container,false);
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_main, container, false);
        final ArrayAdapter<String> adaptershow = new ArrayAdapter<String>
                (getActivity(), R.layout.list_layout_structure, R.id.listText, getResources().getStringArray(R.array.LevelListing));
        setListAdapter(adaptershow);
        setRetainInstance(true);
        String me="Level";
        TextView textView =(TextView)rootView.findViewById(R.id.textView2);
        textView.setText("Select your " + me);

        final ListView listViewr = (ListView) rootView.findViewById(R.id.list);
        listViewr.setAdapter(adaptershow);

        listViewr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

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

    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String item= (String)getListAdapter().getItem(position);
        if (item.equals("100 Level")){


            //please help me out, i want it to open another listFragment (like Myfragment2 that is also a listfragment just like this) which will replace the original one.



        }
        if (item.equals("200 Level")){
            Toast.makeText(getContext(),"Thank you",Toast.LENGTH_SHORT).show();

            //the same for here
        }
        if (item.equals("300 Level")){
            Toast.makeText(getContext(),"Thank you",Toast.LENGTH_SHORT).show();
        }
        if (item.equals("400 Level")){
            Toast.makeText(getContext(),"Thank you",Toast.LENGTH_SHORT).show();
        }
        if (item.equals("500 Level")){
            Toast.makeText(getContext(),"Thank you",Toast.LENGTH_SHORT).show();
        }


    }

    }

}


}

我使用的是Android Studio v 1.3.2。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

ListFragment fragment = new MyFragmentB();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(layout id you want to replace, fragment).addToBackStack(null).commit();