如何添加在android系统,以便自定义列表视图? [英] How to add a view to custom listview in android?

查看:199
本文介绍了如何添加在android系统,以便自定义列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在里面有进度的android活动取得的定制列表视图。在该列表视图顶部还有另外一个进度条,显示进度日复一日的一个月。现在我想根据其中有四个进度条定制的ListView月进度条的进度来设置一个吧。请参阅所附的图像。

I have made custom listview in android activity which has progressbar. On top of that listview there is another progress bar which shows progress as day passes in the month. Now i want to set a bar according to the progress of the month progress bar in custom listview which has four progress bars. Please see the attached image.

当月进度栏的进步,这是我们摆在列表视图中进度条应处于同一位置的装置对准酒吧和这家酒吧,我们把为过一天应该移动的顶部的进度增加时。

The progress of the month progress bar and the bar which we put in listview progress bar should be at same position means aligned and this bar which we put should move when the top progress increases as day passes.

在版式文件我用的LinearLayout,我想放了吧。

In Layout file I have used LinearLayout where I want to put the bar.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<RelativeLayout android:id="@+id/Top"
    android:layout_centerHorizontal="true" android:layout_width="250dip"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/LeftText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="O"
        android:textColor="#FFFFFF" android:textStyle="normal"
        android:layout_alignParentLeft="true" />
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentRight="true">
        <TextView android:id="@+id/TextOne" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="O"
            android:textColor="#FFFFFF" android:textStyle="normal"
            android:layout_marginRight="5dip" />
        <TextView android:id="@+id/TextTwo" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/outof"
            android:textColor="#FFFFFF" android:textStyle="normal"
            android:layout_toRightOf="@+id/TextOne" android:layout_marginRight="5dip" />
        <TextView android:id="@+id/RightText" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="O"
            android:textColor="#FFFFFF" android:textStyle="normal"
            android:layout_toRightOf="@+id/TextTwo" />
    </RelativeLayout>
</RelativeLayout>
<RelativeLayout android:id="@+id/Bottom"
    android:layout_below="@+id/Top" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerHorizontal="true">
    <ProgressBar android:id="@+id/CustomPro"
        android:layout_width="250dip" android:layout_height="wrap_content"
        android:progress="0" android:max="100" android:secondaryProgress="0"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/accountdataprogress"
        android:layout_centerHorizontal="true" />
    <TextView android:id="@+id/MiddleText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="0%"
        android:textColor="#000000" android:textStyle="bold"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:id="@+id/Middle"
    android:layout_centerHorizontal="true" android:layout_width="250dip"
    android:layout_height="wrap_content" android:layout_below="@+id/Top">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/PuttingBar"
        android:layout_alignParentLeft="true">
    </LinearLayout>
</RelativeLayout>
<View android:layout_width="fill_parent" android:layout_height="10dip"
    android:background="#00000000" android:layout_below="@+id/Bottom" />
</RelativeLayout>

在Java中我做了这样的事情。

In Java I have done something like this.

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressListAdapter extends BaseAdapter 
{
private Context mContext;
private ArrayList<ProgressList> mPList;
private TextView mLeft, mMiddle, mRight, mTextOne;
private ProgressBar mCustomProgressBar;
private LinearLayout mLinearLayout;

public ProgressListAdapter(Context nContext, ArrayList<ProgressList> nPList) 
{
    this.mContext = nContext;
    this.mPList = nPList;
}

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

@Override
public Object getItem(int nPosition) 
{
    return mPList.get(nPosition);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ProgressList mEntry = mPList.get(position);
    if (convertView == null) 
    {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.customprogress, null);
    }

    mLeft = (TextView) convertView.findViewById(R.id.LeftText);
    mLeft.setText(mEntry.getValueText());

    mMiddle = (TextView) convertView.findViewById(R.id.MiddleText);
    mCustomProgressBar = (ProgressBar) convertView.findViewById(R.id.CustomPro);
    int mBarPosition = Usage.mElapsed;

    mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
    View mView = new View(mContext);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(2, 25);
    layoutParams.setMargins(mBarPosition, 0, 0, 0);
    mView.setMinimumHeight(25);
    mView.setMinimumWidth(2);
    mView.setBackgroundColor(Color.rgb(12, 56, 99));
    mLinearLayout.addView(mView, layoutParams);
    mView.invalidate();

    if(mEntry.getValueNumber() > mBarPosition)
    {
        mCustomProgressBar.setProgress(mBarPosition);
        mCustomProgressBar.setSecondaryProgress(mEntry.getValueNumber());
    }
    else if(mEntry.getValueNumber() <= mBarPosition)
    {
        mCustomProgressBar.setProgress((mEntry.getValueNumber()));
        mCustomProgressBar.setSecondaryProgress(0);
    }
    mMiddle.setText(Integer.toString(mEntry.getValueNumber()) + "%");

    mTextOne = (TextView) convertView.findViewById(R.id.TextOne);
    mTextOne.setText(mEntry.getValue());
    mRight = (TextView) convertView.findViewById(R.id.RightText);
    mRight.setText(mEntry.getValueOne());

    return convertView;
}
}

但是当我运行它显示了这样的进度条额外栏

But When I run It shows extra bar in progress bar like this

任何人做过这种工作,请给我你的手。等待卓有成效的答复。
谢谢你。

Anybody has done kind of work please give me your hand. Waiting for fruitful reply. Thanks.

推荐答案

在您应该添加调用 removeAllViews 你的 getView funcion:

You should add call to removeAllViews in your getView funcion:

mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
mLinearLayout.removeAllViews(); 
View mView = new View(mContext);

这是因为的LinearLayout 积累的意见,并有可能是你的 addView 函数多次调用。所以,你需要添加之前删除已添加另外一个 PuttingBar 新的保证金。

That's because LinearLayout accumulates views, and there might be multiple calls to your addView function. So you need to remove already added view before adding another PuttingBar with new margin.

这篇关于如何添加在android系统,以便自定义列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