单击Android Studio中ListFragment中的项目时显示详细信息片段

时间:2015-02-13 13:16:06

标签: android android-listfragment onitemclick

我的应用有一个标签栏,应该始终可见。第一个选项卡包含ListFragment。当我点击其中的某个项目时,它会加载一个新活动,该活动会创建一个详细信息片段,显示列表中对象的内容。我想显示这些内容,而不必开始新的活动,因为它也会破坏标签栏!

非常感谢任何帮助!

为了说明,这里有一些截图和代码:

ListFragment After an item in the list has been clicked

这是创建选项卡的FragmentActivity的代码:

public class MainFragmentActivity extends FragmentActivity
    implements ActionBar.TabListener {

SectionsPagerAdapter sectionsPagerAdapter = null;
ViewPager viewPager = null;

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

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

    getActionBar().setDisplayShowHomeEnabled(false);
    getActionBar().setDisplayShowTitleEnabled(false);

    sectionsPagerAdapter =
            new SectionsPagerAdapter
                    (
                            getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(sectionsPagerAdapter);

    viewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });    // End of sectionPageAdapter.

    Tab browseTab = actionBar.newTab();
    browseTab.setIcon(R.drawable.search);
    browseTab.setTabListener(this);
    actionBar.addTab(browseTab);

    Tab myStuffTab = actionBar.newTab();
    myStuffTab.setIcon(R.drawable.my_stuff);
    myStuffTab.setTabListener(this);
    actionBar.addTab(myStuffTab);

    Tab profileTab = actionBar.newTab();
    profileTab.setIcon(R.drawable.profile);
    profileTab.setTabListener(this);
    actionBar.addTab(profileTab);

    Tab settingsTab = actionBar.newTab();
    settingsTab.setIcon(R.drawable.settings);
    settingsTab.setTabListener(this);
    actionBar.addTab(settingsTab);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab,
                          FragmentTransaction fragmentTransaction) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {

            case 0:
                Fragment browseFragment = new BrowseFragment();
                Bundle browseArgs = new Bundle();
                browseArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
                browseFragment.setArguments(browseArgs);
                return browseFragment;

            case 1:
                Fragment myStuffFragment = new MyStuffFragment();
                Bundle myStuffArgs = new Bundle();
                myStuffArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
                myStuffFragment.setArguments(myStuffArgs);
                return myStuffFragment;

            case 2:
                Fragment profileFragment = new ProfileFragment();
                Bundle profileArgs = new Bundle();
                profileArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
                profileFragment.setArguments(profileArgs);
                return profileFragment;

            case 3:
                Fragment settingsFragment = new SettingsFragment();
                Bundle settingsArgs = new Bundle();
                settingsArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
                settingsFragment.setArguments(settingsArgs);
                return settingsFragment;
        }
        return null;
    }

    // There are always 4 tabs
    @Override
    public int getCount() {
        return 4;
    }

    // Return a CharSequence for the selected tab
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            case 3:
                return getString(R.string.title_section4).toUpperCase();

        }
        return null;
    }
}

}    // End of class.

这是第一个标签的代码:

public class BrowseFragment extends ListFragment {


public static String sectionNumberKey = "sec_num";
private String activityName = "Browse";

int currentPosition = 0;

List<Listing> listings = new ListingData().getListings();

public BrowseFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    BrowseArrayAdapter adapter = new BrowseArrayAdapter(getActivity(),
            R.layout.browselist_item,
            listings);
    setListAdapter(adapter);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_browse,
            container, false);

    TextView tv = (TextView) rootView.findViewById(R.id.section_label);

    int intSectionNumber = getArguments().getInt(sectionNumberKey);
    String numAsString = Integer.toString(intSectionNumber);
    tv.setText(numAsString);

    activityName += " " + numAsString;
    return rootView;
}


@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    showDetails(position);
}

void showDetails(int index) {
    currentPosition = index;

    Intent intent = new Intent();
    intent.setClass(getActivity(), BrowseDetailsActivity.class);
    intent.putExtra("index", index);
    startActivity(intent);
}

以下是详细活动的代码:

public class BrowseDetailsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        // During initial setup, plug in the details fragment.
        BrowseDetailFragment details = new BrowseDetailFragment();
        details.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
    }
}
}

1 个答案:

答案 0 :(得分:1)

基本上,你只需要在BrowseFragment的showDetails方法中使用BrowseDetailActivity中的onCreate方法的内容。通过这种方式,您可以删除BrowseDetailsActivity。

<强> BrowseFragment.java

void showDetails(int index) {
    BrowseDetailFragment details = BrowseDetailFragment.newInstance(index);
    getChildFragmentManager().beginTransaction().add(details).commit();
}

并在BrowseDetailFragment中使用静态newInstance方法,如下所示:

<强> BrowseDetailFragment.java

public class BrowseDetailFragment extends Fragment {

    private int position;

    public static BrowseDetailFragment newInstance(int position) {
        BrowseDetailFragment fragment = new BrowseDetailFragment();
        fragment.position = position;
        return fragment;
    }

    public BrowseDetailFragment() {
        //Required empty constructor
    }

    //Lifecycle methods and logics
}

请务必提供一些导航选项,以便用户返回列表。