如何添加粘头的列表视图与背景图像列表行中的android?列表、视图、图像、背景

2023-09-06 18:09:58 作者:终是笑话

我想补充粘头的列表视图。我实现了与code,我发现在 https://github.com/帮助beworker /固定截面-列表视图

下面是我在其中得到了列表视图行作为图片

它工作正常,但我需要在上面行定制。有用于单排,它们是)listheader.xml湾)listrow.xml两个XML两个单独的布局被两个分开的列表视图的单个行的份

现在我想要做的是,列表行头应与透明背景(listheader.xml)和listrow.xml不应该有一个人背着相机的背景图像。图像应被设置为每个列表视图行的背景。这将类似于下

所以我的列表视图列将有一个背景图片,而不是上面的图片,但在图像上,你可以在上面的图片看到一个头。 有谁能够告诉我,我怎么能做到这一点。 下面是我的$ C $适配器我用了C。

 公共抽象类SectionAdapter扩展了BaseAdapter工具
        OnItemClickListener {

    私人诠释mCount = -1;

    上下文语境;

    公共SectionAdapter(上下文C){
        this.context = C;
        // TODO自动生成构造函数存根
    }

    公共抽象INT增加NumberOfSections();

    公共抽象INT numberOfRows(INT部分);

    公共抽象查看getRowView(INT部分,诠释行,查看convertView,
            ViewGroup中父);

    公共抽象对象getRowItem(INT部分,诠释行);

    公共布尔hasSectionHeaderView(INT部分){
        返回false;
    }

    公共查看getSectionHeaderView(INT部分,查看convertView,
            ViewGroup中父){
        返回null;
    }

    公共对象getSectionHeaderItem(INT部分){
        返回null;
    }

    公众诠释getRowViewTypeCount(){
        返回1;
    }

    公众诠释getSectionHeaderViewTypeCount(){
        返回0;
    }

    / **
     *必须介于0和getRowViewTypeCount()返回一个值(不包括)
     * /
    公众诠释getRowItemViewType(INT部分,诠释行){
        返回0;
    }

    / **
     *必须介于0和getSectionHeaderViewTypeCount返回值()
     *(排除在外,如果大于0)
     * /
    公众诠释getSectionHeaderItemViewType(INT部分){
        返回0;
    }

    @覆盖
    / **
     *调度呼叫onRowItemClick
     * /
    公众最终无效onItemClick(适配器视图<>母公司视图中查看,
            INT位置,长的id){
        onRowItemClick(父母,看来,getSection(位置),
                getRowInSection(位置),身份证);
    }

    公共无效onRowItemClick(适配器视图<>母公司视图中查看,INT部分,
            INT排,长的id){

    }

    @覆盖
    / **
     *计数单元=标题+行量
     * /
    公众最终诠释getCount将(){
        如果(mCount℃,){
            mCount = numberOfCellsBeforeSection(增加NumberOfSections());
        }
        返回mCount;
    }

    @覆盖
    公共布尔的isEmpty(){
        返回getCount将()== 0;
    }

    @覆盖
    / **
     *调度呼叫getRowItem或getSectionHeaderItem
     * /
    公众最终目标的getItem(INT位置){
        INT部分= getSection(位置);
        如果(isSectionHeader(位置)){
            如果(hasSectionHeaderView(部分)){
                返回getSectionHeaderItem(部分);
            }
            返回null;
        }
        返回getRowItem(部分,getRowInSection(位置));
    }

    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }

    @覆盖
    / **
     *调度呼叫getRowView或getSectionHeaderView
     * /
    公共最后查看getView(INT位置,查看convertView,ViewGroup中父){

        / *
         * RelativeLayout的RL =新RelativeLayout的(上下文);的LayoutParams
         * listParams =新的LayoutParams(LayoutParams.MATCH_PARENT,
         * LayoutParams.MATCH_PARENT); rl.setLayoutParams(listParams);
         * convertView =(查看)RL;
         * /

        INT部分= getSection(位置);
        如果(isSectionHeader(位置)){
            如果(hasSectionHeaderView(部分)){
                返回getSectionHeaderView(部分,convertView,父母);
            }
            返回null;
        }
        返回getRowView(部分,getRowInSection(位置),convertView,
                父);
        //返回convertView;
    }

    / **
     *返回所指示的小区的区段号
     * /
    保护INT getSection(INT位置){
        INT节= 0;
        而(numberOfCellsBeforeSection(部分)< =位置){
            部分++;
        }
        返回部分 -  1;
    }

    / **
     *返回指定的小区不宜与调用的行索引
     *位置定向到节头
     * /
    保护INT getRowInSection(INT位置){
        INT部分= getSection(位置);
        INT行=位置 -  numberOfCellsBeforeSection(部分);
        如果(hasSectionHeaderView(部分)){
            返回行 -  1;
        } 其他 {
            返回行;
        }
    }

    / **
     *如果这个索引处的细胞是节头返回true
     * /
    保护布尔isSectionHeader(INT位置){
        INT部分= getSection(位置);
        返回hasSectionHeaderView(部分)
                &功放;&安培; numberOfCellsBeforeSection(部分)==地位;
    }

    / **
     *所指示的前返回单元的数目(=标题+行)
     * 部分
     * /
    保护INT numberOfCellsBeforeSection(INT部分){
        诠释计数= 0;
        的for(int i = 0; I< Math.min(增加NumberOfSections(),一节);我++){
            如果(hasSectionHeaderView(i))的{
                数+ = 1;
            }
            数+ = numberOfRows(我);
        }
        返回计数;
    }

    @覆盖
    公共无效notifyDataSetChanged(){
        super.notifyDataSetChanged();
        mCount = numberOfCellsBeforeSection(增加NumberOfSections());
    }

    @覆盖
    公共无效notifyDataSetInvalidated(){
        super.notifyDataSetInvalidated();
        mCount = numberOfCellsBeforeSection(增加NumberOfSections());
    }

    @覆盖
    / **
     *调度呼叫getRowItemViewType或getSectionHeaderItemViewType
     * /
    公众最终诠释getItemViewType(INT位置){
        INT部分= getSection(位置);
        如果(isSectionHeader(位置)){
            返回getRowViewTypeCount()
                    + getSectionHeaderItemViewType(部分);
        } 其他 {
            返回getRowItemViewType(部分,getRowInSection(位置));
        }
    }

    @覆盖
    / **
     *调度呼叫getRowViewTypeCount和getSectionHeaderViewTypeCount
     * /
    公众最终诠释getViewTypeCount(){
        返回getRowViewTypeCount()+ getSectionHeaderViewTypeCount();
    }

    @覆盖
    / **
     *默认情况下,禁用节头
     * /
    公共布尔的IsEnabled(INT位置){
        返回isSectionHeader(位置)!;
    }
}
 

