滚动时GridView会更改切片

时间:2018-03-24 11:34:51

标签: java android android-layout gridview

我正在尝试在Fever应用中创建一个像这样的视图。

enter image description here

所以基本上,点击图块会更改其背景,再次单击会恢复图像。我在GridView中执行此操作,所有系统都可以正常工作,但是,当我向下滚动视图然后再向上滚动时,布局会重新显示未选中的切片(使用图像,即使我之前已单击它们)。有没有人知道如何解决这个问题?

亲切的问候,

的Grzegorz

这里问的是我的适配器代码:

public class IssueTileAdapter extends BaseAdapter {

    private Context context;
    private List<String> issues;

    public IssueTileAdapter(Context context, List<String> issuesList) {
        this.context = context;
        this.issues = issuesList;
    }

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

    @Override
    public Object getItem(int position) {
        return issues.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View root = layoutInflater.inflate(R.layout.issue_tile, parent, false);
        context = parent.getContext();

        ImageView tvIssueImage = (ImageView) root.findViewById(R.id.iv_issue_image);
        TextView tvIssueTitle = (TextView) root.findViewById(R.id.tv_issue_title);

        tvIssueImage.setImageResource(context.getResources().getIdentifier(issues.get(position).replaceAll("\\s+","").toLowerCase(), "drawable", context.getPackageName()));
        tvIssueTitle.setText(issues.get(position));

        return root;
    }

    public static int getDrawable(Context context, String name)
    {
        Assert.assertNotNull(context);
        Assert.assertNotNull(name);

        return context.getResources().getIdentifier(name,
                "drawable", context.getPackageName());
    }
}

这是onClickMethod。

IssueTileAdapter adapter = new IssueTileAdapter(getActivity().getBaseContext(), issueList);
        issuesGrid.setAdapter(adapter);

        issuesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                issueName=issueList.get(position);
                Log.d("NAME CLICKED",issueList.get(position));
                if(!issues.contains(issueName)) {
                    issues.add(issueName);
                    view.findViewById(R.id.shadow_cast).setVisibility(View.INVISIBLE);
                    view.findViewById(R.id.iv_issue_image).setVisibility(View.INVISIBLE);
                }else{
                    deleteIssue(issueName);
                    view.findViewById(R.id.shadow_cast).setVisibility(View.VISIBLE);
                    view.findViewById(R.id.iv_issue_image).setVisibility(View.VISIBLE);
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

issues传递给IssueTileAdapter并在getView方法

中使用此代码
            issueName=issueList.get(position);
            if(!issues.contains(issueName)) {

                view.findViewById(R.id.shadow_cast).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.iv_issue_image).setVisibility(View.INVISIBLE);
            }else{

                view.findViewById(R.id.shadow_cast).setVisibility(View.VISIBLE);
                view.findViewById(R.id.iv_issue_image).setVisibility(View.VISIBLE);
            }

getView中,您始终以相同的方式进行初始化。你必须检查它是否曾被点击过。如果点击设置了您单击的要求,而不是根据您的不同要求设置。