Android RecyclerView空白区域

时间:2016-09-16 01:17:57

标签: android android-recyclerview

我有一个Android项目,我使用Recycler视图打印带有项目的列表,每个项目只有一个图像,在这些图像之间,android正在创建一个很大的空白区域。我研究并看到要将我的布局更改为wrap_content,并将其更改为回收器视图布局,itemlayout,页面布局但无法正常工作,我想知道我还需要更改哪些选项,我会发布这里是我的2个类,活动和适配器,以及布局XML和项目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);
        }
    };
}

}

ADAPTER

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>

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

3 个答案:

答案 0 :(得分:2)

问题在于RecyclerView项目布局。您已将match_parent属性提供给ImageView。而不是像48dp那样给出恒定的大小。

答案 1 :(得分:1)

在lista_item布局中,您应将高度设置为wrap_content或恒定大小。

答案 2 :(得分:0)

您必须在lista_item中指定ImageView的高度,而不是将高度设置为match_parent