在选项卡式活动中,ListView不可见。

时间:2017-07-24 12:50:07

标签: java android listview android-fragments

使用ListView打开片段时有空白屏幕。其他组件没有问题 - 按钮等我尝试将可见性“match_parent”更改为“wrap_content”等。也许有人有类似的问题。 我的代码:

MainActivity的片段

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView;
        if (getArguments().getInt(ARG_SECTION_NUMBER)==1)
        {
            rootView = inflater.inflate(R.layout.fragment_activity_list, container, false);
            return rootView;
        }
        if (getArguments().getInt(ARG_SECTION_NUMBER)==2)
        {
            rootView = inflater.inflate(R.layout.fragment_activity_exp, container, false);
            return rootView;
        }
        else
        {
            rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

activity_list的片段

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

public ArrayAdapter<String> adapter;
public ListView listOfQuests;
public View view;
public String[] initialValues;
public ArrayList<String> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    view = inflater.inflate(R.layout.fragment_activity_list, container, false);
    listOfQuests = (ListView)view.findViewById(R.id.listOfQuests);
    initialValues = new String[]{"Olawska 12, wroclaw", "curie-sklodowskiej 12, wroclaw", "Stysia 12, wroclaw",
            "pl. Jana Pawla II 12, wroclaw", "pl. Strzegomski 12, Wroclaw"};

    list = new ArrayList<String>();
    for (int i = 0; i < initialValues.length; ++i)
    {
        list.add(initialValues[i]);
    }

    adapter = new ArrayAdapter<String>(this.getActivity(),
            android.R.layout.simple_list_item_1, list);
    listOfQuests.setAdapter(adapter);
    listOfQuests.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                                final int position, long id)
        {

        }
    });

    return view;
}

和activity_fragment_list

<FrameLayout 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"
tools:context="com.example.cat.quests.activity_list">

<!-- TODO: Update blank fragment layout -->

<ListView
    android:id="@+id/listOfQuests"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible" />

0 个答案:

没有答案