保持片段的视图状态

时间:2014-07-15 08:08:29

标签: android android-fragments savestate

我已经看到了Link1这个问题,但可以理解它。我有一个加载列表的片段。当我单击列表项时,它会打开另一个活动。但我按下后退按钮再次加载列表。我希望它处于以前的滚动位置。在上面提到的链接中,它指定使用标志,但我没有说明这一点。

public class MainActivity extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard); 
    android.app.Fragment fragment = new MeFragment();    
    getFragmentManager().beginTransaction().replace(R.id.layout_FragmentsContainer, fragment).addToBackStack(null).commit();
   }
}

public class MeFragment extends Fragment
{
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  {
    return inflater.inflate(R.layout.fragment_me, container, false);
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState);

    meLV = (ListView) getView().findViewById(R.id.lv_Inbox);
    loadingListProgress = (ProgressBar) getView().findViewById(R.id.progress_LoadingList);  
    meList = new ArrayList<Message>();
    meAdapter = new MessagesListAdapter(getActivity(), meList);  
    //addFooter();
    meLV.setAdapter(meAdapter);
    meLV.setOnItemClickListener(this); 
    pageCount = 0;
    loadmoreProgressDialog = new ProgressDialog(getActivity()); 
    loadmoreProgressDialog.setTitle("Please wait ..."); 
    loadmoreProgressDialog.setMessage("Loading more ...");  
    loadmoreProgressDialog.setCancelable(true);
    loadUserMessages(); 
    meLV.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // TODO Auto-generated method stub
            //addFooter();
            loadmoreProgressDialog.show();
            loadUserMessages();
        }
    }); 
  }  
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    Utils.showToast_msg(getActivity(), "MessageItemClicked"); 
    ReferralDetailFragment fragment = new ReferralDetailFragment();    
    getFragmentManager().beginTransaction().replace(R.id.layout_FragmentsContainer, fragment).addToBackStack(null).commit();

  }
} 

public class ReferralDetailFragment extends Fragment implements OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
{
    View view = inflater.inflate(R.layout.fragment_referraldetail,container, false);
    linkToAcknowledge   = (TextView) view.findViewById(R.id.lbl_Link_to_Acknowledge);
    return view;  
  }
}

3 个答案:

答案 0 :(得分:1)

根据我们在评论中的交流,我完全取消了我的答案并重新写了一个新答案。

我从我的某个应用中复制/粘贴代码,删除无用的内容并更改名称。希望没有太多的输入错误,因为这是让它工作的最低要求。

当我从SecondFragment弹回FirstFragment时,FirstFragment的滚动位置与我点击一个项目以加载SecondFragment时相同。

请注意,我不会延长FragmentActivity。我有一个加载片段的活动。

扩展/修改以满足您的需求。

MainActivity:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
    }
}

FirstFragment Class:

public class FirstFragment extends Fragment implements OnItemClickListener {
    private ListView mListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first_fragment_layout, container, false);
    }

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

        mListView = (ListView) getView().findViewById(R.id.listview_first_fragment);
        mListView.setAdapter(mAdapter); // depends on your adapter
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mListView.setItemChecked(position, true);

        //in case you need, set the bundle here, for example pass the position
        Bundle arguments = new Bundle();
        arguments.putInt("position", position);
        SecondFragment secondFragment = new SecondFragment();
        secondFragment.setArguments(arguments);

        getFragmentManager().beginTransaction().replace(R.id.fragment_container, secondFragment).addToBackStack(null).commit();
    }
}

SecondFragment Class:

public class SecondFragment extends Fragment {
    private Integer mPosition;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.second_fragment_layout, container, false);
    }

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

        Bundle arguments = getArguments();
        if (arguments == null) {
            mPosition= 0;
        } else {
            mPosition= arguments.getInt("Position");
        }
    }
}

答案 1 :(得分:1)

我在我的应用程序中为此实现了一个简单的解决方案,基本上当您再次按下再次转到片段时,会调用onCreateView()。在onCreateView()中,您已完成所有初始化,因此我们更改了

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
{
    View view = inflater.inflate(R.layout.fragment_me, container, false);
    /*
     *Whatever you want to do
     *
     */
    return view;  
}

为:

View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
{
    if(view==null){

        view = inflater.inflate(R.layout.fragment_me, container, false);
        /*
         *Whatever you want to do
         *
         */
    }
    else{
         ((ViewGroup)view.getParent()).removeView(view);
    }
    return view;  
}

在这里,我们将View视图移到外面并使其成为类变量。因此,如果它是第一次调用片段,则它为null并且初始化发生,否则它将变为其他黑色。其他块也是必需的,因为onCreateView()会添加它作为视图父级的子项返回的内容,因此,由于视图已经存在,我们将其删除,onCreateView会自动重新添加它。

答案 2 :(得分:0)

您尝试实现的目标可以在savedInstanceState的帮助下完成。我也遇到过这种问题,我在转换过程中使用add()方法而不是replace()来解决这个问题。 如果你可以改变你的方法或者已经没有使用add()而不是试一试。 如果add()方法没有做到这一点,那么请检查savedInstanceState

的实现
  1. correctly save instance state.

  2. How to save states of fragment views.