2

我创建了一个带有片段的列表视图,但存在一些问题。检查下图。

在此处输入图像描述

它仅在屏幕旋转时发生。我的代码如下:

public class LListFragment extends SherlockListFragment {

    private String[] line;

    public static final String[] TITLES = { "Henry IV (1)", "Henry V",
            "Henry VIII", "Richard II", "Richard III", "Merchant of Venice",
            "Othello", "King Lear" };

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

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, TITLES));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getActivity(), lineDetail.class);
        intent.putExtra("selected_line", "" + TITLES[position]);

        startActivity(intent);
        super.onListItemClick(l, v, position, id);
    }

}

我该如何解决这个问题?

4

2 回答 2

1

我正在使用ListFragment类似的布局,但没有遇到这个问题。尝试使用您自己Adapter的列表项。像这样的东西

public class MyAdapter extends BaseAdapter {
    @SuppressWarnings("unused")
    private static final String TAG = MyAdapter.class.getSimpleName();

    private List<String> _items;
    private LayoutInflater inflater;

    public MyAdapter(List<String> items, Context context) {
        _items = items;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return (_items != null) ? _items.size() : 0;
    }

    public Object getItem(int position) {
        return (_items != null) ? _items.get(position) : null;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.your_item_layout, null);
        }

        ((TextView)convertView.findViewById(R.id.itemText)).setText(_items.get(position));

        return convertView;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


    public synchronized void refresAdapter(List<String> items) {
        _items.clear();
        _items.addAll(items);
        notifyDataSetChanged();
    }
}

- - 编辑 - -

这是我的代码ListFragment

public class HomeListFragment extends ListFragment implements DataListener {

    private MyAdapter _adapter;
    private Context _ctx;

    public HomeListFragment(){}

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

        initialize();
    }

    private void initialize() {
        _adapter = new MyAdapter(new List<String>(), _ctx);

        setListAdapter(_adapter);

        final SomeServiceHandler s = new SomeServiceHandler(_ctx, HomeListFragment.this);
        s.request(RequestType.GETSOMEDATA, "");
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //do stuff
    }

    /**
    * Listener method that is invoked by SomeServiceHandler
    */
    public void onResponseRecieved(List<String> responseList) {
        _adapter.refresAdapter(responseList);
    }
}

我唯一能看到的与你不同的是,我在通过调用SomeServiceHandler类再次创建 Activity 之后刷新我的适配器,然后使用onResponseRecieved方法中的响应刷新。

于 2013-06-07T07:36:58.723 回答
0

它仅在屏幕旋转时发生。

把它放在你的 mainfest 中,作为运行片段的活动的一部分。我在我的应用程序中使用它来防止屏幕旋转时发生奇怪的事情。

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

完整的 Activity 声明如下所示:

<activity
    android:name=".ActivityName"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
于 2013-09-21T15:32:46.237 回答