【发布时间】:2014-10-15 12:51:32
【问题描述】:
我想创建一个自定义列表,其中每个项目都包含一个自定义视图和一些其他内容,例如文本视图...
我应该在适配器中做什么,以便我可以在列表中显示不同颜色的自定义视图???
下面是我目前的代码
Main.java
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//some data declaration
ListView lv = (ListView)findViewById(R.id.myList);
lv.setAdapter(new MyAdapter(this, data, R.layout.item));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/myList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MyAdapter.java
public class MyAdapter extends BaseAdapter {
//private variables
//some functions
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null) vi = inflater.inflate(gif_view);
View customView = (View)findViewById(R.id.my_custom_view);
/* what to do here ?? */
/* */
}
item.xml
<?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="match_parent">
<com.android.me.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.android.gif.GIFView"
android:id="@+id/my_custom_view"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
/*
some other stuff
*/
</RelativeLayout>
MyView.java
public class MyView extends View{
int color;
public MyView(Context context, int color){
super(context);
this.color = color;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(color);
super.onDraw(canvas);
this.invalidate();
}
}
任何帮助将不胜感激。 谢谢:)
【问题讨论】:
-
也许我错过了这个问题......
-
您有问题吗?
-
opps ,我忘了突出显示顶部的问题.. 它写在 MyAdapter.java /* 在这里做什么? */
标签: android android-listview custom-controls android-custom-view custom-component