列表片段未出现在我的布局中

时间:2014-04-27 11:51:39

标签: android android-layout android-fragments

我正在尝试在我的布局中实现列表片段,但片段没有出现,这是我的代码:

categorias.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/categorias_land"
    android:orientation="horizontal" >

<android.support.v7.widget.Space
    android:id="@+id/space1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="47" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="82"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/categoria" />

</LinearLayout>

<android.support.v7.widget.Space
    android:id="@+id/space2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="75" />

<fragment
    android:id="@+id/listFragment"
    android:name="zzzzz"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="237"
    tools:layout="@layout/listacategoriasfragment" />

<android.support.v7.widget.Space
    android:id="@+id/space3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="71" />

</LinearLayout>

Categorias.class

package zzzzz;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class Categorias extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categorias);
    getSupportFragmentManager().beginTransaction().add(R.id.layoutListFragment, new ListaCategoriasFragment7p()).commit();
}

}

ListaCategoriasFragment7p.class

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListaCategoriasFragment7p extends ListFragment {
    private String[] month = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November",
            "December" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListAdapter listAdapter = new ListaCategoriasAdapter(getActivity());
        setListAdapter(listAdapter);
    }

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

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    public class ListaCategoriasAdapter extends ArrayAdapter<String> {
        private Activity context;
        private Typeface fuenteToonish;

        public ListaCategoriasAdapter(Activity context) {
            super(context, R.layout.itemlistacategoriasfragment);
            this.context = context;
            fuenteToonish = Typeface.createFromAsset(context.getAssets(),
                    "fonts/toonish.ttf");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View itemListaCategoriasFragment = inflater.inflate(R.layout.itemlistacategoriasfragment, null);

            TextView textViewItemLista = (TextView) itemListaCategoriasFragment.findViewById(R.id.tituloCategoria);
            textViewItemLista.setText(month[position]);
            textViewItemLista.setTypeface(fuenteToonish);
            textViewItemLista.setShadowLayer(1, 0, 0, Color.BLACK);

            return itemListaCategoriasFragment;
        }

    }

}

您认为这可能是什么问题,我尝试了很多stackoverflow帖子。

2 个答案:

答案 0 :(得分:0)

您没有在片段中使用onCreateView方法。

例如:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    View v = inflater.inflate(R.layout.campaign_fragment, container, false);

            // Init your views 
            Button b = (Button) v.findViewById(R.id.yourButton);

    return v;
}

此外,这是一个很好的例子。

link

答案 1 :(得分:0)

问题在于这一行:

public ListaCategoriasAdapter(Activity context) {
            super(context, R.layout.itemlistacategoriasfragment);

我没有打电话给构造函数,因为我没有发送数据,现在它可以工作:

public ListaCategoriasAdapter(Activity context) {
            super(context, R.layout.itemlistacategoriasfragment, month);