gpt4 book ai didi

java - 如何添加 ListFragment 使用到布局

转载 作者:行者123 更新时间:2023-12-01 12:05:02 25 4
gpt4 key购买 nike

我正在尝试将 ListFragment 添加到 java 中的布局中。但是我收到一条错误消息,指出无法应用 FragmentTransaction 中的 add() 。我可以通过将 Fragment manger 从等于 getSupportFragmentManger 更改为 getFragmentManger 来解决此问题。这很好,但是如果我正在为需要支持库的早期版本的 android 进行开发怎么办?我该怎么做才能解决这个问题,提前致谢。

    package com.listfragment.example.listfragmentexample;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager manager;
manager = getSupportFragmentManager();
MyListFragment frag1 = new MyListFragment();

FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.main, frag1, "Fragment1");
fragmentTransaction.commit();
}
}

package com.listfragment.example.listfragmentexample;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

/**
* Created by Neil on 12/27/2014.
*/
public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener {

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter =ArrayAdapter.createFromResource(getActivity(), R.array.heros, android.R.layout.simple_list_item_1 );
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}

/* @Override
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT ).show();
}*/

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT).show();
}
}

错误消息截图 /image/GE8s9.png

    <?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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@android:id/empty" />

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


</LinearLayout>

下面的activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main"
>

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</RelativeLayout>

最佳答案

导入

import android.support.v4.app.ListFragment

代替

import android.app.ListFragment;

在类MyListFragment

关于java - 如何添加 ListFragment 使用到布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com