【问题标题】:Your content must have a ListView whose id attribute is 'android.R.id.list' im trying to create a listfragment您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView,我正在尝试创建一个 listfragment
【发布时间】:2015-12-14 20:59:31
【问题描述】:

我是 android studio 的新手,我正在尝试创建一个带有列表的片段并将其显示在用于显示所有片段的抽屉类上,我已经阅读了很多帖子,但我不能找到我要找的答案,让我把我的代码贴给你,让你检查一下。

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lista"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

片段类代码:

public class FragmentoPrincipalChofer extends ListFragment {
    private List<ParseObject> mViaje;
    private ListView mLista;

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

        View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
        mLista = (ListView)x.findViewById(R.id.lista);
        String[] nombres= new String[2];
        nombres[0]="gaston";
        nombres[1]="gaston";
        ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, nombres);
        mLista.setAdapter(adaptador);

        return x;

    }

抽屉类代码:

public class DrawerPrincipal extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer_principal);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //cargar fragment principal usuario

    FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
    if (fragmento==null){
        fragmento=new FragmentoPrincipalChofer();
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(android.R.id.content,fragmento,"fragmento");
        transaction.commit();

    }


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

【问题讨论】:

    标签: android android-fragments android-listfragment


    【解决方案1】:

    您使用的是ListFragment 所以...

    ListFragment 具有由单个列表视图组成的默认布局。但是,如果您愿意,您可以通过从 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回您自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个 ID 为“@android:id/list”的 ListView 对象(或列表,如果它在代码中)

    ListView XML 更改为以下内容

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_gravity="center_horizontal" />
    

    你对它的引用

    mLista = (ListView)x.findViewById(android.R.id.list);
    

    【讨论】:

    • 嗨 Andrew 非常感谢您的回答,现在它可以正常工作了,但我有一个问题,列表与标题栏重叠,您知道我该如何更改吗?非常感谢。
    • 你能提供一张图片吗?不过,您最好在单独的问题中提出这个问题
    • 没问题!考虑将此答案标记为正确,以帮助其他人在将来找到它!
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多