如果选择一项,请禁用onLongClick并启用onClick

时间:2019-06-03 09:04:35

标签: java android adapter onclicklistener onlongclicklistener

我的应用程序包含多个项目的列表。这些项目同时具有onClick和onLongClick方法。 如果没有选择任何项目,但是如果已经选择了一个项目(通过检查列表),则选中一个项目后,onLongClick应该更改该项目的背景并显示一个选项工具栏(共享,删除,复制等)其中包含所有选中的项目),应禁用onLongClick方法并启用onClick(这也会更改项目的背景,始终保留选项工具栏)。因此,当没有选择任何项目=启用longClick时, 如果至少选择了一项,则=启用onClick以加快选择速度。 为此应用程序,我正在使用RecycelView

(通过选择,我的意思是更改项目的背景并替换工具栏)


适配器

public class myAdapter extends   
RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<ModelloDati> data_list;
private ArrayList<ModelloDati> selected_items_list = new ArrayList<>();
private Context context;
private Uri uri;
public ModelloDati passOggetto;
private static final int tlTEXT = 0;
private static final int tlIMAGE = 1;
private static final int tlVIDEO = 2;
private static final int tlDATE = 3;
private int total_items = 0 ;
private Toolbar options_toolbar;
private Toolbar toolbar;




public class ImageViewHolder extends RecyclerView.ViewHolder {
    //ImageView mImage;
    private ImageView imageView;
    private TextView ora_layout_foto;
    private RelativeLayout selezione_layout_foto;


    public ImageViewHolder(View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.immagine);
        ora_layout_foto = itemView.findViewById(R.id.ora_layout_Foto);
        selezione_layout_foto = 
itemView.findViewById(R.id.layout_selezione_foto);
    }
}


public class DataLayoutHolder extends RecyclerView.ViewHolder {
    private TextView data;
    private CardView sfondo;


    public DataLayoutHolder(@NonNull View itemView) {
        super(itemView);
        data = itemView.findViewById(R.id.Data);
        sfondo = itemView.findViewById(R.id.sfondo_data);
    }
}

public class TextViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    private TextView titolo;
    private TextView ora;
    private RelativeLayout selezione_layout_testo;


    public TextViewHolder(View v) {
        super(v);
        titolo = itemView.findViewById(R.id.testo);
        selezione_layout_testo = 
itemView.findViewById(R.id.layout_selezione_testo);
        ora = itemView.findViewById(R.id.ora);

    }
}

public class VideoViewHolder extends RecyclerView.ViewHolder{
    private ImageView videoView;
    private LinearLayout clicca;
    private TextView ora_layout_video;
    private  RelativeLayout selezione_layout_video;

    public VideoViewHolder(View v) {
        super(v);
        videoView = itemView.findViewById(R.id.rec);
        ora_layout_video = itemView.findViewById(R.id.ora_layout_Video);
        selezione_layout_video = 
itemView.findViewById(R.id.layout_selezione_video);
        clicca = itemView.findViewById(R.id.clicca);

    }



}






public myAdapter(ArrayList<ModelloDati> lista, Context context, Toolbar 
toolbar, Toolbar toolbar_selezionato) {
    this.data_list = lista;
    this.context = context;
    this.options_toolbar = toolbar_selezionato;
    this.toolbar = toolbar;

}


@NonNull
@Override
public RecyclerView.ViewHolder  onCreateViewHolder(@NonNull ViewGroup 
parent, int viewType) {

    if(viewType == tlTEXT) {
        View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_testo,  
parent, false);
        return new TextViewHolder(v);
    }
    else if (viewType == tlIMAGE) {
        View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_foto, 
parent, false);
        return new ImageViewHolder(v);
    }
    else if (viewType == tlVIDEO) {
        View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_video, 
parent, false);
        return new VideoViewHolder(v);
    }
    else if (viewType == tlDATE){
        View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_data, 
parent, false);
        return  new DataLayoutHolder(v);

    }
    else{return null;}
}

