解决方案

问题

您希望您的listheader.xml要在列表视图顶部 您想上的ListView每个项目都有自己的背景

解决方案

css 插入图片实时视图能显示 网页显示不了

要带上你的 listheader.xml 的ListView 你应该做这样的事情在顶部的主要活动XML

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 的xmlns:工具=htt​​p://schemas.android.com/tool​​s
 机器人:layout_width =match_parent
 机器人:layout_height =match_parent>


  <包括
    机器人:ID =@ + ID / listheader
    机器人:layout_alignParentTop =真
    布局=@布局/ listheader/>
  。 <! - 不要忘记你的listheader.xml应该有透明背景 - >

  <的ListView
    机器人:ID =@ + ID / yourList
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_alignParentTop =真
    机器人:分隔=@机器人:彩色/透明>
  < / ListView控件>

< / RelativeLayout的>
 

要设置背景到您的列表视图项目,你应该这样做:

 公共最后查看getView(INT位置,查看convertView,ViewGroup中父){


  / **
   *我没有时间去研究你的code会发生什么在这里,
   *但我知道,你应该在这里实现的可能的解决方案:)
   *在这里,我将张贴code,它提出的背景
   *到ListView的排在这个位置,如图
   *在你的形象。所以,在这里,你把你的情况为你
   *的行是相同的如上述的图像中已示出
   * * /

    如果(/ *把你的条件在这里,以使行会是一样的,在图像* /){
     LayoutInflater充气=(LayoutInflater)context.getApplicationContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
    rowView = inflater.inflate(R.layout.listrow,NULL); //这是你的listrow.xml
      //我想你的XML有作为根元素的相对布局
      RelativeLayout的rLayout =(RelativeLayout的)rowView .findViewById(R.id.rLayout);
       资源RES = contetx.getResources(); //资源句柄
        可绘制绘制= res.getDrawable(R.drawable.newImage); //在这里你将根据你的行位置或任何你的条件是,你要对每行图像

      rLayout.setBackgroundDrawable(绘制);
    }

  }
 

