列表项目背景上的进度条

时间:2014-01-27 10:14:57

标签: android android-layout android-listview android-progressbar

我希望列表项的背景像进度条一样工作。 例如,在tTorrent文件列表中: enter image description here

现在就是这样做的:我使用了relativelayout和两个线性输出。一个有textview,另一个有两个视图作为进度条。使用视图的权重更改“进度”,这是在getView方法中动态设置的。然后将带有textview的linearlayout放在前面。这是代码:

布局:          

<LinearLayout
    android:padding="5dp"
    android:id="@+id/catStatsRowLay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- textviews on the front -->
    ...
</LinearLayout>

<!-- views which work as progress bar -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/catStatsRowLay"
    android:layout_alignLeft="@id/catStatsRowLay"
    android:layout_alignRight="@id/catStatsRowLay"
    android:layout_alignTop="@id/catStatsRowLay"
    android:layout_below="@id/catStatsRowLay"
    android:layoutDirection="rtl"
    android:orientation="horizontal" >

    <View
        android:id="@+id/catStatsRowBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".70"
        android:background="#cc550000" />

    <View
        android:id="@+id/catStatsRowBar2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".30"
        android:background="@android:color/transparent" />
</LinearLayout>

</RelativeLayout>

getView方法:

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.categories_stats_list_row,
                parent, false);

        //filling textviews
        ...

        //getting bars that work as progressbar
        View bar = (View) rowView.findViewById(R.id.catStatsRowBar);
        View bar2 = (View) rowView.findViewById(R.id.catStatsRowBar2);
        //calculating and setting weights
        float percent = cs.getBarWeight();
        LinearLayout.LayoutParams barPar = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, (1-percent));
        LinearLayout.LayoutParams bar2Par = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, percent);

        bar.setLayoutParams(barPar);
        bar2.setLayoutParams(bar2Par);
        bar.setBackgroundColor(cs.getColor(position));
        //bringing LinearLayout with textviews to front
        LinearLayout ll = (LinearLayout) rowView
                .findViewById(R.id.catStatsRowLay);
        ll.bringToFront();

        return rowView;
    }

它完美适用于4.2.2及更低版本的设备(截图如下)。

enter image description here

问题:4.3及更高版本(模拟器和设备)都没有显示“进度”视图。我尝试不将带有textviews的linearlayout带到前面,将权重设置为常量,更改linearlayout的对象 - 没有结果,“progress”视图没有显示。我怎样才能使它适用于新版本的android?

1 个答案:

答案 0 :(得分:5)

解决方案很简单。

在FrameLayout上替换RelativeLayout后,现在一切似乎都正常工作,设备和模拟器4.3 +

getView方法的代码是相同的,除了将带有textview的LinearLayout带到前面 - 现在不需要它。

布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
 >
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    android:orientation="horizontal" >


    <View
        android:id="@+id/catStatsRowBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".70"
        android:background="#cc550000" />

    <View
        android:id="@+id/catStatsRowBar2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".30"
        android:background="@android:color/transparent" />
</LinearLayout>

<LinearLayout
    android:padding="5dp"
    android:id="@+id/catStatsRowLay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- textviews on the front -->

</LinearLayout>    

</FrameLayout> 

出于某种原因,relativelayout在4.3+上以不同的方式工作,我仍然不知道为什么。

感谢来自ttorrent团队的Gabor向我展示了正确的方法。