在viewpager底部叠加listview并关注当前项目

时间:2017-04-24 08:46:45

标签: android listview android-viewpager

大家好我正在开发一个图库应用。 我已经实现了Gridview并从服务器显示图像。当点击该图像时,它将打开一个对话框,对话框底部包含viewpager和listview。

根据viewpager位置图像,它也会平滑幻灯片列表视图。

问题:我想在listview上显示叠加层,只显示当前焦点项目,其他项目看起来模糊不清。

这是我的代码

 imagelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
            imagdialog = new Dialog(MainActivity.this);
            imagdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            imagdialog.setContentView(R.layout.imagelist);
            imagdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(final DialogInterface arg0) {
                    // recreate();
                    selectedAdapter = new Sadapter(getApplicationContext(), arraylist);
                    imagelist.setAdapter(selectedAdapter);
                }
            });


            photoid = arraylist.get(i).getId();
            Log.v("Photoid111", "" + photoid);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(imagdialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;
            imagdialog.getWindow().setAttributes(lp);
            viewpager = (ViewPager) imagdialog.findViewById(R.id.img);
            final Button accept = (Button) imagdialog.findViewById(R.id.accept);
            final Button reject = (Button) imagdialog.findViewById(R.id.reject);
            final EditText cmnts = (EditText) imagdialog.findViewById(R.id.comnt);
            ImageView closewin = (ImageView) imagdialog.findViewById(R.id.search_close_btn);
            imglist = (RecyclerView) imagdialog.findViewById(R.id.imglist);
            LinearLayoutManager sublimationmanager
                    = new LinearLayoutManager(imagdialog.getContext(), LinearLayoutManager.HORIZONTAL, false);
            imglist.setLayoutManager(sublimationmanager);
            cmnts.setText("" + arraylist.get(i).getCmnts());
            cmnts.setSelection(cmnts.getText().length());


            adaptor = new ViewPager_Adaptor(MainActivity.this, arraylist);
            viewpager.setAdapter(adaptor);
            viewpager.setCurrentItem(i);
            img_adaptor = new Image_list(getApplicationContext(), arraylist);
            imglist.setAdapter(img_adaptor);
            imglist.smoothScrollToPosition(i);
            pos = i;

            viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    photoid = arraylist.get(position).getId();
                    Log.v("Photoid", "" + photoid);
                    cmnts.setText("" + arraylist.get(position).getCmnts());
                    cmnts.setSelection(cmnts.getText().length());
                    imglist.smoothScrollToPosition(position);
                    pos = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });

            closewin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    imagdialog.dismiss();
                    selectedAdapter = new Sadapter(getApplicationContext(), arraylist);
                    imagelist.setAdapter(selectedAdapter);
                }
            });
            accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    comments = cmnts.getText().toString();
                    Log.v("Photoid", "" + photoid);
                    Log.v("Photoid", "" + comments);

                    isInternetPresent = cd.isConnectingToInternet();
                    if (isInternetPresent) {
                        mdialog = new Dialog(MainActivity.this);

                        mdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                        mdialog.setContentView(R.layout.custom_progress_dialog);
                        mdialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                        mdialog.setCancelable(false);

                        mdialog.show();
                        new Accept().execute(URL.amateurpath + "Accept");

                    } else {
                        if (imagdialog.isShowing())
                            imagdialog.dismiss();
                        nonetwork.setVisibility(View.VISIBLE);
                        main_layout.setVisibility(View.GONE);
                        noimg.setVisibility(View.GONE);
                    }

                }
            });

            reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    comments = cmnts.getText().toString();
                    isInternetPresent = cd.isConnectingToInternet();
                    if (isInternetPresent) {
                        mdialog = new Dialog(MainActivity.this);

                        mdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                        mdialog.setContentView(R.layout.custom_progress_dialog);
                        mdialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                        mdialog.setCancelable(false);

                        mdialog.show();
                        new Reject().execute(URL.amateurpath + "Reject");
                    } else {
                        if (imagdialog.isShowing())
                            imagdialog.dismiss();
                        nonetwork.setVisibility(View.VISIBLE);
                        main_layout.setVisibility(View.GONE);
                        noimg.setVisibility(View.GONE);
                    }

                }
            });
            imagdialog.show();

        }
    });

这是我的Viewpager适配器

public class ViewPager_Adaptor extends PagerAdapter {
    private Context context;
    ArrayList<CustomImages> data;
    private LayoutInflater layoutInflater;

    public ViewPager_Adaptor(Context context, ArrayList<CustomImages> arraylist) {
        // TODO Auto-generated constructor stub
        this.context = context;
        data = arraylist;
        Log.d("Abhi", data.toString());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.d("DATASIZE", String.valueOf(data.size()));

        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == (LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemview = layoutInflater.inflate(R.layout.viewpager_img, container, false);
        final ImageView iimg = (ImageView) itemview.findViewById(R.id.imageview1);
        ImageView clockw = (ImageView) itemview.findViewById(R.id.clock);

        ImageView anticlockw = (ImageView) itemview.findViewById(R.id.anticlock);

        clockw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iimg.setRotation(iimg.getRotation() + 90);

            }
        });
        anticlockw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iimg.setRotation(iimg.getRotation() - 90);

            }
        });
        Picasso.with(MainActivity.this).load(data.get(position).getUrl().replaceAll(" ", "%20")).placeholder(R.drawable.temp_img).error(R.drawable.no_media).into(iimg);
        iimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intnt = new Intent(MainActivity.this, ImageShow.class);
                intnt.putExtra("photo", data.get(position).getPhoto());
                intnt.putExtra("url", data.get(position).getUrl());
                startActivity(intnt);
            }
        });


        container.addView(itemview);
        return itemview;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}

最后Listview适配器如下

    public class Image_list extends RecyclerView.Adapter<Image_list.MyViewHolder> {
    ArrayList<CustomImages> arraylist1;
    Context c;

    public Image_list(Context c, ArrayList<CustomImages> arraylist) {
        this.arraylist1 = arraylist;
        this.c = c;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.inflate_image_list, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        Log.v("Position of i", "" + pos);
 //            if (pos == position) {
 //                holder.img.setBackgroundResource(R.drawable.border);
 //            } else {
 //

//}

        Picasso
                .with(this.c)
                .load(arraylist1.get(position).getUrl().replaceAll(" ", "%20"))
                .placeholder(R.drawable.temp_img) // can also be a drawable
                .placeholder(R.drawable.temp_img)
                .error(R.drawable.no_media)
                .into(holder.img);
    }

    @Override
    public int getItemCount() {
        return arraylist1.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;

        public MyViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.catimg);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getLayoutPosition();

                    viewpager.setCurrentItem(position);
                }
            });
        }
    }
}

问题:我想在listview上显示叠加层,只显示当前焦点项目,其他项目看起来模糊不清。

由于对问题的关注,我设法根据viewpager幻灯片滑动列表但我想显示blurissh图像并且只关注listview中显示的listview中的一个图像。

显示在viewpager

中显示的屏幕右侧列表中的图像

1 个答案:

答案 0 :(得分:1)

每当在viewPager中选择一个项目时,获取该项目的位置。将该项的位置保存在适配器中的变量viewPagerPosition中。然后通过调用notifyDataSetChanged()刷新适配器。

在适配器中有一个具有一定透明度的视图,并将其视为默认值。在适配器的onBindViewHolder中检查

if(position = viewPagerPosition){
    transparentView.setVisibility(GONE);
} else{
    transparentView.setVisibility(VISIBLE);
}

这应该使当前视图突出显示,其他视图变暗。如果你想要模糊,那么你可以使用相同条件的模糊。