I am trying to add sticky header in listview. I implemented that with the help of code which I found at https://github.com/beworker/pinned-section-listview

Below is the picture in which I got the listview row as

Its working fine but I need to customize above row. there are two separate layout for a single row which are a.) listheader.xml b.) listrow.xml both the xml are two separate parts of a single row of listview.

Now what I want to do is that list row header should be with transparent background(listheader.xml) and listrow.xml should not have the background image of a guy carrying camera. the image should be set to background of each listview row. which will look like below

so my listview row will have a background image and a header not above image but over the image as you can see in the above image. can anybody tell me how can I do that. below is my code of adapter I used.

public abstract class SectionAdapter extends BaseAdapter implements
        OnItemClickListener {

    private int mCount = -1;

    Context context;

    public SectionAdapter(Context c) {
        this.context = c;
        // TODO Auto-generated constructor stub
    }

    public abstract int numberOfSections();

    public abstract int numberOfRows(int section);

    public abstract View getRowView(int section, int row, View convertView,
            ViewGroup parent);

    public abstract Object getRowItem(int section, int row);

    public boolean hasSectionHeaderView(int section) {
        return false;
    }

    public View getSectionHeaderView(int section, View convertView,
            ViewGroup parent) {
        return null;
    }

    public Object getSectionHeaderItem(int section) {
        return null;
    }

    public int getRowViewTypeCount() {
        return 1;
    }

    public int getSectionHeaderViewTypeCount() {
        return 0;
    }

    /**
     * Must return a value between 0 and getRowViewTypeCount() (excluded)
     */
    public int getRowItemViewType(int section, int row) {
        return 0;
    }

    /**
     * Must return a value between 0 and getSectionHeaderViewTypeCount()
     * (excluded, if > 0)
     */
    public int getSectionHeaderItemViewType(int section) {
        return 0;
    }

    @Override
    /**
     * Dispatched to call onRowItemClick
     */
    public final void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        onRowItemClick(parent, view, getSection(position),
                getRowInSection(position), id);
    }

    public void onRowItemClick(AdapterView<?> parent, View view, int section,
            int row, long id) {

    }

    @Override
    /**
     * Counts the amount of cells = headers + rows
     */
    public final int getCount() {
        if (mCount < 0) {
            mCount = numberOfCellsBeforeSection(numberOfSections());
        }
        return mCount;
    }

    @Override
    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override
    /**
     * Dispatched to call getRowItem or getSectionHeaderItem
     */
    public final Object getItem(int position) {
        int section = getSection(position);
        if (isSectionHeader(position)) {
            if (hasSectionHeaderView(section)) {
                return getSectionHeaderItem(section);
            }
            return null;
        }
        return getRowItem(section, getRowInSection(position));
    }

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

    @Override
    /**
     * Dispatched to call getRowView or getSectionHeaderView
     */
    public final View getView(int position, View convertView, ViewGroup parent) {

        /*
         * RelativeLayout rl = new RelativeLayout(context); LayoutParams
         * listParams = new LayoutParams(LayoutParams.MATCH_PARENT,
         * LayoutParams.MATCH_PARENT); rl.setLayoutParams(listParams);
         * convertView = (View)rl;
         */

        int section = getSection(position);
        if (isSectionHeader(position)) {
            if (hasSectionHeaderView(section)) {
                return getSectionHeaderView(section, convertView, parent);
            }
            return null;
        }
        return getRowView(section, getRowInSection(position), convertView,
                parent);
        // return convertView;
    }

    /**
     * Returns the section number of the indicated cell
     */
    protected int getSection(int position) {
        int section = 0;
        while (numberOfCellsBeforeSection(section) <= position) {
            section++;
        }
        return section - 1;
    }

    /**
     * Returns the row index of the indicated cell Should not be call with
     * positions directing to section headers
     */
    protected int getRowInSection(int position) {
        int section = getSection(position);
        int row = position - numberOfCellsBeforeSection(section);
        if (hasSectionHeaderView(section)) {
            return row - 1;
        } else {
            return row;
        }
    }

    /**
     * Returns true if the cell at this index is a section header
     */
    protected boolean isSectionHeader(int position) {
        int section = getSection(position);
        return hasSectionHeaderView(section)
                && numberOfCellsBeforeSection(section) == position;
    }

    /**
     * Returns the number of cells (= headers + rows) before the indicated
     * section
     */
    protected int numberOfCellsBeforeSection(int section) {
        int count = 0;
        for (int i = 0; i < Math.min(numberOfSections(), section); i++) {
            if (hasSectionHeaderView(i)) {
                count += 1;
            }
            count += numberOfRows(i);
        }
        return count;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        mCount = numberOfCellsBeforeSection(numberOfSections());
    }

    @Override
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        mCount = numberOfCellsBeforeSection(numberOfSections());
    }

    @Override
    /**
     * Dispatched to call getRowItemViewType or getSectionHeaderItemViewType
     */
    public final int getItemViewType(int position) {
        int section = getSection(position);
        if (isSectionHeader(position)) {
            return getRowViewTypeCount()
                    + getSectionHeaderItemViewType(section);
        } else {
            return getRowItemViewType(section, getRowInSection(position));
        }
    }

    @Override
    /**
     * Dispatched to call getRowViewTypeCount and getSectionHeaderViewTypeCount
     */
    public final int getViewTypeCount() {
        return getRowViewTypeCount() + getSectionHeaderViewTypeCount();
    }

    @Override
    /**
     * By default, disables section headers
     */
    public boolean isEnabled(int position) {
        return !isSectionHeader(position);
    }
}

