从ListActivity到Fragment

时间:2014-04-23 13:27:26

标签: android fragment listactivity

如何将以下代码转换为片段。我无法将其转换为Fragment而不是“ListActivity”。我有滑动标签和我希望下面的代码之一,但唯一的问题是它必须碎片。

public class PinnedSectionListActivity extends ListActivity implements OnClickListener {

    private static final int[] COLORS = new int[] {
        R.color.green_light, R.color.orange_light,
        R.color.blue_light, R.color.red_light };

    private class MyPinnedSectionListAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {

        public MyPinnedSectionListAdapter(Context context, int resource, int textViewResourceId, List<Item> objects) {
            super(context, resource, textViewResourceId, objects);
        }

        @Override public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView) super.getView(position, convertView, parent);
            view.setTextColor(Color.DKGRAY);
            view.setTag("" + position);
            if (getItem(position).type == Item.SECTION) {
                //view.setOnClickListener(PinnedSectionListActivity.this);
                view.setBackgroundColor(parent.getResources().getColor(COLORS[position % COLORS.length]));
            }
            return view;
        }

        @Override public int getViewTypeCount() {
            return 2;
        }

        @Override public int getItemViewType(int position) {
            return getItem(position).type;
        }

        @Override public boolean isItemViewTypePinned(int viewType) {
            return viewType == Item.SECTION;
        }
    }

    private static class Item {
        public static final int ITEM = 0;
        public static final int SECTION = 1;

        public final int type;
        public final String text;

        public Item(int type, String text) {
            this.type = type;
            this.text = text;
        }

        @Override public String toString() {
            return text;
        }
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(
                this, android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems());
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "Item: " + position, Toast.LENGTH_SHORT).show();
    }

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

    private static ArrayList<Item> prepareItems() {
        ArrayList<Item> result = new ArrayList<Item>();
        for (int i = 0; i < 30; i++) {
            result.add(new Item(Item.SECTION, "Section " + i));
            for (int j=0; j<4; j++) {
                result.add(new Item(Item.ITEM, "Item " + j));
            }
        }
        return result;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(this, "Item: " + v.getTag(), Toast.LENGTH_SHORT).show();
    }

}

MY尝试onCreateView方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View v = inflater.inflate(R.layout.activity_main, container, false); MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems()); setListAdapter(adapter); 
return v; 
} 
private void setListAdapter(MyPinnedSectionListAdapter adapter) {
 } 

2 个答案:

答案 0 :(得分:0)

片段不是活动,但可以包含在FragmentActivity中。

构建片段,在FragmentActivity中实例化。 然后使用Fragment.onCreateView()方法来扩充布局。

答案 1 :(得分:0)

public class PinnedSectionListActivity extends ListFragment implements OnClickListener
{
  /*rest of the code*/
}