片段中的ListView不覆盖完整的父级

时间:2018-11-23 21:42:29

标签: java android android-fragments

我有一个片段中的ListView,当列表未完全填充时,它不会覆盖整个Parent。 问题是ListView的滚动行为,我只能在ListView最初加载的小窗口中浏览。 (4个或更多条目确实占据了整个父级,出现问题的条目越少) Screenshot

片段:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
   >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"

        />       </LinearLayout>

主要XML:

<android.support.design.widget.CoordinatorLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation" />


    </android.support.design.widget.CoordinatorLayout>

通过Volley + JSON使用ListView的适配器填充ListView。注释是否需要代码。

当我尝试将ListViews的高度放在“ match_parent”上时,整个屏幕都被ListView占据,但是它不再可滚动了。

我该怎么办?

编辑:

协调器布局-这是其中带有底部导航的框架协调器的代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation" />

  </android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:0)

您可以为listView设置最小高度属性,并调整为适合您的属性。 例如。要将其设置为20dp,请将android:minHeight="20dp"添加到您的listView属性中。

最好的解决方案是将CoordinatorLayout的所有内容包装在您可以控制的布局中,例如ConstraintLayout。在这种情况下,您将在其他组件之间对齐FrameLayout(及其处理的片段),并且所有内容都可以滚动和单击。此解决方案的模拟为:

<CoordinatorLayout>
    <ConstraintLayout height="match_parent">
        <BottomNavigation bottomToBottomOf="parent" />

        <FrameLayout height="0dp"
            topToTopOf="parent"
            bottomToTopOf="bottomNavigation" />
    </ConstraintLayout>
</CoordinatorLayout>

答案 1 :(得分:0)

我发布了一个答案,以确保每个人都知道问题已经发展。

之前: 在此供稿中发布1或2个线程以显示它们确实导致了以下情况: enter image description here

如您所见,它与父级的整个高度不匹配,具有3个或更多帖子(取决于图像高度),效果很好。

现在在此使用此布局代码(感谢iFanie的新代码):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation" />


    </android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>

现在具有与整个屏幕达到正确高度之前相同的2个线程: enter image description here

现在只有1个线程,ListView不会填充屏幕: enter image description here

这是片段的代码:

    public fragment_news() {
        // Required empty public constructor
    }

    public static fragment_news newInstance(String param1, String param2) {
        fragment_news fragment = new fragment_news();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news_main, container,true);

        if(config.debugcode) {
            Log.e("Fragment", "Fragment News");
        }

        listView = view.findViewById(R.id.list);

        feedItems = new ArrayList<FeedItem>();


        // We first check for cached request
                Cache cache = 

AppController.getInstance().getRequestQueue().getCache();
        // DK: Fresh Data every damn time
        cache.clear();
// making fresh volley request and getting json
            JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
                    URL_FEED, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                VolleyLog.d(TAG, "Response: " + response.toString());
                if (response != null) {
                    parseJsonFeed(response);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });

        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonReq);

    return inflater.inflate(R.layout.fragment_news_main, container, false);
}



    public void parseJsonFeed(JSONObject response) {
        try {

            JSONArray feedArray = response.getJSONArray("feed");
            for (int i = 0; i < feedArray.length(); i++) {
                JSONObject feedObj = (JSONObject) feedArray.get(i);

                FeedItem item = new FeedItem();
                item.setId(feedObj.getInt("id"));
                item.setName(feedObj.getString("name"));

                // Image might be null sometimes
                String image = feedObj.isNull("image") ? null : feedObj
                        .getString("image");
                item.setImge(image);
                item.setStatus(feedObj.getString("status"));
                item.setProfilePic(feedObj.getString("profilePic"));
                item.setTimeStamp(feedObj.getString("timeStamp"));

                // url might be null sometimes
                String feedUrl = feedObj.isNull("url") ? null : feedObj
                        .getString("url");
                item.setUrl(feedUrl);

                feedItems.add(item);

            }

            listAdapter = new FeedListAdapter(getActivity(), feedItems);
            listView.setAdapter(listAdapter);


            // notify data changes to list adapater
            listAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }



    public boolean onCreateOptionsMenu(Menu menu) {
        getActivity().getMenuInflater().inflate(R.menu.newsfeed_main, menu);
        return true;
    }



}