Android 单选之史上最简单的ListView实现单选效果

Android 单选之史上最简单的ListView实现单选效果,通过设置背景颜色来实现单选效果

一.效果图:

Android 单选之史上最简单的ListView实现单选效果_第1张图片

 

二.简单实现:

1.主函数代码:

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.m1571.myapplication.DataSupport;
import com.example.m1571.myapplication.ListViewActivity;
import com.example.m1571.myapplication.R;
import com.example.m1571.myapplication.badgeview.Badge;
import com.example.m1571.myapplication.badgeview.QBadgeView;
import com.example.m1571.myapplication.bean.RadioBean;
import com.example.m1571.myapplication.view.activity.animation.AnimationActivity;
import com.example.m1571.myapplication.view.activity.dialog.AlertDialogActivity;

import java.util.ArrayList;
import java.util.List;

public class ListViewRadioActivity extends AnimationActivity{
    ListView listview;
    private List data = new ArrayList<>();
    private ListAdapter listAdapter;
    private AlertDialog.Builder builder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_radio);
        listview = (ListView) findViewById(R.id.listview);
//        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//单选模式
        data.clear();
        for (int i = 0; i < 100; i++) {
            data.add(new RadioBean(i+"",""+i,false));
        }
        listAdapter = new ListAdapter();
        listview.setAdapter(listAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, final int position, long id) {
                builder = new AlertDialog.Builder(ListViewRadioActivity.this).setIcon(R.drawable.g).setTitle("系统提示")
                        .setMessage("确定要删除吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //ToDo: 你想做的事情
                                data.remove(position);
                                listAdapter.notifyDataSetChanged();
                                Toast.makeText(ListViewRadioActivity.this, "已删除"+position, Toast.LENGTH_LONG).show();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //ToDo: 你想做的事情
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
//                data.remove(position);
//                listview.getAdapter().getItem(position);
//                listview.getAdapter().getView(position,view,listview);
//                listAdapter.notifyDataSetChanged();
            }
        });


    }
    class ListAdapter extends BaseAdapter {

        public ListAdapter() {
//            data = new DataSupport().getData();
        }

        @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(final int position, View convertView, ViewGroup parent) {
            final ListAdapter.Holder holder;
            if (convertView == null) {
                holder = new ListAdapter.Holder();
                convertView = LayoutInflater.from(ListViewRadioActivity.this).inflate(R.layout.item_view, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.tv_content);
                holder.root = (RelativeLayout) convertView.findViewById(R.id.root);
                holder.badge = new QBadgeView(ListViewRadioActivity.this).bindTarget(convertView.findViewById(R.id.imageview));
                holder.badge.setBadgeTextSize(12, true);
                convertView.setTag(holder);
            } else {
                holder = (ListAdapter.Holder) convertView.getTag();
            }
            holder.textView.setText(data.get(position).getGoodsName());
            holder.badge.setBadgeNumber(position);
            holder.badge.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                @Override
                public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                    if (dragState == STATE_SUCCEED) {
                        Toast.makeText(ListViewRadioActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
                    }
                }
            });
//            holder.root.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
                    if(data.get(position).getIsUser()){
                        data.get(position).setIsUser(false);
                        holder.root.setBackgroundColor(Color.BLUE);
                    }else {
                        data.get(position).setIsUser(true);
                        holder.root.setBackgroundColor(Color.RED);
                    }
                    for (int i = 0; i < data.size(); i++) {
                        if(data.get(position).getIsUser()){
                            data.get(position).setIsUser(true);
                        }else {
                            data.get(position).setIsUser(false);
                        }
                    }
//                }
//            });
            return convertView;
        }

        class Holder {
            TextView textView;
            Badge badge;
            RelativeLayout root;
        }
    }
}

2.主函数布局:




    

3.适配器布局:




    

        

        

        
    

5.shape背景:

item_single_choice_selector.xml:


    
    
    
    
    
    
    

shape_line_blue_new.xml: 




    
    
    
    
    
    
    
    
    
    
    
    

shape_line.xml:



    
        
            
        
    
    
        
            
        
    

shape_line_gray.xml:



    
        
            
        
    
    
        
            
        
    

item_text_choice_selector.xml:



    
    
    
    

color.xml:



    #3F51B5
    #303F9F
    #FF4081
    #89000000
    #fff
    #333
    #ffd2d2d2

    #33691E
    #9C27B0
    #A020F0
    #FF4081
    #FF9800
    #00BCD4
    #F57F17


    #454545
    #707070

    #00ff00

    #FF3baa24
    #278918
    #98cc8e
    #4e9ce6
    #3479d4
    #9fc4ee
    #eeeeee
    #e8e8e8
    #222222
    #FFFFFF
    #6633B5E5
    #ebebeb
    #f7f7f7
    #ffffffd3

 

 

 

 

 

你可能感兴趣的:(RecyclerView列表类)