我创建了一个动态线性布局,我想将这些布局添加到 ListView。代码如下:
动态布局代码:
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DynamicContentLayout extends LinearLayout{
private Context context;
private int linearLayoutMargin;
public DynamicContentLayout(Context context) {
super(context);
this.context = context;
linearLayoutMargin = (int) context.getResources().getDimension(R.dimen.xlarge_margin_padding_fixed);
initViews();
}
private void initViews() {
LinearLayout.LayoutParams layout_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_lp.setMargins(linearLayoutMargin, linearLayoutMargin, linearLayoutMargin, linearLayoutMargin);
this.setLayoutParams(layout_lp);
this.setOrientation(LinearLayout.VERTICAL);
this.addView(headerTitle("My Text"));
}
private TextView headerTitle(String title) {
TextView txtView = new TextView(context);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtView.setPadding(6, 6, 6, 6);
txtView.setText(title);
txtView.setBackgroundColor(context.getResources().getColor(R.color.red));
txtView.setTextColor(context.getResources().getColor(R.color.white));
return txtView;
}
}
列表适配器代码:
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class ListViewAdapter extends ArrayAdapter<DynamicContentLayout> {
private Context context;
private ArrayList<DynamicContentLayout> layoutList = null;
public ListViewAdapter(Context context, int textViewResourceId, ArrayList<DynamicContentLayout> layoutList) {
super(context, textViewResourceId, layoutList);
this.context = context;
this.layoutList = layoutList;
}
@Override
public int getCount() {
return layoutList.size();
}
@Override
public DynamicContentLayout getItem(int position){
return layoutList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DynamicContentLayout entry = (DynamicContentLayout) layoutList.get(position);
convertView = (View) entry;
return convertView;
}
}
最后是我设置适配器的 MainActivity 中的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<DynamicContentLayout> contents = new ArrayList<DynamicContentLayout>();
contents.add(new DynamicContentLayout(getApplicationContext()));
contents.add(new DynamicContentLayout(getApplicationContext()));
contents.add(new DynamicContentLayout(getApplicationContext()));
ArrayAdapter<DynamicContentLayout> arrayAdapter = new ArrayAdapter<DynamicContentLayout>(getApplicationContext(), android.R.layout.simple_list_item_1, contents);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
Xml 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:background="@color/blue"
android:layout_height="fill_parent"
android:alwaysDrawnWithCache="true"
android:clickable="true"
android:layout_alignParentTop="true"
android:divider="@color/grey"
android:drawingCacheQuality="auto"
android:fastScrollEnabled="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
现在,当我运行时,没有报告错误。但我看不到 listItems 布局。我只能看到班级的名称。例如:“com.myexample.dynamicontentoverview.DynamicContentLayout@405193d0”作为 listItem 的行而不是 Layout。
我还想补充一点。我不能使用 XMl 布局,因为我的 DynamicLinearLayout 中的视图可以增加/减少某些行。
请提出一些建议。
谢谢。