RecyclerView中的两个适配器工作?

时间:2017-03-24 03:31:02

标签: android android-recyclerview onclicklistener swiperefreshlayout

在我的适配器中,我传递的是SwipeRefreshLayout(List<>)和OnClickListener(ListenerClickDiches)。当我使用它们中的任何一个时,如果它工作。但是,当我同时实现这两个时,它对我不起作用。你能帮我知道错误是什么吗?

这里是Main

public class DichasActivity extends AppCompatActivity 
 implements AdapterDichas.EscuchaClickDichas {

RecyclerView rdichas;
LinearLayoutManager layoutManager;
AdapterDichas adaptador;

public static final String EXTRA_POSICION = "com.sss.frases_de_amor.extra.POSICION";

private SwipeRefreshLayout refreshLayout;
private static final int CANTIDAD_ITEMS = 5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dichas);
    showToolbar(getResources().getString(R.string.toolbar_title_dichas),true);

    refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshDichas);

    rdichas = (RecyclerView) findViewById(R.id.recicladorDichas);

    layoutManager = new LinearLayoutManager(this);
    rdichas.setLayoutManager(layoutManager);

    adaptador = new  AdapterDichas(this, Dichas.randomList(CANTIDAD_ITEMS));
    rdichas.setAdapter(adaptador);



    refreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    new HackingBackgroundTask().execute();
                }
            }
    );
    }

private class HackingBackgroundTask extends AsyncTask<Void, Void, List<Dichas>> {

    static final int DURACION = 3 * 1000; // 3 segundos de carga

    @Override
    protected List doInBackground(Void... params) {
        // Simulación de la carga de items
        try {
            Thread.sleep(DURACION);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Retornar en nuevos elementos para el adaptador
        return Dichas.randomList(CANTIDAD_ITEMS);
    }

    @Override
    protected void onPostExecute(List result) {
        super.onPostExecute(result);

        // Limpiar elementos antiguos
        adaptador.clear();

        // Añadir elementos nuevos
        adaptador.addAll(result);

        // Parar la animación del indicador
        refreshLayout.setRefreshing(false);
    }

}

public void showToolbar(String title2, boolean upButton) {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(title2);
    getSupportActionBar().setDisplayHomeAsUpEnabled(upButton);
}

@Override
public void onItemClick(RecyclerView.ViewHolder holder, int posicion) {
    Intent intent = new Intent(this, DetalleDichasActivity.class);
    intent.putExtra(EXTRA_POSICION, posicion);
    startActivity(intent);
}
}

此处详情

public class DetalleDichasActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detalle_dichas);

    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    anadirToolbar();
    ImageView vfd = (ImageView) findViewById(R.id.verFotoDichas);

    int posicion = getIntent().getIntExtra(DichasActivity.EXTRA_POSICION, -1);
    Dichas itemSeleccionado =Dichas.FOTOSDICHAS.get(posicion);

    Glide.with(this).load(itemSeleccionado.getImagendichas()).into(vfd);

}

private void anadirToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarDetalleDichas);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
    }
   } }

此处适配器

 public class AdapterDichas extends 
 RecyclerView.Adapter<AdapterDichas.DichasViewHolder>{

private List<Dichas> items;



public void addAll(List<Dichas> lista){
    items.addAll(lista);
    notifyDataSetChanged();
}
public void clear(){
    items.clear();
    notifyDataSetChanged();
}

public interface EscuchaClickDichas {
    void onItemClick(RecyclerView.ViewHolder holder, int posicion);
}

private EscuchaClickDichas escucha;

public AdapterDichas(EscuchaClickDichas escucha,List<Dichas> items) {
    this.escucha = escucha;
    this.items = items;
}



public class DichasViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener {

    public ImageView imageviewdichas;

    public DichasViewHolder(View itemView) {
        super(itemView);
        imageviewdichas = (ImageView) itemView.findViewById(R.id.pictureCardView);
        itemView.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        escucha.onItemClick(this, getAdapterPosition());
    }
}

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

@Override
public void onBindViewHolder(DichasViewHolder holder, int position) {
    //Paso de datos, generando tarjetas
    Dichas dic = Dichas.FOTOSDICHAS.get(position);

    Glide.with(holder.imageviewdichas.getContext()).load(dic.getImagendichas()).into(holder.imageviewdichas);
}

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

此处数据

 public class Dichas {

private int imagendichas;

public Dichas(int imagendichas) {
    this.imagendichas = imagendichas;
}

public int getImagendichas() {
    return imagendichas;
}

public void setImagendichas(int imagendichas) {
    this.imagendichas = imagendichas;
}

public static final List<Dichas> FOTOSDICHAS = new ArrayList<>();

static {
    FOTOSDICHAS.add(new Dichas(R.drawable.i05));
    FOTOSDICHAS.add(new Dichas(R.drawable.image));
    FOTOSDICHAS.add(new Dichas(R.drawable.image));
    FOTOSDICHAS.add(new Dichas(R.drawable.i05));
    FOTOSDICHAS.add(new Dichas(R.drawable.image));
}

public static ArrayList randomList(int count) {
    Random random = new Random();
    HashSet items = new HashSet();

    // Restricción de tamaño
    count = Math.min(count, FOTOSDICHAS.size());

    while (items.size() < count) {
        items.add(FOTOSDICHAS.get(random.nextInt(FOTOSDICHAS.size())));
    }

    return new ArrayList(items);
}}

0 个答案:

没有答案