@Override
 public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder 
 myViewHolder, final int position) {
    final ModelloDati oggetto =  
 data_list.get(myViewHolder.getAdapterPosition());
        switch (myViewHolder.getItemViewType()) {

            case tlTEXT:
                final TextViewHolder textViewHolder = (TextViewHolder) 
                 myViewHolder;
                textViewHolder.titolo.setText(oggetto.getTesto());
                textViewHolder.ora.setText(aggiornaTempo(0));
                if((selected_items_list.size() - 1) < 1){


 textViewHolder.selezione_layout_testo.setOnLongClickListener(new 
 View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            onMioClick(oggetto,position,myViewHolder);
                            return true;}
                    });

                }else if(selected_items_list.size() >= 1){

 textViewHolder.selezione_layout_testo.setOnClickListener(new 
 View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           onMioClick(oggetto,position,myViewHolder);
                       }
                   });
                }

            break;

            case tlIMAGE:
                final ImageViewHolder imageViewHolder = (ImageViewHolder) 
 myViewHolder;

 imageViewHolder.imageView.setImageBitmap(oggetto.getImmagini());
                imageViewHolder.ora_layout_foto.setText(aggiornaTempo(0));
                if(selected_items_list.size() > 1){


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

                            onMioClick(oggetto,position,myViewHolder);
                            return true;
                        }
                    });
                }



            break;


            case tlVIDEO:
                final VideoViewHolder videoViewHolder = (VideoViewHolder) 
 myViewHolder;
                uri = oggetto.getUri();


                videoViewHolder.videoView.requestFocus();
                //videoViewHolder.videoView.setVideoURI(uri);
                //videoViewHolder.videoView.seekTo( 100 );
                Glide
                  .with(context)
                  .load(uri)
                  .into(videoViewHolder.videoView);
                videoViewHolder.ora_layout_video.setText(aggiornaTempo(0));
                videoViewHolder.clicca.setOnClickListener(new 
 View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        videoViewHolder.clicca.setOnClickListener(new 
 View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Context context = v.getContext();
                                Intent intent = new 
 Intent(context,FullscreenVideo.class);
                                intent.putExtra("VIDEO",uri.toString());
                                context.startActivity(intent);
                            }
                        });
                    }
                });

 videoViewHolder.selezione_layout_video.setOnLongClickListener(new 
 View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onMioClick(oggetto,position,myViewHolder);
                        return true;
                    }
                });
            break;


            case tlDATE:
                String data = aggiornaTempo(1);
                DataLayoutHolder viewHolder2 = (DataLayoutHolder) 
 myViewHolder;
                viewHolder2.data.setText(data);

                break;


            default:
                Log.e("FATAL","Unable to bind data");


        }

    }








@Override
public int getItemViewType(int position){

    switch (data_list.get(position).getTYPE()) {
        case 0: return tlTEXT;
        case 1: return tlIMAGE;
        case 2: return tlVIDEO;
        case 3: return tlDATE;
    }
    return 0;

}


@Override
public int getItemCount() {
    if (data_list == null)
        return 0;
    return data_list.size();
}







 public void delete(ModelloDati list){

       int position = data_list.indexOf(passOggetto);
        data_list.remove(position);
        notifyItemRemoved(position);

}


private  String aggiornaTempo (int tipo) {


    if(tipo == 0){
       Date CURRENT_TIME = new Date();
       Calendar calendar = GregorianCalendar.getInstance();
       calendar.setTime(CURRENT_TIME);
       String tempo = 
 calendar.get(calendar.HOUR_OF_DAY)+":"+calendar.get(calendar.MINUTE)+" ";
       return tempo;
    }if(tipo == 1){
        Date CURRENT_TIME = new Date();
        Calendar calendar = GregorianCalendar.getInstance();
        int mese = calendar.get(calendar.MONTH);
        calendar.setTime(CURRENT_TIME);
        String tempo = ""+calendar.get(calendar.DAY_OF_MONTH)+" 
 "+ConvertiMese(mese);
        return tempo;
    }
    else{return null;}
}

private  String ConvertiMese (int mese){
    if(mese == 1) return "Gennaio";
    if(mese == 2) return "febbraio";
    if(mese == 3) return "marzo";
    if(mese == 4) return "aprile";
    if(mese == 5) return "maggio";
    if(mese == 6) return "giugno";
    if(mese == 7) return "luglio";
    if(mese == 8) return "agosto";
    if(mese == 9) return "settembre";
    if(mese == 10) return "ottobre";
    if(mese == 11) return "novembre";
    if(mese == 12) return "dicembre";
    else return "Erroe";
}

