android - Listview 不显示在 masterdetailsflow 的 fragment 中

标签 android android-fragments

我正在使用 masterdetailsflow 模板构建自定义详细信息布局。它似乎效果不佳。之前提出的最接近的问题似乎并没有解决我的问题 ListView not showing up in fragment 我放了一个Log做debug,似乎通过了“Check1”但是没有显示。

public class RouteItemDetailFragment extends Fragment {
...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_routeitem_detail, container, false);
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.routeitem_detail)).setText(mItem.title);
             Log.i("GPS", "onCreateView - RouteItemDetailFragment - check listadapter");          
        }
        return rootView;
    }
    @Override
    public void onViewCreated (View view, Bundle savedInstanceState){
        ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("helo1");
        listItems.add("helo2");
        listItems.add("helo3");
        listItems.add("helo4");
        ArrayAdapter<String> adapter;
            ((TextView) view.findViewById(R.id.txt1)).setText("kkkkkk");
            Log.i("Check1", "onCreateView - RouteItemDetailFragment - check listadapter");          
            adapter=new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1,
                    listItems);
            adapter.notifyDataSetChanged();
            ListView listAdapter = (ListView) view.findViewById(R.id.lv);
            listAdapter.setAdapter(adapter);

    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/twopanecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        style="?android:attr/textAppearanceLarge"
        android:id="@+id/routeitem_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        tools:context=".RouteItemDetailFragment" />
    <TextView
        android:id="@+id/txt1"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        />
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

最佳答案

您的 LinearLayout 默认水平显示 View ,因此您的第二个 TextView 和 ListView 可能只是在屏幕外显示。将方向更改为 “垂直”:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/twopanecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

关于android - Listview 不显示在 masterdetailsflow 的 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436150/

相关文章:

Android - 运行时使用 Dagger 创建对象

android - 如何从 Adapter 设置 LiveData?

android - Fragment 背面刷新 - Android 导航

android - fragment 中的 getActivity() 与 this.getActivity()

android - React Native : java. lang.NoClassDefFoundError:解析失败:Lcom/google/firebase/auth/internal/InternalAuthProvider

Android 从字符串中获取私钥

android - centos 中的 PJSIP ndk 配置问题

java - android HTTP post php脚本问题

android - 在什么情况下我不应该使用 FragmentManager 的 executePendingTransactions()(在主线程中)?