【问题标题】:Dynamically added views not displaying in LinearLayout动态添加的视图未在 LinearLayout 中显示
【发布时间】:2014-09-09 17:30:25
【问题描述】:

我正在为其构建的客户端需要一个功能,该功能需要我围绕大量内容(包括“新闻”部分)包裹滚动视图。因此,我不能使用 ListView。我正在尝试将所有 ListItem 视图手动膨胀为垂直 LinearLayout。我意识到我会失去 ListView 的甜蜜优化,但这是客户想要的,所以我必须答应。我觉得我的解决方案应该可以工作,但屏幕上没有显示任何视图。我的 ScrollView 里面是一个 LinearLayout,里面是第二个 LinearLayout:

<!-- News Fragment -->
        <LinearLayout
            android:id="@+id/news_frag"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/test_yellow"/>

这是 NewsListItem 的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="horizontal"
android:onClick="newsListItemClick"
android:background="@color/test_green">

<com.android.volley.toolbox.NetworkImageView
    style="@style/thumbnail_image"
    android:id="@+id/news_list_item_image"
    android:layout_width="@dimen/thumbnail_width"
    android:layout_height="@dimen/thumbnail_height" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView 
        style="@style/headline_text"
        android:id="@+id/news_list_item_headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView 
        style="@style/promo_text"
        android:id="@+id/news_list_item_promo_brief"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

最后,我以编程方式使用的代码。 newsRequest() 是我在 onCreateView 中的最后一次调用:

private void newsRequest() {
    Log.d("snw", "newsRequest()");
    JsonArrayRequest jsonRequest = new JsonArrayRequest(NEWS_FEED_URL, 
            new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                    NewsListItem item = new NewsListItem((JSONObject) response.get(i));
                    View view = addNewsToView(item);
                    containerView.addView(view);
                    containerView.invalidate();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            containerView.bringToFront(); //something I tried, didn't help
            containerView.postInvalidate(); //Same as above
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }

    });

    volleyQueue.add(jsonRequest);
}

private View addNewsToView(NewsListItem item) {
    Log.d("snw", "addNewsToView()");
    ViewHolder holder;
    View view = new View(getActivity());

    if (view.getTag() == null) {
        Log.d("snw", "Holder was null, inflating view");
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.news_list_item, null);

        // set up ViewHolder
        holder = new ViewHolder();
        holder.thumbnail = (NetworkImageView) view.findViewById(R.id.news_list_item_image);
        holder.headline = (TextView) view.findViewById(R.id.news_list_item_headline);
        holder.promoBrief = (TextView) view.findViewById(R.id.news_list_item_promo_brief);

        //set custom fonts
        Typeface futuraBold =
                Typeface.createFromAsset(view.getContext().getAssets(), "fonts/futura_today_bold.ttf");
        Typeface futuraLight =
                Typeface.createFromAsset(view.getContext().getAssets(), "fonts/futura_today_light.ttf");

        holder.headline.setTypeface(futuraBold);
        holder.promoBrief.setTypeface(futuraLight);

        // store holder with the view
        view.setTag(holder);
    }
    else {
        holder = (ViewHolder) view.getTag();
    }
    holder.headline.setText(item.getHeadline());
    holder.promoBrief.setText(item.getPromoBrief());
    holder.thumbnail.setImageUrl(UrlBuilder.buildUrl(item.getImgUrl()),
            VolleySingleton.getInstance(getActivity()).getImageLoader());


    return view;
}

private class ViewHolder {
    NetworkImageView thumbnail;
    TextView headline;
    TextView promoBrief;
}

我读过的所有内容都表明这应该可以正常工作,就像调用 addView 并将我的膨胀视图附加到 ViewGroup 容器(一个 LinearLayout)一样简单。我读过的一些内容建议在附加视图之前手动设置 LayoutParameters,但据我所知,这没有任何效果。有什么见解吗?

【问题讨论】:

  • 你找到解决方案了吗?

标签: android android-layout


【解决方案1】:

我有一个使用 ListView 的想法,即使您必须使用它上下滚动内容。您可以为 ListView 定义页脚和页眉,这使它像您期望的那样显示在上方和下方。对我来说,看起来像这样:

View headerView = inflater.inflate(R.layout.my_header_layout, null, false);
this.footer = new MyFooterView(getActivity());
this.interactionsList.addHeaderView(headerView);
this.interactionsList.addFooterView(this.footer);

像魅力一样工作。 :)

我找到了一个例子,注意上面和下面的元素,它们的行为就像它们在 ListView 中一样:https://www.youtube.com/watch?v=pAiHQAE5G6c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2020-07-26
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多