我正在使用ListView
安卓系统。ListView
我知道如何使用常用方式填写 android 。但是假设我们有一个像下面这样的类,它包含两个TextView
,aButton
和 a ProgressBar
。我已经声明RelativeLayout
并成功在此布局中添加了所有视图。(经测试使用setContentView(new ListViewItem(this).getLayout());
基本没问题)。
public class ListViewItem {
public ListViewItem(Context context) {
this.M_speedTextView = new TextView(context);
this.M_nameTextView = new TextView(context);
this.M_progressBar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
this.M_activityButton = new Button(context);
this.M_layout = new RelativeLayout(context);
this.M_speedTextView.setId(new Random().nextInt());
this.M_nameTextView.setId(new Random().nextInt());
this.M_progressBar.setId(new Random().nextInt());
this.M_activityButton.setId(new Random().nextInt());
this.M_layout.setId(new Random().nextInt());
}
public void setSpeed(int speed) {
this.M_speedTextView.setText(Integer.toString(speed));
}
public void setName(String name) {
this.M_nameTextView.setText(name);
}
public void setProgress(int progress) {
this.M_progressBar.setProgress(progress);
}
public void setButtonText(String text) {
this.M_activityButton.setText(text);
}
public RelativeLayout getLayout() {
// Layout setting
this.M_layout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, 50));
this.M_layout.setBackgroundResource(android.R.color.transparent);
// Name Text setting
LayoutParams nameTextParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
nameTextParams.setMargins(5, 10, 0, 0);
nameTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
nameTextParams.addRule(RelativeLayout.LEFT_OF,
this.M_speedTextView.getId());
this.M_nameTextView.setTextSize(15);
this.M_nameTextView.setText("TextView");
this.M_nameTextView.setSingleLine();
this.M_nameTextView.setEllipsize(TruncateAt.END);
this.M_layout.addView(this.M_nameTextView, nameTextParams);
// Activity Button setting
LayoutParams activityParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
activityParams.setMargins(0, 5, 5, 0);
activityParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
activityParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
this.M_activityButton.setText("Start");
this.M_layout.addView(this.M_activityButton, activityParams);
// ProgressBar setting
LayoutParams progressParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
progressParams.addRule(RelativeLayout.ALIGN_BOTTOM,
this.M_activityButton.getId());
progressParams.addRule(RelativeLayout.ALIGN_LEFT,
this.M_nameTextView.getId());
progressParams.addRule(RelativeLayout.BELOW,
this.M_nameTextView.getId());
progressParams.addRule(RelativeLayout.LEFT_OF,
this.M_activityButton.getId());
this.M_layout.addView(this.M_progressBar, progressParams);
// Speed TextView setting
LayoutParams speedParam = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
speedParam.addRule(RelativeLayout.ALIGN_BASELINE,
this.M_nameTextView.getId());
speedParam.addRule(RelativeLayout.ALIGN_BOTTOM,
this.M_nameTextView.getId());
speedParam.addRule(RelativeLayout.LEFT_OF,
this.M_activityButton.getId());
this.M_speedTextView.setText("0 KiB/s");
this.M_speedTextView.setTextSize(15);
this.M_layout.addView(this.M_speedTextView, speedParam);
return this.M_layout;
}
TextView M_speedTextView;
TextView M_nameTextView;
ProgressBar M_progressBar;
Button M_activityButton;
RelativeLayout M_layout;
}
现在,我创建了一个数组,ListViewItem
并且我想将这些数组项添加到 ListView。getLayout()
(我们可以使用ListViewItem 类的方法获取包含所有这些 View 的 Layout )。
请注意,我想动态添加这些视图。不使用常用方式填写。谁能指导我如何做到这一点?
编辑常用方法:将xml文件声明为listViewItem,膨胀并使用它的子项目并在getView
方法中返回它。
编辑我当前使用的适配器类和 LogCat 错误:
public class ListViewAdapter extends BaseAdapter {
private ArrayList<ListViewItem> M_items;
public ListViewAdapter(ArrayList<ListViewItem> items) {
this.M_items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return this.M_items.get(position).getLayout();
}
@Override
public int getCount() {
return this.M_items.size();
}
@Override
public ListViewItem getItem(int position) {
return this.M_items.get(position);
}
@Override
public long getItemId(int position) {
return this.M_items.get(position).getId();
}
}
LogCat 错误:
09-03 03:56:23.359: E/AndroidRuntime(9415): FATAL EXCEPTION: main
09-03 03:56:23.359: E/AndroidRuntime(9415): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.measureScrapChild(ListView.java:1189)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.measureHeightOfChildren(ListView.java:1272)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.onMeasure(ListView.java:1181)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.os.Looper.loop(Looper.java:130)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-03 03:56:23.359: E/AndroidRuntime(9415): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 03:56:23.359: E/AndroidRuntime(9415): at java.lang.reflect.Method.invoke(Method.java:507)
09-03 03:56:23.359: E/AndroidRuntime(9415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-03 03:56:23.359: E/AndroidRuntime(9415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-03 03:56:23.359: E/AndroidRuntime(9415): at dalvik.system.NativeStart.main(Native Method)
提前致谢 :)