Inisializing自定义列表视图

时间:2015-07-17 13:29:05

标签: android android-listview

我通过XML初始化了Listview:

 <com.example.app.ListViewManage
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_marginTop="45dp"
        />

我无法管理在类中初始化列表有没有办法呢?

类代码:即时尝试使用init()函数进行设置,如果将此类传递给XML,如何从另一个类传递值?

public class ListViewManage extends ListView{

    int HEADER_HEIGHT = 45;

    int HeaderID = 0;

    Context context = null;
    int HeightInPixels = 0;
    int WidthInPixels = 0;
    float alphaScale = 0;
    float PrevAlpha = 0;

    public ListViewManage(Context context) {
        super(context);
        init(context);

    }
    public ListViewManage(Context context,AttributeSet attrs){
        super(context, attrs);
    }
    public ListViewManage(Context context,AttributeSet attrs,int defStyle){
        super(context, attrs,defStyle);

    }

    public void init(Context context){
        this.context = context;
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        this.HeightInPixels = dm.heightPixels;
        this.WidthInPixels = dm.widthPixels;
        this.alphaScale = this.HEADER_HEIGHT/this.HeightInPixels;

        FrameLayout.LayoutParams fl=(FrameLayout.LayoutParams)getLayoutParams();
        fl.topMargin=HEADER_HEIGHT;
        setLayoutParams(fl);
        ArrayAdapter<YTFileDownload> ListAdapter = new ArrayAdapter<YTFileDownload>(context,R.layout.download_list_view,R.id.Title,YTDownloadsManage.Downloads){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ProgressBar bar =(ProgressBar)v.findViewById(R.id.Process);
                bar.setProgress(0);
                TextView tv = (TextView)v.findViewById(R.id.Title);
                tv.setText(YTDownloadsManage.Downloads.get(position).getVideoID());
                TextView tv2 = (TextView)v.findViewById(R.id.Speed);
                tv2.setText("0MB");
                return v;
            }
        };
        LayoutInflater li=LayoutInflater.from(context);
        this.setAdapter(ListAdapter);
    }





}

3 个答案:

答案 0 :(得分:0)

ListViewManage mlv = (ListViewManage) findViewById(R.id.list);

mlv.setAdapter(adapter);

答案 1 :(得分:0)

adapter.notifyDataSetChanged();

答案 2 :(得分:0)

在你的getView()方法中,我认为你应该错过:

     LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.custom_listview_item, null);