本帖最后由 liu 于 2018-12-21 10:53 编辑
先看下要实现的效果:
思路就是写一个数据用来存放每个item的状态(例如初始状态设置为0,点击后设置为1),点击时通过item的状态来判断是否该选中还是取消
示例代码:
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:numColumns="3" />
</LinearLayout>
item布局
item_gridview.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@android:color/black"/>
</LinearLayout>
MainActivity
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
GridView gridView = (GridView) findViewById(R.id.gv);
MyAdapter adapter = new MyAdapter(this, list);
gridView.setAdapter(adapter);
}
}
MyAdapter
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2018/12/17.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Integer> list;
//这个数组用来存放item的点击状态
private int[] statue;
public MyAdapter(Context context, List<Integer> list) {
this.context = context;
this.list = list;
statue = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
statue[i] = 0; //初始化item点击状态的数组 0:未选中 1:选中
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.item_gridview, null);
holder.tv = view.findViewById(R.id.tv);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tv.setText(String.valueOf(list.get(i)));
holder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (statue[i] == 0) {
holder.tv.setBackgroundColor(Color.BLUE);
holder.tv.setTextColor(Color.WHITE);
//设置为选中状态
statue[i] = 1;
} else {
holder.tv.setBackgroundColor(Color.WHITE);
holder.tv.setTextColor(Color.BLACK);
//设置为未选中状态
statue[i] = 0;
}
}
});
return view;
}
class ViewHolder {
private TextView tv;
}
}
|