从gridview中删除项目

时间:2015-10-06 13:39:25

标签: java android gridview adapter

按下按钮时,我从网格视图中删除图像。

图像确实删除了很好,并且gridview也会通过调用更新" adapter.notifyDataSetChanged();"。

当移除图像视图时,此位置旁边的另一个图像应该占据其位置。这种情况会发生,但这里的图像不会重新加载,所以只有一个空白区域?如何让此imageview重新加载其图像?

问题:enter image description here

这是我的gridadapter:

public class FavoriteMovieGridAdapter extends BaseAdapter {
Context context;
ArrayList<DataFavorites> List;
FavoritesMovie fragment;
private static LayoutInflater inflater = null;
FavoriteMovieGridAdapter adapter = this;


public FavoriteMovieGridAdapter(Context context, ArrayList<DataFavorites> List, FavoritesMovie fragment) {
    this.context = context;
    this.List = List;
    this.fragment = fragment;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return List.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    // Avoid unneccessary calls to findViewById() on each row
    final ViewHolder holder;

    /*
     * If convertView is not null, reuse it directly, no inflation
     * Only inflate a new View when the convertView is null.
     */

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.favorite_grid_item, null);

        holder = new ViewHolder();
        holder.poster = (ImageView) convertView.findViewById(R.id.upcoming_image);

        holder.editbutton = (ImageView) convertView.findViewById(R.id.delete_item);
        // The tag can be any Object, this just happens to be the ViewHolder
        convertView.setTag(holder);
    }
    else{

        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();

    }


    final View finalConvertView = convertView;

    final DataFavorites e;
    new DataFavorites();
    e = List.get(position);

    String url = String.valueOf(e.getUrl());

    // load image url into poster

// Seems as if this doesn't run for the imageview next to this when this view is removed?              
  Picasso.with(context).load(url).fit().placeholder(R.drawable.movie_back).into(holder.poster);

    // Create onclick and show edit button

    convertView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {


     // show edit button
     holder.editbutton.setVisibility(View.VISIBLE);

     YoYo.with(Techniques.FadeIn).duration(700).playOn(finalConvertView.findViewById(R.id.delete_item));



            // onclick edit button remove item and update gridview
     holder.editbutton.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {

      YoYo.with(Techniques.ZoomOut).duration(700).playOn(finalConvertView.findViewById(R.id.favorite_relative));


      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
                        public void run() {


       adapter.List.remove(e);
       adapter.notifyDataSetChanged();


        // Delete specific movie from data base
        DatabaseHandlerMovie db = new DatabaseHandlerMovie(context);
        // Reading all movies
        db.deleteMovie(e);

            }
                    }, 1000);




                }
            });

            return false;
        }
    });


    return convertView;
}

static class ViewHolder {
    ImageView poster;
    ImageView editbutton;
}



}

我的网格项布局:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/favorite_relative"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal">



<ImageView android:layout_width="123.3dp"
    android:layout_height="185.3dp"
    android:id="@+id/upcoming_image"
    android:scaleType="fitXY" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/delete_item"
    android:src="@drawable/ic_remove_circle_outline_white_24dp"
    android:tint="@color/colorPrimaryDark"
    android:clickable="true"
    android:visibility="gone"
    android:layout_alignRight="@+id/upcoming_image"
    android:layout_alignEnd="@+id/upcoming_image"/>

    </RelativeLayout>

我的网格布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Favorites"
android:background="#FFFFFF"
android:focusableInTouchMode="true">



<GridView

    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/movies_gridlayout"
    android:paddingRight="-1dp"
    android:paddingEnd="-1dp"
    android:numColumns="3"
    android:background="#ffffff"
    android:visibility="invisible"/>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/favorite_none"
    android:text="No favorite movies found"
    android:textSize="15sp"
    android:visibility="gone"
    android:textColor="@color/SecondaryText"
    />


<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        >
    </ProgressBar>
    </FrameLayout>

2 个答案:

答案 0 :(得分:0)

原来第三方的lib造成了这个:

@Bean
public IntegrationFlow serviceFlow() {

    return IntegrationFlows
            .from(Jms
                    .inboundGateway(serviceMLC)
                    .autoStartup(true)
                    .defaultReplyDestination(responseMqQueue)
                    .replyChannel(responseOutCh)
                    .replyTimeout(180000)
                    .correlationKey("JMSCorrelationID")
                    .errorChannel(serviceErrorCh)
                    )
            .channel(serviceInCh)
            .get();
}

答案 1 :(得分:0)

您将拥有gridview的适配器和主要活动

因此,您必须在两者中都实现公共接口才能进行通信

以下代码可能会对您有所帮助:)

在适配器类中添加以下代码

private OnItemClickListner onItemClickListner;
public MyAdapter(Context c, List<PDFDoc> pdfDocs,OnItemClickListner onItemClickListner) {
          this.context = c;
          this.pdfDocs = pdfDocs;
          this.onItemClickListner=onItemClickListner;
        }
     
public interface OnItemClickListner {
         void removefromadapter(int position);
        }
    
ImageButton imageButton = view.findViewById(R.id.cancelid);
imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // write your stuff here
            onItemClickListner.removefromadapter(position);
        }
    });

在gridview的MainActivity中添加以下代码

需要实现OnItemClickListner **

public class addclient_fragment extends Fragment implements MyAdapter.OnItemClickListner {

您将需要在mainactivity中添加此方法

@Override
    public void removefromadapter(int position) {
        // write your stuff here
    }