1
votes

Dans l'application Android, la vue RecyclerView n'affiche que la moitié du téléphone avec

Ici dans mon activity_main.xml | dans mon Android RecyclerView montre seulement la moitié de l'écran, je ne suis pas en mesure de découvrir l'erreur. Aidez-moi les gars. J'ai été utilisé avec une disposition différente comme la disposition relative, la disposition des contraintes, la disposition linéaire. mais je n'obtenais que la moitié de l'écran RecyclerView avec la taille.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<TextView
    android:id="@+id/tvName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textSize="16sp"
    android:layout_margin="5dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btnAdd"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Add Items"/>
        <Button
            android:id="@+id/btnMinus"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Add Items"/>

    </LinearLayout>
</LinearLayout>

Ici, je joins mon MainActivity.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> implements Filterable {

    public Context context;
    private List<Model> modelList;
    private List<Model> myfilterList;


    public RecyclerViewAdapter(Context context, List<Model> modelList) {
        this.context = context;
        this.modelList = modelList;
        this.myfilterList = modelList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.recyclerview_items, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Model model = myfilterList.get(i);
        myViewHolder.llMain.setTag(i);
        myViewHolder.textView.setText(model.getName());
    }

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

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String text = constraint.toString();
                if (text.isEmpty()) {
                    myfilterList = modelList;
                } else {
                    ArrayList<Model> filterable = new ArrayList<>();
                    for (Model model : modelList) {
                        System.out.println("check equal " + model.getName() 
                         + "   " + text);
                        if (model.getName().toLowerCase().contains(text)) {
                            filterable.add(model);
                        }
                    }
                    myfilterList = filterable;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = myfilterList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                myfilterList = (ArrayList<Model>) results.values;
                notifyDataSetChanged();

            }
        };
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        Button btnAdd, btnMinus;
        LinearLayout llMain;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tvName);
            btnAdd = itemView.findViewById(R.id.btnAdd);
            btnMinus = itemView.findViewById(R.id.btnMinus);
            llMain = itemView.findViewById(R.id.llMain);

            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (Integer) llMain.getTag();
                    System.out.println("linear layout position details 
                    "+pos);
                    myfilterList.add(pos+1, new Model("dummy"));
                    notifyItemChanged(pos+1);
                    notifyDataSetChanged();
                }
            });
        }
    }
    public List<Model> getList()
    {
        return myfilterList;
    }
}

Ici, je joins ci-dessous ma classe d'adaptateur

package com.example.admin.recyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


    public class MainActivity extends AppCompatActivity {

        private List<Model> models;
        RecyclerView recyclerView;
        private RecyclerView.LayoutManager layoutManager;
        public SearchView searchView;
        private Button button;
        private RecyclerViewAdapter recyclerViewAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            searchView = findViewById(R.id.search);
            button = findViewById(R.id.btnGet);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<Model> models1 = recyclerViewAdapter.getList();
                    System.out.println("final order list "+new Gson().toJson(models1));
                }
            });
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    if (recyclerViewAdapter != null) {
                        recyclerViewAdapter.getFilter().filter(newText);
                    }
                    return true;
                }
            });

            //recyclerView.setHasFixedSize(true);

            models = new ArrayList<>();
            models.add(new Model("rajesh"));
            models.add(new Model("deva"));
            models.add(new Model("merlin"));
            models.add(new Model("antony"));
            models.add(new Model("giri"));
            models.add(new Model("guru"));
            System.out.println("get the position " + 
            models.get(0).getName());
            System.out.println("json view "+new Gson().toJson(models));
            recyclerViewAdapter = new 
            RecyclerViewAdapter(getApplicationContext(), models);

            recyclerView.setAdapter(recyclerViewAdapter);

        }
    }

Ici, je joins mon article xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        tools:context=".MainActivity">

        <android.support.v7.widget.SearchView
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:focusable="false"
            android:visibility="gone"
            app:iconifiedByDefault="false"
            app:queryHint="Search Distributors..." />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_below="@+id/search"
            android:layout_centerInParent="true"
            android:layout_height="match_parent" />

        <Button
            android:id="@+id/btnGet"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_alignParentBottom="true"
            android:text="get" />


    </RelativeLayout>


3 commentaires

Vous dites donc que recyclerView occupe la moitié de la largeur de l'écran du téléphone?


Utilisez ce View view = layoutInflater.inflate (R.layout.recyclerview_items, view‌ Group, false); au lieu de ce View view = layoutInflater.inflate (R .layout.recyclerview_items, null);


