ListAdapter列表适配器

ListAdapter列表适配器其实是继承了BaseAdapter

示例图:

MainActivity
package com.shaoxin.mylistadapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.shaoxin.mylistadapter.R.drawable.b;
import static com.shaoxin.mylistadapter.R.drawable.c;

public class MainActivity extends ListActivity {
    private List<Map> list;
    private String[] strPerson = {"妹子", "美女", "美美", "大美女"};
    private int[] iDrawble = {R.drawable.a, b, c, R.drawable.d};
    MyAdapter myAdapter;
    private float fFirstx;
    private float fSecx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.txt);
                Log.v("msg", textView.getText().toString());
            }
        });
        list = new ArrayList<>();
        for (int i = 0; i < strPerson.length; i++) {
            Map map = new HashMap();
            map.put("person", strPerson[i]);
            map.put("id", iDrawble[i]);
            list.add(map);
        }
        myAdapter = new MyAdapter();
        setListAdapter(myAdapter);
    }

    public class MyAdapter extends BaseAdapter {

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHold viewHold = new ViewHold();
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.listview_item, null);
                viewHold.strName = (TextView) convertView.findViewById(R.id.txt);
                viewHold.iDrawable = (ImageView) convertView.findViewById(R.id.img);
                viewHold.button = (Button) convertView.findViewById(R.id.btn_del);
                convertView.setTag(viewHold);
            } else {
                viewHold = (ViewHold) convertView.getTag();
            }
            viewHold.iDrawable.setImageResource((Integer) list.get(position).get("id"));
            viewHold.strName.setText(list.get(position).get("person").toString());
            viewHold.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    myAdapter.notifyDataSetChanged();
                }
            });
            convertView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_UP:
                            fSecx = event.getX();
                            if ((fSecx - fFirstx) > 10) {
                                ViewHold viewHold = (ViewHold) v.getTag();
                                viewHold.button.setVisibility(Button.VISIBLE);
                                viewHold.button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        list.remove(position);
                                        myAdapter.notifyDataSetChanged();
                                    }
                                });
                            }
                            break;
                        case MotionEvent.ACTION_DOWN:
                            fFirstx = event.getX();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            break;
                    }
                    return true;
                }
            });
            return convertView;
        }

        class ViewHold {
            private TextView strName;
            private ImageView iDrawable;
            private Button button;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.shaoxin.mylistadapter.MainActivity">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

listview_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">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="100dp"
        android:maxWidth="100dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/img"
        android:layout_toEndOf="@+id/img"
        android:layout_toRightOf="@+id/img"
        android:text="hello world" />

    <Button
        android:id="@+id/btn_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        android:layout_alignBaseline="@+id/txt"
        android:visibility="gone"
        android:layout_alignBottom="@+id/txt"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

 

posted @ 2016-12-13 21:08  情似雨餘黏地絮  阅读(4417)  评论(0编辑  收藏  举报