【问题标题】:Create a list of Custom Views in Android在 Android 中创建自定义视图列表
【发布时间】: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


【解决方案1】:

您的MyView 将被系统实例化,因此您必须手动更改颜色。我添加了setColor(int) 方法。

public class MyView extends View{
  int color;

  public MyView(Context context){
    super(context);
  }

  public void setColor(int color) {
    this.color = color;
    invalidate();
  }


  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(color);

    // DON'T CALL THIS, OR IT WILL LOOP INDEFINITELY
    //this.invalidate();
  }
}



public class MyAdapter extends BaseAdapter {
   public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if(convertView == null) vi = inflater.inflate(gif_view);

    ((MyView)(vi.findViewById(R.id.my_custom_view)).setColor(randomColor);
}

【讨论】:

  • 感谢@simon-marquiz 的帮助......不过有一个问题......我们可以像 setImage 或 setUrl 等一样制作任何仲裁函数......就像你对 setColor 所做的那样??
  • 是的,当然,您可以在类中创建任意数量的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
相关资源
最近更新 更多