ListView 条目互斥和自定义dialog

显示效果:

ListView 条目互斥和自定义dialog_第1张图片ListView 条目互斥和自定义dialog_第2张图片

代码实现:

package com.example.tuhuadmin.testlistview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //准备要传入的数据
        ArrayList mData = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mData.add("crs" + i);
        }

        ListView lv = (ListView) findViewById(R.id.lv);

        final MyAdapter myAdapter = new MyAdapter(this, mData);
        myAdapter.selectedItemPosition(0);
        lv.setAdapter(myAdapter);
        
        //条目监听事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                myAdapter.selectedItemPosition(i);
                //相当于调用getView()重新绘制条目
                myAdapter.notifyDataSetChanged();
                ShowDialog(MainActivity.this, "已经加入购物车");
            }
        });
    }


    public void ShowDialog(Context context, String text) {

        //控制Dialog显示的大小
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wm.getDefaultDisplay();
        int width = defaultDisplay.getWidth();

        final BaseDialog baseDialog = new BaseDialog(context, R.layout.show_dialog);
        //获取对话框所在的窗口
        Window dialogWindow = baseDialog.getWindow();
        //获取对话框窗口的布局参数
        WindowManager.LayoutParams p = dialogWindow.getAttributes();

        p.height = (int) (width * 0.5);
        p.width = (int) (width * 0.5);
        //当Window的Attributes改变时系统会调用此函数 setAttributes()
        dialogWindow.setAttributes(p);

        //为对话框设置文字
        ((TextView) baseDialog.getView().findViewById(R.id.show_text)).setText(text);
        //触摸外面不能够取消
        baseDialog.setCanceledOnTouchOutside(false);
        baseDialog.show();

        CountDownTimer timer = new CountDownTimer(2000, 2000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }
            @Override
            public void onFinish() {
                //定时器类 2秒后自动消失
                baseDialog.dismiss();
            }
        };

        timer.cancel();
        //定时器开始执行
        timer.start();
    }
}

适配器:

package com.example.tuhuadmin.testlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created on 2016/7/29.
 * Author:crs
 * Description:XXX
 */
public class MyAdapter extends BaseAdapter {

    private int selectedPosition;
    private Context mContext;
    private ArrayList mData;
    private final LayoutInflater mInflate;


    public MyAdapter(Context context, ArrayList mData) {
        this.mContext = context;
        this.mData = mData;
        mInflate = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflate.inflate(R.layout.item, null);
            holder = new ViewHolder();
            holder.tv = (TextView) view.findViewById(R.id.tv_name);
            holder.bg = (LinearLayout) view.findViewById(R.id.bg);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        //赋值
        holder.tv.setText(mData.get(i));

        //被选中和未被选中的显示样式
        if (selectedPosition == i) {
            holder.bg.setBackgroundColor(mContext.getResources().getColor(R.color.car_item_list_bg_selected));
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.car_item_list_item_text_selected));
        } else {
            holder.bg.setBackgroundColor(mContext.getResources().getColor(R.color.car_item_list_bg_normal));
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.fontColorce));
        }
        return view;
    }

    public void selectedItemPosition(int position) {
        this.selectedPosition = position;
    }

    static class ViewHolder {
        TextView tv;
        LinearLayout bg;
    }
}
自定义Dilaog:

package com.example.tuhuadmin.testlistview;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created on 2016/7/29.
 * Author:crs
 * Description:XXX
 */
public class BaseDialog extends Dialog {

    private View view;

    //构造函数
    public BaseDialog(Context context, int layout) {
        super(context, R.style.Dialog);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(layout, null);

        this.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        this.setContentView(view);
    }

    public View getView() {
        return view;
    }
    public void setView(View view) {
        this.view = view;
    }

    public void closewindow() {
        this.dismiss();
    }
}
布局文件:




    

        

            

            
        
    
样式:

    




你可能感兴趣的:(ListView 条目互斥和自定义dialog)