关闭

ListView自定义布局以及相关操作

101人阅读 评论(0) 收藏 举报
分类:

1.实现自定义布局的ListView

(1) 继承BaseAdapter,重写适配器类

public class ListViewAdapter extends BaseAdapter{
    private List<Note> data;
    private LayoutInflater layoutInflater;
    private Context context;
    public ListViewAdapter(Context context,List<Note> data){
        this.context=context;
        this.data=data;
        this.layoutInflater=LayoutInflater.from(context);
    }
    /**
     * 组件集合,对应list.xml中的控件
     * @author Administrator
     */
    public final class NoteItem{
        public TextView tvTitle;
        public TextView tvDate;
        public TextView tvContent;
}
    @Override
    public int getCount() {
        return data.size();
    }
    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NoteItem noteItem=null;
        if(convertView==null){
            noteItem=new NoteItem();
            //获得组件,实例化组件
            convertView=layoutInflater.inflate(R.layout.note_item, null);
            noteItem.tvTitle=(TextView)convertView.findViewById(R.id.tv_title);
            noteItem.tvDate=(TextView)convertView.findViewById(R.id.tv_date);
            noteItem.tvContent=(TextView)convertView.findViewById(R.id.tv_content);
            convertView.setTag(noteItem);
        }else{
            noteItem=(NoteItem) convertView.getTag();
        }
        //绑定数据
        noteItem.tvTitle.setText(data.get(position).getTitle().toString());
        noteItem.tvDate.setText(data.get(position).getDate().toString());
        noteItem.tvContent.setText(data.get(position).getContent().toString());
        return convertView;
    }
}

(2) ListView自定义的item布局 note_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F1F5F8"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:singleLine="true"
        android:maxEms="8"
        android:ellipsize="end"/>
    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2016"
        android:singleLine="true"
        />
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="具体内容"
        android:singleLine="true"
        android:maxEms="8"
        android:ellipsize="end"/>



</LinearLayout>

(3) ListView所对应的实体类 实现了Serializable,以便对象传输

public class Note implements Serializable{

    private String title;
    private String content;
    private String date;



    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }



    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getDate()
    {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

(4) 收集数据 这里的数据是从数据库中取的

private List<Note> noteList=new ArrayList<>();

 Cursor c = db.query("notes",null,null,null,null,null,null);//查询并获得游标
        while(c.moveToNext()){//判断游标是否为空
                String date = c.getString(c.getColumnIndex("date"));
                String title = c.getString(c.getColumnIndex("name"));
                String content=c.getString(c.getColumnIndex("content"));
                note=new Note();
                note.setTitle(title);
                note.setDate(date);
                note.setContent(content);
                noteList.add(note);

        }

(5) 为ListView添加适配器

 listViewAdapter=new ListViewAdapter(getActivity(),noteList);
 lvNotes.setAdapter(listViewAdapter);

2.为ListView的item设置点击事件和长按事件

(1) 点击事件

//Item点击事件 Intent跳转实现同时实现对象传输
        lvNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Note note=noteList.get(position);
                Intent intent1=new Intent(getActivity(), FactNoteActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("note", note);
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });

(2) 长按事件,实现对应的item的删除。

//Item长按事件 这里用到了AlertDialog的自定义布局实现
        lvNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                final int index=position;
                final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
                dialog.show();
                dialog.getWindow().setContentView(R.layout.alert_dialog);
                dialog.getWindow().findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_LONG).show();
                        String title = noteList.get(index).getTitle();
                        String date=noteList.get(index).getDate();

                        //删掉长按的item
                        noteList.remove(index);
                        //动态更新listview 但是此时并未 从数据库删除数据
                        listViewAdapter.notifyDataSetChanged();


                        String whereClause = "name=? and date=?";//删除的条件
                        String whereArgs[] ={title,date};//删除的条件参数
                        db.delete("notes",whereClause,whereArgs);//执行删除
                    }
                });
                dialog.getWindow().findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                //返回true能屏蔽点击事件的干扰
                return true; 
            }
        });

3.如果将ListView嵌套在ScrollView中使用的时候,需要重写ListView,以解决两者的滑动冲突问题。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;


public class NoScrollListView extends ListView{
    public NoScrollListView(Context context) {
        super(context);
    }

    public NoScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);//这是让ListView 不滚动 确定高度 让scrollView滚动
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11229次
    • 积分:1023
    • 等级:
    • 排名:千里之外
    • 原创:71篇
    • 转载:1篇
    • 译文:16篇
    • 评论:6条
    最新评论