根据发布日期显示列表项目

时间:2014-08-06 12:58:20

标签: android listview

我正在创建一个自定义列表。在列表中,我显示了用户上传的文档。我的列表如下所示 enter image description here

我希望列表标题应该包含月份,并且该月份上传的所有文档都应该在该标题下。 例如像这个列表

enter image description here

Xml代码

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv_docs_image"
            android:adjustViewBounds="true" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginLeft="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="File Name"
                android:id="@+id/tv_file_name"
                android:textSize="25sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/tv_posted_on" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_weight="2.6"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/share_list"
                android:layout_gravity="end"
                android:focusable="false"
                android:id="@+id/bt_share" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

P.s-所有列表内容都来自本地创建的数据库

1 个答案:

答案 0 :(得分:0)

看看这个项目:

http://code.google.com/p/android-section-list/

祝你好运!

这是上面链接的项目源代码中的一个用法示例。

public class SectionListActivity extends Activity {



private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> {



    private final SectionListItem[] items;



    public StandardArrayAdapter(final Context context,

            final int textViewResourceId, final SectionListItem[] items) {

        super(context, textViewResourceId, items);

        this.items = items;

    }



    @Override

    public View getView(final int position, final View convertView,

            final ViewGroup parent) {

        View view = convertView;

        if (view == null) {

            final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = vi.inflate(R.layout.example_list_view, null);

        }

        final SectionListItem currentItem = items[position];

        if (currentItem != null) {

            final TextView textView = (TextView) view

                    .findViewById(R.id.example_text_view);

            if (textView != null) {

                textView.setText(currentItem.item.toString());

            }

        }

        return view;

    }

}



SectionListItem[] exampleArray = { // Comment to prevent re-format

new SectionListItem("Test 1 - A", "A"), //

        new SectionListItem("Test 2 - A", "A"), //

        new SectionListItem("Test 3 - A", "A"), //

        new SectionListItem("Test 4 - A", "A"), //

        new SectionListItem("Test 5 - A", "A"), //

        new SectionListItem("Test 6 - B", "B"), //

        new SectionListItem("Test 7 - B", "B"), //

        new SectionListItem("Test 8 - B", "B"), //

        new SectionListItem("Test 9 - Long", "Long section"), //

        new SectionListItem("Test 10 - Long", "Long section"), //

        new SectionListItem("Test 11 - Long", "Long section"), //

        new SectionListItem("Test 12 - Long", "Long section"), //

        new SectionListItem("Test 13 - Long", "Long section"), //

        new SectionListItem("Test 14 - A again", "A"), //

        new SectionListItem("Test 15 - A again", "A"), //

        new SectionListItem("Test 16 - A again", "A"), //

        new SectionListItem("Test 17 - B again", "B"), //

        new SectionListItem("Test 18 - B again", "B"), //

        new SectionListItem("Test 19 - B again", "B"), //

        new SectionListItem("Test 20 - B again", "B"), //

        new SectionListItem("Test 21 - B again", "B"), //

        new SectionListItem("Test 22 - B again", "B"), //

        new SectionListItem("Test 23 - C", "C"), //

        new SectionListItem("Test 24 - C", "C"), //

        new SectionListItem("Test 25 - C", "C"), //

        new SectionListItem("Test 26 - C", "C"), //

};



private StandardArrayAdapter arrayAdapter;



private SectionListAdapter sectionAdapter;



private SectionListView listView;



@Override

public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view,

            exampleArray);

    sectionAdapter = new SectionListAdapter(getLayoutInflater(),

            arrayAdapter);

    listView = (SectionListView) findViewById(getResources().getIdentifier(

            "section_list_view", "id",

            this.getClass().getPackage().getName()));

    listView.setAdapter(sectionAdapter);

}



@Override

public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.test_menu, menu);

    return true;

}



@Override

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.standard_list:

        arrayAdapter = new StandardArrayAdapter(this,

                R.id.example_text_view, exampleArray);

        sectionAdapter = new SectionListAdapter(getLayoutInflater(),

                arrayAdapter);

        listView.setAdapter(sectionAdapter);

        return true;

    case R.id.empty_list:

        arrayAdapter = new StandardArrayAdapter(this,

                R.id.example_text_view, new SectionListItem[] {});

        sectionAdapter = new SectionListAdapter(getLayoutInflater(),

                arrayAdapter);

        listView.setAdapter(sectionAdapter);

        return true;

    default:

        return super.onOptionsItemSelected(item);

    }

}

这里使用的布局XML文件:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView"

    android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

    <pl.polidea.sectionedlist.SectionListView android:id="@+id/section_list_view" android:layout_width="fill_parent"

            android:layout_height="fill_parent">

    </pl.polidea.sectionedlist.SectionListView>

</FrameLayout>