我正在构建的客户端需要一个功能,需要我围绕很多内容包含滚动视图,包括“新闻”部分。因此,我无法使用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,但就我所知,这没有任何影响。有什么见解吗?
答案 0 :(得分:0)
我有一个使用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