使用layout.swipe从页面适配器中删除特定位置页面

时间:2017-03-31 04:38:43

标签: java android swipe android-pageradapter

我在这里遇到了问题。最近我正在开展一个像火种这样的项目,但这并不是那么进步。我做的更简单。

有一个可以滑过不同页面的滑动页面。你可以在每个页面中按压或不喜欢。

我想在这里做的是当他们点击“赞”按钮时,该页面将会消失并自动转到下一页。

我创建了带有喜欢和不喜欢按钮的幻灯片页面,但现在问题是

如何点击类似按钮,该页面将消失,它将自动转到下一页。

我会在下方附上我的寻呼机适配器,希望有人可以帮助我。如果您需要更多信息,请告诉我。非常感谢您提前。

PagerAdapter

package com.example.lenovo.maidfriends;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;



import java.util.ArrayList;

import static android.widget.Toast.LENGTH_SHORT;

/**
 * Created by lenovo on 8/3/2017.
 */

public class CustomAdapter extends PagerAdapter{
    Activity activity;
    ArrayList<ImageData> images = new ArrayList<>();
    LayoutInflater inflater;

    public CustomAdapter(Activity activity, ArrayList<ImageData> images){
        this.activity = activity;
        this.images = images;
    }

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

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

    @Override
    public int getItemPosition(Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        inflater = (LayoutInflater)activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View itemView = inflater.inflate(R.layout.swipe,container,false);

        ImageView image;
        image = (ImageView)itemView.findViewById(R.id.imageView);
        Button like;
        like = (Button) itemView.findViewById(R.id.button6);

        DisplayMetrics dis = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dis);
        int height = dis.heightPixels;
        int width = dis.widthPixels;
        image.setMinimumHeight(height);
        image.setMinimumWidth(width);

        final String url = images.get(position).getImageUrl();

        image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                String clickedId = images.get(position).getId();
                // Do something with the selected image Id
                Intent intent=new Intent(view.getContext(),viewPersonal.class);
                Bundle b = new Bundle();
                b.putString("myID", clickedId);
                b.putString("myImage",url);
                intent.putExtras(b);
                view.getContext().startActivity(intent);
            }
        });

        like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Remove the current possition page and make next page to current page
            }
        });

        try{
            Picasso.with(activity.getApplicationContext())
                    .load(url)
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .into(image);
        }catch (Exception ex){

        }

        container.addView(itemView);
        return itemView;
    }

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

}

1 个答案:

答案 0 :(得分:1)

试试这个

images.remove(postion); // This will remove that current page in ViewPager
notifyDataSetChanged(); // This will reload viewpager