private void onMioClick(ModelloDati oggetto, int position, 
RecyclerView.ViewHolder myViewHolder) {


    if (getItemViewType(position) == tlTEXT) {
        boolean trovato = false;

        TextViewHolder textViewHolder = (TextViewHolder) myViewHolder;

        for (int i = 0; i <= selected_items_list.size() - 1; i++) {
            if (selected_items_list.get(i) == oggetto) {


 textViewHolder.selezione_layout_testo.setBackgroundColor(
 context.getResources()
 .getColor(R.color.transparent));
                selected_items_list.remove(i);
                total_items--;
                trovato = true;
            }
        }

        if (!trovato) {
            selected_items_list.add(oggetto);
            total_items++;
            textViewHolder.selezione_layout_testo.setBackgroundColor(
            context.getResources().getColor(R.color.Colore_selezione));
            toolbar.setVisibility(View.GONE);
            options_toolbar.setVisibility(View.VISIBLE);
        }

        if (total_items <= 0) {
            toolbar.setVisibility(View.VISIBLE);
            options_toolbar.setVisibility(View.GONE);
        }

    }

    if (getItemViewType(position) == tlIMAGE) {
        boolean trovato = false;
        ImageViewHolder imageViewHolder = (ImageViewHolder) myViewHolder;

        for (int i = 0; i <= selected_items_list.size() - 1; i++) {
            if (selected_items_list.get(i) == oggetto) {
                imageViewHolder.selezione_layout_foto.setBackgroundColor(
                context.getResources().getColor(R.color.transparent));
                selected_items_list.remove(i);
                total_items--;
                trovato = true;
            }
        }

        if (!trovato) {
            selected_items_list.add(oggetto);
            total_items++;
            imageViewHolder.selezione_layout_foto.setBackgroundColor(
            context.getResources().getColor(R.color.Colore_selezione));
            toolbar.setVisibility(View.GONE);
            options_toolbar.setVisibility(View.VISIBLE);
        }

        if (total_items <= 0) {
            toolbar.setVisibility(View.VISIBLE);
            options_toolbar.setVisibility(View.GONE);
        }
    }

    if (getItemViewType(position) == tlVIDEO) {
        boolean trovato = false;

        final VideoViewHolder videoViewHolder = (VideoViewHolder) 
       myViewHolder;

        for (int i = 0; i <= selected_items_list.size() - 1; i++) {
            if (selected_items_list.get(i) == oggetto) {


videoViewHolder.selezione_layout_video.setBackgroundColor(
context.getResources().getColor(R.color.transparent));
                selected_items_list.remove(i);
                total_items--;
                trovato = true;
            }
        }

        if (!trovato) {
            selected_items_list.add(oggetto);
            total_items++;
            videoViewHolder.selezione_layout_video.setBackgroundColor(
            context.getResources().getColor(R.color.Colore_selezione));
            toolbar.setVisibility(View.GONE);
            options_toolbar.setVisibility(View.VISIBLE);
        }

        if (total_items <= 0) {
            toolbar.setVisibility(View.VISIBLE);
            options_toolbar.setVisibility(View.GONE);
        }

    }

    if (total_items > 0) {

    }

}



}

主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/recyclerView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimary"
        android:elevation="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:title="@string/app_name"
        app:titleTextColor="#fff" />

    <android.support.v7.widget.Toolbar
        android:visibility="gone"
        android:id="@+id/toolbar_selezionato"
        android:elevation="20dp"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimary">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageButton
                    android:id="@+id/indietro_toolbar_selezionato"
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:src="@drawable/back_button_toolbar"
                    android:backgroundTint="@color/colorPrimary"/>


            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:layout_marginEnd="55dp">
                <ImageButton
                    android:id="@+id/cancella_toolbar_selezionato"
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:src="@drawable/delete_button_toolbar"
                    android:backgroundTint="@color/colorPrimary"/>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end">

                <ImageButton
                    android:id="@+id/condividi_toolbar_selezionato"
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:src="@drawable/share_button_toolbar"
                    android:backgroundTint="@color/colorPrimary" />

            </RelativeLayout>



        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

</RelativeLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#0F0294A5"
    android:descendantFocusability="blocksDescendants"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/relativeLayout" />


<LinearLayout
    android:focusable="true"
    android:elevation="8dp"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:orientation="horizontal">

    <EditText
        android:layout_gravity="center_vertical"
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="55dp"
        android:background="#0003A9F4"
        android:hint="Scrivi"
        android:padding="10dp" />




    <Button
        android:layout_gravity="center_vertical"
        android:id="@+id/camera"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/camera"
      />

    <Button
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="0.5dp"
        android:id="@+id/video"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/video"
        />

    <Button
        android:layout_gravity="center_vertical"
        android:id="@+id/send"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@mipmap/send" />


</LinearLayout>



 </android.support.constraint.ConstraintLayout>

0 个答案:

没有答案