在列表视图中实现多个布局Android

时间:2016-02-15 07:46:39

标签: android android-layout listview layout

您好我正在尝试在listview中实现多个布局,其中filetype来自webservices,任何人都可以建议如何实现任务在此先感谢以下是我的代码我实现了下面的适配器但是在视图中给出了< / p>

适配器

package kbs.com.dynamiclistview;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
* Created by user on 2/13/2016.
*/
public class MyCustomAdapter extends BaseAdapter {

    ArrayList<Bean> slist = new ArrayList<Bean>();
    Context context;


    public MyCustomAdapter(ArrayList<Bean> slist, Context context) {
        this.slist = slist;
        this.context = context;
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int i) {
        return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final Bean bean = slist.get(i);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null) {
            if (slist.get(i).getFiletype().equalsIgnoreCase("TEXT")) {

                System.out.println("In if text :--" + bean.getFiletype());


                view = inflater.inflate(R.layout.textlayout, viewGroup, false);

                TextView itemname = (TextView) view.findViewById(R.id.itemname);

                itemname.setText(bean.filetext);

            }

            if (slist.get(i).getFiletype().equalsIgnoreCase("IMAGES")) {

                //   LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.imglayout, viewGroup, false);
                System.out.println("In if image :--" + bean.getFiletype());

                ImageView img = (ImageView) view.findViewById(R.id.img);


                ImageLoader imageLoader = ImageLoader.getInstance();

                imageLoader.init(ImageLoaderConfiguration.createDefault(context));
                DisplayImageOptions option = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(1000)).cacheInMemory(true)
                        .cacheOnDisc(true).resetViewBeforeLoading(true)
                        .showImageForEmptyUri(R.mipmap.ic_launcher)
                        .showImageOnFail(R.drawable.close)
                        .showImageOnLoading(R.drawable.images).bitmapConfig(Bitmap.Config.RGB_565).build();


                imageLoader.displayImage(bean.getFileurl(), img, option);


            }

        }
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在示例下使用getViewTypeCount方法:

@Override
public int getViewTypeCount() {
    return 2;
}

然后我们必须实现逻辑以根据位置值选择要分配给行的布局类型:

@Override public int getItemViewType(int position) 
{
 return (contactList.get(position).getContactType() == ContactType.CONTACT_WITH_IMAGE) ? 0 : 1; 
} 

您可以在getView中检查类型视图

int type = getItemViewType(position);

更多信息此链接sample