如何更好地编写此代码?

时间:2015-03-01 01:37:46

标签: android

我在Android应用的网格视图适配器中有这段代码。基本上它的作用是将网格视图中的第一个图像“拟合”成方形。下一个图像被裁剪。在此部分之前省略了一些代码。

我想我正在不必要地重复一大堆东西。什么是重写此代码的更好方法?

    if (position ==0) {
        //set thumbnail URL
        String url = item.get(position).getHDUrl();

        // Trigger the download of the URL asynchronously into the image view.
        Picasso.with(mContext) //
                .load(url)
                .placeholder(R.drawable.placeholder) //
                .error(R.drawable.error) //
                .tag(view) //
                .fit()
                .into(holder.thumbnail);
        view.setOnClickListener(new OnImageClickListener(position));
        return view;

    } else {
        //set thumbnail URL
        String url = item.get(position).getHDUrl();

        // Trigger the download of the URL asynchronously into the image view.
        Picasso.with(mContext) //
                .load(url)
                .placeholder(R.drawable.placeholder) //
                .error(R.drawable.error) //
                .tag(view) //
                .into(holder.thumbnail);
        view.setOnClickListener(new OnImageClickListener(position));
        return view;

    }

3 个答案:

答案 0 :(得分:2)

我个人会创建一个" PicassoHelper" class,简单地包装Picasso函数。

然后你可以在你的新助手类中创建一个接受bool的函数,指示它是否应该使用' .fit()'或不。

这样你可以将代码缩小为:

//Get thumbnail URL
String url = item.get(position).getHDUrl();

if (position == 0) {
    // Trigger the download of the URL asynchronously into the image view.
    PicassoHelper.downloadImage(url, view, holder.thumbnail, true);
} else {
    // Trigger the download of the URL asynchronously into the image view.
    PicassoHelper.downloadImage(url, view, holder.thumbnail, false);
}

view.setOnClickListener(new OnImageClickListener(position));
return view;

这样,无论何时使用毕加索,你都不必反复重复相同的代码。

答案 1 :(得分:1)

由于.fit()上添加了唯一的position 0方法,您必须在.fit()上添加position 0方法。

如果符合其位置,您必须将公共代码表格分开。

我不知道Picaso对象初始化,你可以这样做,

   //set thumbnail URL
        String url = item.get(position).getHDUrl();
if (position ==0) {
        Picasso.with(mContext) //
                .load(url)
                .placeholder(R.drawable.placeholder) //
                .error(R.drawable.error) //
                .tag(view) //
                .fit()
                .into(holder.thumbnail);
    } else {
        Picasso.with(mContext) //
                .load(url)
                .placeholder(R.drawable.placeholder) //
                .error(R.drawable.error) //
                .tag(view) //
                .into(holder.thumbnail);

    }
        view.setOnClickListener(new OnImageClickListener(position));
        return view;

我怀疑天气.fit()会在您使用网址和其他参数初始化Picaso之后发挥作用

您可以进一步增强,

//set thumbnail URL
     String url = item.get(position).getHDUrl();
     Picasso.with(mContext) //
                    .load(url)
                    .placeholder(R.drawable.placeholder) //
                    .error(R.drawable.error) //
                    .tag(view) //
                    .into(holder.thumbnail);

     if (position ==0) {
            Picasso.fit();
        } 
            view.setOnClickListener(new OnImageClickListener(position));
            return view;

答案 2 :(得分:1)

我会更喜欢这个?

//set thumbnail URL
String url = item.get(position).getHDUrl();

if (position ==0) {

    // Trigger the download of the URL asynchronously into the image view.
    Picasso.with(mContext) //
            .load(url)
            .placeholder(R.drawable.placeholder) //
            .error(R.drawable.error) //
            .tag(view) //
            .fit()
            .into(holder.thumbnail);
} else {

    // Trigger the download of the URL asynchronously into the image view.
    Picasso.with(mContext) //
            .load(url)
            .placeholder(R.drawable.placeholder) //
            .error(R.drawable.error) //
            .tag(view) //
            .into(holder.thumbnail);
}
view.setOnClickListener(new OnImageClickListener(position));
return view;

因为除了代码片段测试之外,我已经有一段时间从事Android工作了,唯一的变化也取决于你是否可以做下面的事情...不知道毕加索是一个对象还是什么不是,但是...你...应该明白我的意思

//set thumbnail URL
String url = item.get(position).getHDUrl();
//This was common in both if else
Picasso.with(mContext).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).tag(view).into(holder.thumbnail);

if (position ==0) {
   // this was only common to the if pos=0
    Picasso.fit();
}
view.setOnClickListener(new OnImageClickListener(position));
return view;
相关问题