我正在尝试此代码,但是更新了第三进度栏。 我想知道代码中的错误是什么?
package com.yasi.testview;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LayoutInflater inflater;
LinearLayout scrollContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
scrollContent = findViewById(R.id.scrollContent);
for (int i=1; i<4; i++){
View view = inflater.inflate(R.layout.test_item,scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);
progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);
}
}
}
这是我的项目的图片: Project screen Shot
此代码在主要活动布局中具有滚动视图:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/scrollContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我的test_item布局是下面的代码,其中包含进度条和文本视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/prgDownload"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:id="@+id/txtDownload"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="..." />
</RelativeLayout>
感谢您的回答。
答案 0 :(得分:0)
首先,不要一次又一次地在循环中获取新的元素实例,这是一种糟糕的方法,如下面的代码片段所述,其次,您只是一遍又一遍地使用一个进度条,并在同一线程上对其进行更新,需要在xml中有更多进度条,并且您可以动态生成,然后您可以运行循环并进行相应的更新。
package com.yasi.testview;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LayoutInflater inflater;
LinearLayout scrollContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**View view = inflater.inflate(R.layout.test_item,scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);**
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
scrollContent = findViewById(R.id.scrollContent);
for (int i=1; i<4; i++){
progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);
}
}
}
答案 1 :(得分:0)
声明小部件-循环外的视图并使用此代码:
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
return v;
}
如您所见,您将仅对@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_item, scrollContent);
scrollContent = findViewById(R.id.scrollContent);
ProgressBar progressBar = findViewById(R.id.prgDownload);
TextView textView = findViewById(R.id.txtDownload);
for (int i=0; i < 3; i++){
progressBar.setProgress((int) (Math.random() * 100));
textView.setText("Download # " + i);
}
}
ID为ProgressBar
进行更改,因此,您可能需要声明更多{{1 }} s 。
编辑:
要以编程方式显示prgDownload
,请使用以下方法:
ProgressBar
您可以在ProgressBar
循环内将此内容用于三个项目,但是,不建议您尝试显示循环内的方法 。