Android RecyclerView空白空间 [英] Android RecyclerView Blank Space

查看:94
本文介绍了Android RecyclerView空白空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android项目,我正在使用Recycler视图打印带有项目的列表,每个项目只有一个图像,而在这些图像之间,android正在创建一个很大的空白.我进行了研究,看到将布局更改为wrap_content,然后将其更改为回收站视图布局,itemlayout,页面布局,但没有用,我想知道我还需要更改哪些选项,我将发布这里是我的2个类,活动和适配器,以及布局XML和项目XML(Recycler viewItem)

I have an android project and I am using Recycler view to print a list with items, each item has just one image and between those images, android is creating a big blank space. I researched and saw about to change my layout to wrap_content and I changed it the recycler view layout, the itemlayout, the layout of the page but didn't work, I want to know what more options do I have to change, I will post here my 2 classes, the activity, and the adapter, and both layout XML and the item XML (Recycler viewItem )

活动:

public class MenuPrincipal extends AppCompatActivity {

private RecyclerView recyclerView;
private MenuEsportesAdapter adapterGrupos;
private ArrayList<MenuEsporte> listaGruposMenu;
private ImageView imgTopo;

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

    imgTopo = (ImageView) findViewById(R.id.MenuPrincipal_imagemTopo);
    imgTopo.setImageResource(R.drawable.sol);

    MenuEsporte me = new MenuEsporte();

    listaGruposMenu = me.getAllEsportes();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewMenuGrupo);

    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setHasFixedSize(false);
    adapterGrupos = new MenuEsportesAdapter(this, listaGruposMenu, onClickGruposMenu());
    recyclerView.setAdapter(adapterGrupos);

}

private MenuEsportesAdapter.MenuEsporteOnClickListener onClickGruposMenu(){
    return new MenuEsportesAdapter.MenuEsporteOnClickListener(){

        @Override
        public void onClickMenuEsporte(MenuEsportesAdapter.MenuEsportesViewHolder holder, int idx) {
            Toast.makeText(getApplicationContext(),"Clicou " + idx, Toast.LENGTH_SHORT).show();

            Intent i = new Intent(getApplicationContext(),JogosClassificacao.class);
            Bundle bundle = new Bundle();
            bundle.putString("Esporte", listaGruposMenu.get(idx).nomeEsporte);
            i.putExtras(bundle);
            startActivity(i);
        }
    };
}

}

适配器

public class MenuEsportesAdapter extends RecyclerView.Adapter<MenuEsportesAdapter.MenuEsportesViewHolder> {
protected static final String TAG = "livroandroid";
private final List<MenuEsporte> listaMenuEsporte;
private final Context context;
private final MenuEsporteOnClickListener onClickListener;

public interface MenuEsporteOnClickListener {
    public void onClickMenuEsporte(MenuEsportesViewHolder holder, int idx);
}

public MenuEsportesAdapter(Context context, List<MenuEsporte> listaMenuEsporte, MenuEsporteOnClickListener onClickListener) {
    this.context = context;
    this.listaMenuEsporte = listaMenuEsporte;
    this.onClickListener = onClickListener;
}

@Override
public MenuEsportesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    // Este método cria uma subclasse de RecyclerView.ViewHolder
    // Infla a view do layout
    View view = LayoutInflater.from(context).inflate(R.layout.lista_item, viewGroup, false);
    // Cria a classe do ViewHolder
    MenuEsportesViewHolder holder = new MenuEsportesViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final MenuEsportesViewHolder holder, final int position) {
    // Este método recebe o índice do elemento, e atualiza as views que estão dentro do ViewHolder
    MenuEsporte c = listaMenuEsporte.get(position);
    // Atualizada os valores nas views
    //holder.tNome.setText(c.nomeEsporte);
    holder.img.setImageResource(c.fotoResource);

    //holder.img.setImageURI(Uri.fromFile(file));

    // Click
    if (onClickListener != null) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Chama o listener para informar que clicou no Planeta
                onClickListener.onClickMenuEsporte(holder, position);
            }
        });
    }
}

@Override
public int getItemCount() {
    return this.listaMenuEsporte != null ? this.listaMenuEsporte.size() : 0;
}

// Subclasse de RecyclerView.ViewHolder. Contém todas as views.
public static class MenuEsportesViewHolder extends RecyclerView.ViewHolder {
    public TextView tNome;
    ImageView img;
    private View view;

    public MenuEsportesViewHolder(View view) {
        super(view);
        this.view = view;
        // Cria as views para salvar no ViewHolder
        //tNome = (TextView) view.findViewById(R.id.textViewNome);
        img = (ImageView) view.findViewById(R.id.imageViewFoto);
    }
}

}

还有xmls

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<!--
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#000000"
    android:id="@+id/textViewNome"/>
    -->
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageViewFoto"/>

 </LinearLayout>

第二:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="juriteam.br.com.thr_sportgames.paginas.MenuPrincipal"
tools:showIn="@layout/activity_menu_principal"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:id="@+id/MenuPrincipal_imagemTopo"
    android:layout_gravity="center_horizontal" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewMenuGrupo"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:layout_width="match_parent"/>

</LinearLayout>

有人可以帮助我吗?抱歉,葡萄牙语的名称和评论.有任何问题请问我.非常感谢

Can someone please help me ? Sorry about the names and comments in portuguese. Any question please ask me. Thank you so much

推荐答案

问题出在 RecyclerView 项目布局上.您已将 match_parent 属性赋予了 ImageView .取而代之的是提供恒定的大小,例如 48dp .

Problem is with RecyclerView item layout. You have given match_parent property to ImageView. Instead of this give constant size like 48dp.

这篇关于Android RecyclerView空白空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