实现Listview两种布局的加载

实现ListView多种布局的加载,这里加载两种,两种布局如下:


1 item_left,文字在左,图片在右

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">


    <TextView
        android:id="@+id/text_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="left"
        android:padding="5dp"
        />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@mipmap/ic_launcher"
        />
RelativeLayout>

2 item_right,图片在左,文字在右
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"
        />

    <TextView
        android:id="@+id/text_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="right"
        android:padding="5dp"
        />
RelativeLayout>

activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    ListView>
RelativeLayout>
适配器ListAdapter
public class ListAdapter extends BaseAdapter {

    private List contents;
    private Context context;

    public ListAdapter(List contents, Context context) {
        this.contents = contents;
        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return contents.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        LayoutInflater inflater = LayoutInflater.from(context);
        int type = getItemViewType(position);
        if (view == null) {
            //位置为偶数的就让内容显示在右边,奇数就显示在左边
            switch (type) {
                case 0:
                    view = inflater.inflate(R.layout.item_right, parent, false);
                    break;
                case 1:
                    view = inflater.inflate(R.layout.item_left, parent, false);
                    break;
            }
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.imageView = (ImageView) view.findViewById(R.id.image);
            holder.textView = (TextView) view.findViewById(R.id.text_content);
        }

        holder.textView.setText(contents.get(position));

        return view;
    }

    //返回布局的种类个数
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    //返回布局的类型
    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }

    static class ViewHolder {
        TextView textView;
        ImageView imageView;
    }
}

MainActivity代码

public class MainActivity extends Activity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) this.findViewById(R.id.list_content);
        List contents = new ArrayList<>();
        for (int i = 1; i <= 50; i++) {
            contents.add("这是第" + i + "条数据");
        }

        ListAdapter adapter = new ListAdapter(contents, this);
        listView.setAdapter(adapter);
    }
}

你可能感兴趣的:(ListView多布局加载)