gpt4 book ai didi

Android RecyclerView 空白

转载 作者:搜寻专家 更新时间:2023-11-01 08:32:29 25 4
gpt4 key购买 nike

我有一个 android 项目,我正在使用 Recycler View 打印包含项目的列表,每个项目只有一个图像,在这些图像之间,android 正在创建一个大空白。我研究并看到将我的布局更改为 wrap_content 我将其更改为回收站 View 布局、itemlayout、页面布局但没有用,我想知道我还需要更改哪些选项,我将发布这是我的 2 个类, Activity 和适配器,以及布局 XML 和项目 XML(Recycler viewItem)

Activity :

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>

有人可以帮帮我吗?对不起葡萄牙语的名字和评论。有任何问题请问我。非常感谢

最佳答案

RecyclerView 项目布局有问题。您已将 match_parent 属性赋予 ImageView。取而代之的是像 48dp 这样的常数大小。

关于Android RecyclerView 空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39522509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com