0

我正在尝试填充 2 LinearLayout 以实现如下目的: 在此处输入图像描述

这是一个简单的测试,它没有完全实现我想要的代码。

问题是在第一个池之后,linkedList 是空的,池之前的大小是 2。

我的布局来填充每个列空间。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/latest_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/latest_preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:background="@drawable/card_w_shadow"
    android:onClick="onTopicsPreviewClick"
    android:orientation="vertical"
    android:paddingBottom="16dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" >

        <TextView
            android:id="@+id/latest_preview_title"
            style="@style/CardTitle_small"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="title" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="4dp"
        android:background="@color/C_Moodle_Orange" />

    <LinearLayout
        android:id="@+id/latest_description_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="4dp" >

        <TextView
            android:id="@+id/latest_preview_description"
            style="@style/CardText_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:ellipsize="end"
            android:maxLines="4"
            android:text="Lorem ipsum dolor sit amet" />
    </LinearLayout>
</LinearLayout>

我的片段:

public class FragLatest extends Fragment {

@SuppressWarnings("unchecked")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Context context = getActivity().getApplicationContext();
    ManContents content = new ManContents(context);

    // The Linked List, in this example is size =2 and it contains
    // Mancontents objects
    LinkedList<ManLatest> latestList = (LinkedList<ManLatest>) new ManDataStore(
            context).getData("Latest");

    // The Vertical LinearLayout
    LinearLayout outerLayout = new LinearLayout(context);
    outerLayout.setLayoutParams(new LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    outerLayout.setOrientation(LinearLayout.VERTICAL);

    outerLayout.addView(setLayoutTitle(context));

    // Get from resource the number of cards per line
    int cardsPerLine = getResources().getInteger(
            R.integer.latest_item_per_line);

    // Calculate the number of rows to build
    int lines = setRows(latestList, cardsPerLine);

    for (int i = 0; i < lines; i++) {

        // The Horizontal Linear Layout
        LinearLayout innerLayout = new LinearLayout(context);
        innerLayout.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT));
        innerLayout.setOrientation(LinearLayout.HORIZONTAL);

        for (int j = 0; j < cardsPerLine; j++) {

            ManLatest latest = latestList.poll();

            Long topicId = Long.parseLong(latest.getTopicId());
            String courseId = latest.getCourseId();
            MoodleCourseContent[] courseContent = content
                    .getContent(courseId);
            MoodleCourseContent topic = content.getTopic(topicId,
                    courseContent);

            View view = inflater.inflate(R.layout.frag_latest, null);
            view.setLayoutParams(new LayoutParams(
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1f));
            TextView title = (TextView) view
                    .findViewById(R.id.latest_preview_title);
            title.setText(topic.getName());
            TextView description = (TextView) view
                    .findViewById(R.id.latest_preview_description);
            description.setText(topic.getSummary());
            innerLayout.addView(view);

        }
        outerLayout.addView(innerLayout);

    }

    return outerLayout;

}

/**
 * @param latest
 * @param cardsPerLine
 * @return
 */
private int setRows(LinkedList<ManLatest> latest, int cardsPerLine) {
    int colums;
    if (latest.size() > cardsPerLine) {
        Double count = Math.ceil(latest.size() / 2.0);
        colums = count.intValue();
    } else {
        colums = 1;
    }
    return colums;
}

/**
 * @param context
 * @return
 */
private TextView setLayoutTitle(Context context) {
    TextView large_title = new TextView(context);
    large_title.setLayoutParams(new LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    large_title.setText("Latest Contents");
    large_title.setTextAppearance(context, R.style.CardsLayoutTitle);
    // large_title.setTypeface(null, Typeface.ITALIC);
    large_title.setPadding(30, 10, 0, 10);
    return large_title;
}
4

0 回答 0