我需要帮助才能使用GridView 见代码。
首先,MainActivity.java
public class MainActivity extends AppCompatActivity implements OnViewClickLitener {
@BindView(R.id.grid_view)
GridView gridView;
private GridAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
ButterKnife.bind(this);
adapter = new GridAdapter(this);
adapter.setOnViewClickListener(this);
gridView.setAdapter(adapter);
}
@Override
public void onViewClick(int position) {
onItemClick(position);
}
@Override
public boolean onViewLongClick(int position) {
return onItemLongClick(position);
}
@OnItemClick(R.id.grid_view)
void onItemClick(final int position) {
if (adapter == null) {
return;
}
Log.d(TAG, MainActivity.class.getSimpleName() + ".onItemClick() " + position);
if (adapter.getMode() == Mode.EDIT) {
Log.d(TAG, MainActivity.class.getSimpleName() + ".onItemClick() " + position + ": Edit Mode");
adapter.setCheckedItem(position, !adapter.getCheckedItem(position));
adapter.notifyDataSetChanged();
} else {
Log.d(TAG, MainActivity.class.getSimpleName() + ".onItemClick() " + position + ": Normal Mode");
}
}
@OnItemLongClick(R.id.grid_view)
boolean onItemLongClick(final int position) {
if (adapter == null) {
return true;
}
Log.d(TAG, MainActivity.class.getSimpleName() + ".onItemLongClick() " + position);
if (adapter.getMode() == Mode.NORMAL) {
Log.d(TAG, MainActivity.class.getSimpleName() + ".onItemLongClick() " + position + ": Edit Mode");
setTitle("Edit");
adapter.changeMode();
adapter.setCheckedItem(position, !adapter.getCheckedItem(position));
adapter.notifyDataSetChanged();
}
return true;
}
@Override
public void onBackPressed() {
if (adapter != null && adapter.getMode() == Mode.EDIT) {
setTitle(getString(R.string.app_name));
adapter.changeMode();
adapter.initCheckedItems();
adapter.notifyDataSetChanged();
} else {
super.onBackPressed();
}
}
}
其次,GridAdapter.java
public class GridAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private Mode mode = Mode.NORMAL;
private List<String> items = new ArrayList<>();
private List<Boolean> checkedItems = new ArrayList<>();
private OnViewClickListener listener;
GridAdapter(Context context) {
inflater = LayoutInflater.from(context);
for (int i = 0; i < 10; i++) {
items.add(String.valueOf(i + 1));
checkedItems.add(Boolean.FALSE);
}
}
public void setOnViewClickListener(OnViewClickListener listener) {
this.listener = listener;
}
public Mode getMode() {
return mode;
}
public void setCheckedItem(final int position, boolean checked) {
checkedItems.set(position, checked);
}
public boolean getCheckedItem(final int position) {
return checkedItems.get(position);
}
public void changeMode() {
if (mode == null) {
mode = Mode.NORMAL;
} else if (mode == Mode.NORMAL) {
mode = Mode.EDIT;
} else {
mode = Mode.NORMAL;
}
}
public void initCheckedItems() {
for (int i = 0; i < checkedItems.size(); i++) {
checkedItems.set(i, Boolean.FALSE);
}
}
@Override
public int getCount() {
return (items == null) ? 0 : items.size();
}
@Override
public String getItem(final int position) {
return (items == null) ? null : items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView != null && convertView.getTag() instanceof ViewHolder) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.grid_item, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
final String item = getItem(position);
if (item == null) {
initViews(holder);
} else {
drawLayout(holder, position);
drawText(holder, item);
drawDimmed(holder, position);
}
return convertView;
}
private void initViews(final ViewHolder holder) {
if (holder == null) {
return;
}
holder.itemLayout.setChecked(false);
holder.text.setText(null);
holder.dimmedLayout.setVisibility(View.GONE);
}
private void drawLayout(final ViewHolder holder, final int position) {
if (holder == null) {
return;
}
boolean checked = checkedItems.get(position);
holder.itemLayout.setChecked(checked);
}
private void drawText(final ViewHolder holder, final String item) {
if (holder == null) {
return;
}
holder.text.setText(item);
}
private void drawDimmed(final ViewHolder holder, final int position) {
if (holder == null) {
return;
}
holder.dimmedLayout.setVisibility(View.VISIBLE);
holder.dimmedLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mode == Mode.EDIT) {
if (listener != null) {
Log.d(TAG, GridAdapter.class.getSimpleName() + ".onClick() " + position);
listener.onViewClick(position);
}
}
}
});
holder.dimmedLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (listener != null) {
Log.d(TAG, GridAdapter.class.getSimpleName() + ".onLongClick() " + position);
return listener.onViewLongClick(position);
}
return true;
}
});
}
/**
* ViewHolder
*/
static final class ViewHolder {
@BindView(R.id.grid_item_layout)
CheckableRelativeLayout itemLayout;
@BindView(R.id.grid_item_text)
TextView text;
@BindView(R.id.dimmed_layout)
ViewGroup dimmedLayout;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
第三,OnViewClickLitener.java
public interface OnViewClickLitener {
void onViewClick(int position);
boolean onViewLongClick(int position);
}
您构建此代码并长按网格项
看到奇怪的动作和日志?
我案例的日志是
我在这个问题上找不到原因 请帮忙。 感谢。