ListView的复用问题,点击条目变色.不复用

主要的是3个类,包含activity,application,和一个adpter,内部的细节在代码中都有详细的注释.可以参考

ListView的复用问题,点击条目变色.不复用_第1张图片

application:

public class MyApplication extends Application {
    //用于保存oldview防止每次退出该页面的时候,再次进入出现空指针异常
    public static View oldView;

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

activity:

public class Main3Activity extends AppCompatActivity {

    private List list;
    private LinearLayout ll1;
    private List integers;
    private ListView listView1;
    private ListView listView2;
   private SharedPreferences  sp;
    //默认的时候为-1;如果不默认值得话会出现异常
    private int position = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        list = new ArrayList<>();
        //模拟数据这里大概模拟了60个
        for (int i = 0; i < 60; i++) {
            list.add("nihao" + i);
        }
        //第一个listview
        listView1 = (ListView) findViewById(R.id.lv_main3_1);
        //第二个
        listView2 = (ListView) findViewById(R.id.lv_main3_2);
        //通过sp获取到存储的数据
         sp = getSharedPreferences("Item",MODE_PRIVATE);
        //默认返回的数据为-1.如果你想每次都锁定第一个.可以改变postion的值为1
        position = sp.getInt("position", -1);
        //设置适配器
        listView1.setAdapter(new MyListAdapter(list,this));
         listView2.setAdapter(new MyListAdapter(list,this));
        //添加条目点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                //记录老的位置
                LinearLayout linearLayout  = (LinearLayout) view.findViewById(R.id.ll_main3_item);
                linearLayout.setBackgroundColor(Color.WHITE);
                //如果当前位置没有点击的话就记录当前的view,并将当前的位置保存到sp中
                if(position != -1  && position != i  ){
                    //表示当前点击的位置没有记录,将颜色给清空
                    MyApplication.oldView.setBackgroundResource(0);
                }
                //保存当前的view.为了以后删除该view的颜色使用
                MyApplication.oldView = view ;

                //将当前的点击位置保存,同时position需要记录当前的位置
                position =  i;
                SharedPreferences.Editor  editor = sp.edit();
                editor.putInt("position",i);
                editor .commit();
            }
        });
    }
}

adapter:

public class MyListAdapter extends BaseAdapter {
    private List list ;
    private Activity activity ;
   private SharedPreferences sp;
    public MyListAdapter(List list, Activity activity) {
        this.list = list;
        this.activity = activity;
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
           view  = View.inflate(activity,R.layout.item_main3,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        //获取到sp中的数值
        sp = activity.getSharedPreferences("Item", activity.MODE_PRIVATE);
        int position = sp.getInt("position", -1);
        //判断当前的位置是不是点击的位置
        System.out.println("position" + position);
        //判断当前的位置是否是当前的位置.如果是当前位置就改变颜色,否则不变
        if(position == i){
            holder.linearLayout.setBackgroundColor(Color.WHITE);
        }else{
            holder.linearLayout.setBackgroundResource(0);
        }
        holder.textView.setText(list.get(i));
        return view;
    }
//定义一个holder
    class ViewHolder {
        TextView textView;
        LinearLayout linearLayout;
        public ViewHolder(View view ) {
            this.linearLayout = (LinearLayout) view.findViewById(R.id.ll_main3_item);
            this.textView = (TextView) view.findViewById(R.id.tv_main3);
        }
    }
}

activity.xml





    
       
    
    
        


item_main.xml



    
        android:padding="20dp"
        android:layout_gravity="center"
        android:id="@+id/tv_main3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

//如果想要实现每次启动都是第一个被选中,那么需要下面的操作

    //默认的时候为0;如果不默认值得话会出现异常
    private int position = 0;

//改变点击事件,(略微的修改)

      //添加条目点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                //记录老的位置
                LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_main3_item);
                linearLayout.setBackgroundColor(Color.WHITE);
                //如果当前位置没有点击的话就记录当前的view,并将当前的位置保存到sp中
                if (position != 0 && position != i) {
                    //表示当前点击的位置没有记录,将颜色给清空
                    MyApplication.oldView.setBackgroundResource(0);
                }
                //保存当前的view.为了以后删除该view的颜色使用
                MyApplication.oldView = view;

                if (position == 0) {
                    myListAdapter.notifyDataSetChanged();

                }
                //将当前的点击位置保存,同时position需要记录当前的位置
                position = i;
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("position", i);
                editor.commit();
            }
        });



你可能感兴趣的:(listview)