包含章节标题的Android ListView

时间:2015-05-16 06:15:24

标签: android listview sectionheader

请原谅我这里提到的任何内容是无关的,因为我是android开发的先驱。

我已经经历了许多SO问题和许多关于网络解释所有相同内容的例子,但它们都不符合我的要求,因为我需要动态地弹出标题和列表项。

最后来到这里寻求一些好的建议。

我的行布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" >


    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_card"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >


            <TableLayout
                android:id="@+id/tableLayout4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TableRow
                    android:id="@+id/tableRow5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/txtTitle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:layout_weight="1"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/txtTime"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginStart="20dp"
                        android:layout_weight="1"
                        android:textColor="#000"
                        android:textSize="24sp" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/txtCategory"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="12dp"
                        android:layout_marginStart="12dp"
                        android:layout_weight="1"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#0000FF" />
                </TableRow>
            </TableLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/btndelete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_weight="1"
            android:scaleType="fitEnd"
            android:src="@drawable/ic_menu_delete" />

    </LinearLayout>

</RelativeLayout>

这是我的BaseAdapter:

  class CustomAdapter extends BaseAdapter {

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

    private ArrayList<String> mData = new ArrayList<String>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

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

    public void addItem(final String title, String category,String time) {
        mData.add(title);
        mData.add(category);
        mData.add(time);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final String item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

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

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

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
            case TYPE_ITEM:
                //convertView = mInflater.inflate(R.layout.snippet_item1, null);
                convertView = mInflater.inflate(R.layout.adapter_listview, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.txtTitle);
                holder.txtCategory = (TextView) convertView.findViewById(R.id.txtCategory);
                holder.txtTime = (TextView) convertView.findViewById(R.id.txtTime);
                holder.btndelete = (ImageView) convertView.findViewById(R.id.btndelete);
                break;
            case TYPE_SEPARATOR:
                //convertView = mInflater.inflate(R.layout.adapter_listview, null);
                convertView = mInflater.inflate(R.layout.snippet_item1, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.text);
                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 TextView txtCategory;
        public TextView txtTime;
        public ImageView btndelete;
    }

}

这是我的家庭片段:

public class HomeFragment extends Fragment  {
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            private CustomAdapter mAdapter;
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);  
             listViewData = (ListView)rootView.findViewById(R.id.listViewData);



           return rootView;
  }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult called");
        if((requestCode == 1)&&(resultCode == Activity.RESULT_OK)){
            if(data!= null){
                   AddedTask=data.getStringExtra("NewTask");
                   CategoryName=data.getStringExtra("CategoryItem");
                   mAdapter.addSectionHeaderItem(CategoryName);
                   mAdapter.addItem(AddedTask);
                   mAdapter.addItem(TaskTime);
                   listViewData.setAdapter(mAdapter);
                   mAdapter.notifyDataSetChanged();
      }
    }
  }
}

结果输出为:

enter image description here

正如您所看到的,我有一个单行布局,其中包含所有视图,但每一行都在每行中添加。

与此相关的代码在我的适配器中:

public void addItem(final String title, String category,String time) {
    mData.add(title);
    mData.add(category);
    mData.add(time);
    notifyDataSetChanged();
}

现在我有两个问题。

1。如何更改addItem()以获得以下输出

enter image description here

2。如何在添加新标题时检查是否存在标题,并在该标题下方添加其余项目。

任何人都可以建议我更好的方法吗

2 个答案:

答案 0 :(得分:1)

将CustomAdapter更改为

  class CustomAdapter extends BaseAdapter {

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

private Map<String, ArrayList<String>> mData = new HashMap<String, ArrayList<String>>();

private LayoutInflater mInflater;

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

public void addItem(final String header, final String... contents) {
    if (mData.containsKey(header)) {
        ArrayList<String> item = new ArrayList<String>(mData.get(header));
        for(String content : contents) {
            item.add(content);
        }
        mData.remove(header);
        mData.put(header, item);
    } else {
        ArrayList<String> item = new ArrayList<String>();
        for(String content : contents) {
            item.add(content);
        }
        mData.put(header, item);
    }
    notifyDataSetChanged();
}


@Override
public int getItemViewType(int position) {
    return (position&1) == 0 ? TYPE_SEPARATOR : TYPE_ITEM;
}

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

@Override
public int getCount() {
    return mData.size() * 2;
}

@Override
public Object getItem(int position) {
    if (position&1 == 0) {
        Set<String> headers = mData.keySet();
        Iterator<String> iterator = headers.iterator();
        for (int i = 0; i < position / 2 && iterator.hasNext(); i++) {
            iterator.next();
        }
        return iterator.hasNext() ? iterator.next() : null;
    } else {
        Set<ArrayList<String>> items = mData.values();
        Iterator<ArrayList<String>> iterator = items.iterator();
        for (int i = 0; i < position / 2 && iterator.hasNext(); i++) {
            iterator.next();
        }
        return iterator.hasNext() ? iterator.next() : null;
    }
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    int rowType = getItemViewType(position);

    if (convertView == null) {
        switch (rowType) {
        case TYPE_ITEM:
            //convertView = mInflater.inflate(R.layout.snippet_item1, null);
            convertView = mInflater.inflate(R.layout.adapter_listview, parent, false);
            ArrayList<String> items = getItem(position);
            //handle your items here!
            (TextView) convertView.findViewById(R.id.txtTitle).setText()
            (TextView) convertView.findViewById(R.id.txtCategory).setText();
            (TextView) convertView.findViewById(R.id.txtTime).setText();
            (ImageView) convertView.findViewById(R.id.btndelete);
            break;
        case TYPE_SEPARATOR:
            //convertView = mInflater.inflate(R.layout.adapter_listview, null);
            convertView = mInflater.inflate(R.layout.snippet_item1, parent, false);
            (TextView) convertView.findViewById(R.id.text).setText(getItem(position));
            break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

}

答案 1 :(得分:0)

在此代码中:

public void addItem(final String title, String category,String time) {
mData.add(title);
mData.add(category);
mData.add(time);
notifyDataSetChanged();
}

你试图实际添加三个项目因此结果。

要获得所需的结果,请尝试以下方法:

class Item{
private String mTitle;
private String mCategory;
private String mTime;
public Item(String title,String time,String category){
mTitle=title;
mTime=time;
mCategory=category;
}
}

在addItem()中:

public void addItem(final String title, String category,String time) {
mData.add(new Item(title,category,time));
notifyDataSetChanged();
}

更改BaseAdapter类以从列表中获取Item类的对象,从而为elemnets分配其对应的标题,时间和类别

希望有所帮助:)