好的,现在我有一个 ListView
,它正在通过 PHP 脚本填充信息。
现在,列表一次加载一个。通过这个,我的意思是用户可以看到每个列表何时加载(例如他们看到一个项目,一秒钟后他们看到第二个项目,等等)我想要做的是等待直到所有项目都被检索然后显示他们一次。在发生这种情况时,要有某种类型的“加载”指示器(可能是旋转的圆圈类型交易)。有什么办法可以实现吗?这是我的代码:
public class MainActivity extends ActionBarActivity {
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// Construct the data source
arrayOfLocations = new ArrayList<Location>();
// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);
getData();
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
}
因此,我的 getData() 方法将每个 Location 添加到适配器中,然后 然后我的 Adapter 类将数据放入 ListView:
public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(Context context, ArrayList<Location> locations) {
super(context, R.layout.item_location, locations);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Location location = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.item_location, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDetails = (TextView) convertView
.findViewById(R.id.tvDetails);
TextView tvDistance = (TextView) convertView
.findViewById(R.id.tvDistance);
TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
// Populate the data into the template view using the data object
tvName.setText(location.name);
tvDetails.setText(location.details);
tvDistance.setText(location.distance);
tvHours.setText(location.hours);
ivIcon.setImageBitmap(location.icon);
// Return the completed view to render on screen
return convertView;
}
}
另外,我有一个简单的加载指示器的代码:
public class MainActivity extends Activity {
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);
}
public void open(View view) {
progress.setMessage("Loading...Please Wait");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while (jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
所以基本上,我想做的是弄清楚如何做到这一点: 1. Activity 启动时,显示“loading”指示器 2.将所有项目加载到ListView 3.当所有项目都列出来后,移除“loading”指示器并显示ListView
有什么想法吗?谢谢。
最佳答案
另一种实现此目的的简单方法是让 ProgressBar 在您的 View 中可见,并在处理完成时将其隐藏:
<RelativeLayout
android:id="@+id/app_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<FrameLayout
android:id="@+id/loading_progress_container"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="true">
<ProgressBar
android:id="@+id/list_progress_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<com.example.MyListView
android:id="@+id/my_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</RelativeLayout>
然后在你的请求回调中执行如下操作:
final View progressView = containerView.findViewById(R.id.loading_progress_container);
final View myListView = containerView.findViewById(R.id.my_list_view);
activity.runOnUiThread(new Runnable() {
progressView.setVisibility(View.GONE);
myListView.setVisibility(View.VISIBLE);
});
显然,对于上述代码,您需要引用您的容器 View 和 Activity 。
关于java - 如何在 Android 应用程序中实现 "loading"指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23999334/