解决方案

Problem

You want your listheader.xml to be on top of listview You want that each item on listView to have its own background

Solution

To bring your listheader.xml on top of listView you should do something like this in your main activity XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >


  <include
    android:id="@+id/listheader"
    android:layout_alignParentTop="true"
    layout="@layout/listheader" />
  . <!-- dont forget your listheader.xml should have transparent background -->

  <ListView
    android:id="@+id/yourList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:divider="@android:color/transparent" >
  </ListView>

</RelativeLayout>

To set background to your list view items you should do this:

 public final View getView(int position, View convertView, ViewGroup parent) {


  /**
   * I didnt have time to study your code about what happens here,
   * but I know the possible solution that you should be implemented here :)
   * Here I will post the code that puts background 
   * to listView row at this position as shown
   * in your image. So here you put your conditions for your 
   * rows that are the same as in the image above that you have shown
   * */

    if( /* put the your conditions here so that the row would be the same as in the image */ ){
     LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(R.layout.listrow, null); // this is your listrow.xml
      // i guess your xml has as root element a relative layout
      RelativeLayout rLayout = (RelativeLayout) rowView .findViewById (R.id.rLayout);
       Resources res = contetx.getResources(); //resource handle
        Drawable drawable = res.getDrawable(R.drawable.newImage);// here you put your image that you want for each row based on your row position or whatever your condition is

      rLayout.setBackgroundDrawable(drawable);
    }

  }