在Android上具有片段问题的ListView

时间:2013-06-07 06:41:07

标签: android android-fragments fragment android-listfragment

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

Enter image description here

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

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);
    }

}

如何解决此问题?

2 个答案:

答案 0 :(得分: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后刷新我的适配器,然后使用{中的响应刷新{1}}方法。

答案 1 :(得分:0)

  

仅在屏幕旋转时才会发生。

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

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

完整的活动声明如下所示:

<activity
    android:name=".ActivityName"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>