在ListView Android中添加标题

时间:2014-02-03 02:52:27

标签: android xml header textview android-linearlayout

目前我对listview的xml代码如下:

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>


</LinearLayout>

现在每个第5项之后,我想添加一个静态标题。所以它是固定的。标题的xml代码如下:

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

    <TextView  
        android:id="@+id/list_header_title"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
        android:paddingTop="2dip"  
        android:paddingBottom="2dip"  
        android:background="#008000"
        android:textColor="@android:color/white"
        android:paddingLeft="5dip"  
        style="?android:attr/listSeparatorTextViewStyle" />
 </LinearLayout>

现在我如何合并?有关于添加标题到列表和部分列表视图等的在线文章,以编程方式进行。但我的并不是那么复杂。

修复后,在列表视图中每隔五个项目后添加一个标题。知道如何合并上面的两组代码吗?

先谢谢

2 个答案:

答案 0 :(得分:1)

制作一个包含2项活动的新项目

activity_main1.xml中添加此代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/text"
android:layout_height="50dp"
android:gravity="center_vertical"
android:text="text"
android:visibility="visible"
android:layout_width="fill_parent"
android:textColor="#FF000000"
android:background="#FFFFFFFF" />
</LinearLayout>

activity_main2.xml中添加此代码

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/textSeparator"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text"
android:visibility="visible"
android:layout_width="fill_parent"
android:textColor="#FFFFFFFF"
android:background="#000" />
</LinearLayout>

在您的MainActivity.java文件中添加此

public class MainActivity extends ListActivity implements OnTouchListener{

private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = new MyCustomAdapter();
    for (int i = 1; i < 50; i++) {
        mAdapter.addItem("ListView item " + i);
        if (i % 5 == 0) {
            mAdapter.addSeparatorItem("Header " + i);
        }
    }
    setListAdapter(mAdapter);
}
 //Adapter Class
private class MyCustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    public int getCount() {
        return mData.size();
    }

    public String getItem(int position) {
        return mData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.activity_main1, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.activity_main2, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}

执行 Ctrl + Shift + O 然后运行

答案 1 :(得分:0)

要创建包含剖面或标题的列表,请参阅[http://danielme.com/tip-android-27-listview-con-secciones-o-cabeceras/] [1]

[1]:http://danielme.com/tip-android-27-listview-con-secciones-o-cabeceras/