MERCI pour votre rediffusion. J'ai changé toutes les mises en page dans la mise en page relative. je résous mon problème. mais je ne peux pas réaliser dans la disposition de contrainte.


4 Réponses :


1
votes

Essayez de changer l'élément RecyclerView dans votre fichier activity_main.xml avec ce qui suit, supprimez center_in_parent et modifiez android: layout_height = "wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimaryDark"
        android:focusable="false"
        app:iconifiedByDefault="false"
        app:queryHint="Search Distributors..." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/search"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnGet"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentBottom="true"
        android:text="get" />


</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

Edit 1 - Utilisation de ConstraintLayout , car après le RecyclerView , vous souhaitez également afficher le bouton, vous aurez besoin du ConstraintLayout à l'intérieur de NestedScrollView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:focusable="false"
        android:visibility="gone"
        app:iconifiedByDefault="false"
        app:queryHint="Search Distributors..." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_below="@+id/search"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnGet"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentBottom="true"
        android:text="get" />


</RelativeLayout>


7 commentaires

MERCI pour votre rediffusion. J'ai changé toutes les mises en page dans la mise en page relative. je résous mon problème. mais je ne peux pas réaliser dans la disposition de contrainte.


Dans ConstraintLayout, changez android: layout_height = "0dp" et ajoutez ces deux lignes - app: layout_constraintBottom_toBottomOf = "parent" & app: layout_constraintTop_toTopOf = "parent" < / code> pour obtenir la même chose.


Merci pour votre rediffusion. dans recyclerview items.xml n'est pas correctement positionné lorsque j'utilisais contraint.


Donnez-moi un moment pour mettre à jour ma réponse avec la solution à votre problème ci-dessus.


J'ai mis à jour la réponse, utilise maintenant ConstraintLayout pour afficher la vue de recherche en haut et le bouton en bas après le recyclerView, j'espère que cela vous aidera.


@rajeshdeva Veuillez accepter la réponse si cette solution a fonctionné pour vous :)


@ hsm59 j'ai donné un vote positif. comment faire accepter la réponse.? . désolé, je suis nouveau sur stackoverflow.



1
votes

Dans votre adaptateur où vous gonflez l'élément dans onCreateViewHolder, utilisez ce bloc de code:

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);


2 commentaires

MERCI pour votre rediffusion. J'ai changé toutes les mises en page dans la mise en page relative. je résous mon problème. mais je ne peux pas réaliser dans la disposition de contrainte.


faisait face au même problème lors de l'utilisation de la liaison de données, sachez pourquoi mais ce hack a été résolu par problème merci :)



2
votes

Je n'ai pas les privilèges pour ajouter un commentaire, donc le poster ici. Désolé d'avance. Votre question n'est pas tout à fait claire, voulez-vous que votre RecyclerView occupe tout l'écran (c'est-à-dire parent ). Si tel est le cas, avec LinearLayout je suppose que si vous ajoutez un attribut android: layout_weight dans votre RecyclerView , cela résoudra le problème. Voici la description de l'attribut sur le site developer.android:

<₹ LinearLayout prend également en charge l'attribution d'un poids à des enfants individuels avec l'attribut android: layout_weight . Cet attribut attribue une valeur «d'importance» à une vue en termes d'espace qu'elle doit occuper à l'écran. Une valeur de poids plus élevée lui permet de s'étendre pour remplir tout espace restant dans la vue parent. Les vues enfants peuvent spécifier une valeur de pondération, puis tout espace restant dans le groupe de vues est attribué aux enfants dans la proportion de leur poids déclaré. Le poids par défaut est zéro.

J'espère que ce lien aidera https://developer.android.com/guide / topics / ui / layout / linear

De plus, j'ai déjà posé cette question: Quel est le lien entre setVisibility et layout_weight dans la mise en page linéaire

Corrigez-moi si je me trompe.


1 commentaires

MERCI pour votre rediffusion. J'ai changé toutes les mises en page dans la mise en page relative. je résous mon problème. mais je ne peux pas réaliser dans la disposition de contrainte.



0
votes
    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:focusable="false"
        android:visibility="visible"
        app:iconifiedByDefault="false"
        app:queryHint="Search Distributors..." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search" />
    <Button
        android:id="@+id/btnGet"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_alignParentBottom="true"
        android:text="get" />


</android.support.constraint.ConstraintLayout>

0 commentaires