在ListView的视图中显示一行

时间:2016-02-22 10:27:39

标签: android listview

我有一个以编程方式创建的ListView,我需要在红色边框之间只显示 ONE 可见行而不是多个项目,并滚动以获取其他项目。

enter image description here

这是我的功能

    public void fillFormules(List<Category> objectsTextToShow)

{
    LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
    LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);

   LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    layout.setOrientation(LinearLayout.HORIZONTAL);

    for (int j = 0; j <objectsTextToShow.size() ; j++) {

        TextView textCat = new TextView(getActivity());
        textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
        textCat.setTextSize(28);
        textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
        textCat.setTextColor(colorUtils.TITLE);
        textCat.setGravity(Gravity.CENTER);

        relativeLayout.addView(textCat);
        textCat.setBackgroundColor(colorUtils.backgroundColor);
        LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
        parentLayout.setOrientation(LinearLayout.HORIZONTAL);
        List<Item> items = new ArrayList<Item>();
        ListView listView = new ListView(getActivity());
        for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++) {
            items.add(objectsTextToShow.get(j).getItems().get(i));

        }

        listView.setAdapter(new ScrollAdapter(getActivity(), items));

        parentLayout.addView(listView);
        layoutItemDetail.addView(parentLayout);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

我使用这个xml来给视图充气,即使我给匹配的父亲来说重量和高度但它不起作用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="3"
    android:textSize="25dp"
    android:text="TextView" />

这是我最终必须拥有的,但我不知道该怎么做:/

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以为用于显示列表中每个项目的Linearlayout设置修复高度。并将同样的高度添加到listView

LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, yourHeight, (float) 1.0));

在你的LinearLayout xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="yourHeight"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="3"
    android:textSize="25dp"
    android:text="TextView" />