gpt4 book ai didi

java - 在 ViewPager 中时,ListFragment 在事务后显示空屏幕

转载 作者:行者123 更新时间:2023-12-02 05:34:26 24 4
gpt4 key购买 nike

我正在尝试做什么
创建一个 ListFragment(将位于 ViewPager 内),它将用一个新的 ListFragment 替换自身,该新的 ListFragment 包含基于前一个列表的单击位置的新数据。

我遇到的问题
onListItemClick后,屏幕不显示任何内容。我考虑过只清除ListAdapter而不是初始化多个ListFragments,但我想保留backStack(我不知道如何在没有Fragment Transactions的情况下保留backStack)



MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity {

private ViewPager mPager;
private MainPagerAdapter mPagerAdapter;

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

mPagerAdapter = new MainPagerAdapter(this.getSupportFragmentManager());
mPager = (ViewPager) this.findViewById(R.id.fragment_container);
mPager.setAdapter(mPagerAdapter);
}

private class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return new MyListFragment(); //Simplified for the post
}

@Override
public int getCount() {
return 1; //Simplified for the post
}
}
}



MyListFragment.java

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyListFragment extends ListFragment {
private ArrayList<String> titles;

public MyListFragment() {
titles = new ArrayList<String>();
titles.add("one");titles.add("two");titles.add("three");
}

public MyListFragment(ArrayList<String> titles) {
this.titles = titles;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new MyListAdapter());
}

@Override
public void onListItemClick (ListView l, View v, int position, long id) {
ArrayList<String> list = new ArrayList<String>();
list.add("1");list.add("2");list.add("3");

getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new MyListFragment(list))
.addToBackStack(null)
.commit();
}

private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(getActivity(), android.R.layout.simple_list_item_1, titles);
}
}
}



activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

最佳答案

终于明白了。我创建了一个 ListRootFragment,它将扩大其布局并添加到 ListFragment 中。

ListRootFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListRootFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment, container, false);

this.getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment, new MyListFragment())
.commit();

return view;
}
}



fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragment" >

</LinearLayout>



更新了MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity {

...

private class MainPagerAdapter extends FragmentStatePagerAdapter {
...

@Override
public Fragment getItem(int position) {
return new ListRootFragment();
}

...
}
}

关于java - 在 ViewPager 中时,ListFragment 在事务后显示空屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25150750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com