如何动态控制gridview特殊项的视图

时间:2017-03-03 16:40:43

标签: android gridview customization

我想在gridview项目中控制(删除(删除),添加(make visible),设置数据)视图,例如当用户点击gridview项目中的下载按钮时,网格项的progressBar运行完成下载后的进度;下载按钮消失,打开按钮变得可见,可能还有更多操作,例如更改删除按钮的背景,在点击下载按钮后匹配我的代码我的订单正常运行但是当用户滚动gridview时,某些项目获得类似的效果,我命令最后一个网格项目[可见打开按钮等],我在网格中看到一些相同的项目,任何身体都可以帮我解决这个问题吗?或者提议使用recyclerview?

这是我的适配器类

tooltip

这是我的活动

public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;

public CustomGrid(Context c,String[] web,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
}

@Override
public int getCount() {
        return web.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                grid = inflater.inflate(R.layout.grid_single,parent,false);
        } else {
                grid = convertView;
        }
        final ProgressBar downloadPb =(ProgressBar)grid.findViewById(R.id.single_gride_progressBar);
        final Button downloadBtn = (Button) grid.findViewById(R.id.grid_single_download_btn);
        final Button openBtn = (Button) grid.findViewById(R.id.grid_single_open_btn);
        final Button deleteBtn = (Button) grid.findViewById(R.id.grid_single_delete_btn);

        downloadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        new CountDownTimer(5000,1){ //simulating download progress
                                @Override
                                public void onTick(long millisUntilFinished) {
                                        if(millisUntilFinished<=500){
                                                downloadPb.setProgress(100);
                                        }else if(millisUntilFinished<=1000){
                                                downloadPb.setProgress(80);
                                        }else if(millisUntilFinished<=2000){
                                                downloadPb.setProgress(60);
                                        }else if(millisUntilFinished<=3000){
                                                downloadPb.setProgress(40);
                                        }else if(millisUntilFinished<=4000){
                                                downloadPb.setProgress(20);
                                        }
                                }

                                @Override
                                public void onFinish(){
                                        downloadBtn.setVisibility(View.GONE);
                                        openBtn.setVisibility(View.VISIBLE);
                                        deleteBtn.setBackgroundResource(R.drawable.delete_selector);
                                }
                        }.start();
                }
        });
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
        textView.setText(web[position]);
        imageView.setImageResource(Imageid[position]);

        return grid;
}
}

0 个答案:

没有答案