How to add a view to custom listview in android?

人走茶凉 提交于 2019-12-11 08:15:57

问题


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.

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>

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.


回答1:


You should add call to removeAllViews in your getView funcion:

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

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.



来源:https://stackoverflow.com/questions/6197236/how-to-add-a-view-to-custom-listview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!