【问题标题】:In android application RecyclerView view show only half of the phone with在 android 应用程序 RecyclerView 视图中只显示手机的一半
【发布时间】:2019-01-03 06:08:25
【问题描述】:

在我的activity_main.xml中| 在我的 android RecyclerView 中只显示一半的屏幕,我无法找出错误。帮帮我,伙计们。我使用了不同的布局,例如 relativelayout、constraintlayout、linearlayout。但我只得到了屏幕 RecyclerView 的一半大小。

 <?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>

这里附上我的 MainActivity.java

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);

        }
    }

下面我附上我的适配器类

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;
    }
}

我在下面附上我的项目 xml

<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>

【问题讨论】:

  • 所以你是说recyclerView占据了手机屏幕宽度的一半?
  • 使用这个 View view=layoutInflater.inflate(R.layout.recyclerview_items,viewGroup,false); 而不是这个View view = layoutInflater.inflate(R.layout.recyclerview_items, null);
  • 感谢您的重播。我更改了相对布局中的所有布局。我解决了我的问题。但我无法在约束布局中实现。

标签: android android-recyclerview


【解决方案1】:

在您正在为 onCreateViewHolder 中的项目充气的适配器中,使用以下代码块:

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);

【讨论】:

  • 感谢您的重播。我更改了相对布局中的所有布局。我解决了我的问题。但我无法在约束布局中实现。
  • 在使用数据绑定时遇到了同样的问题 dnt 知道为什么但是这个黑客解决了问题谢谢:)
【解决方案2】:

我没有添加评论的权限,因此在此处发布。提前抱歉。 您的问题不太清楚,您是否希望您的RecyclerView 占据整个屏幕(即parent)。如果是这种情况,我想如果你在RecyclerView 中添加属性android:layout_weight 将解决问题。 以下是 developer.android 网站上的属性说明:

LinearLayout 还支持使用android:layout_weight 属性为单个孩子分配权重。这个属性根据它应该在屏幕上占据多少空间来为视图分配一个“重要性”值。较大的权重值允许它扩展以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后将视图组中的任何剩余空间按照其声明权重的比例分配给子视图。默认权重为零。

希望这个链接对https://developer.android.com/guide/topics/ui/layout/linear有帮助

我也已经问过这个问题了:What is the connection between setVisibility and layout_weight in Linear Layout

如果我错了,请纠正我。

【讨论】:

  • 感谢您的重播。我更改了相对布局中的所有布局。我解决了我的问题。但我无法在约束布局中实现。
【解决方案3】:

尝试使用以下内容更改activity_main.xml 文件中的RecyclerView 元素,删除center_in_parent 并更改android:layout_height="wrap_content"

<?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>

编辑 1 - 使用 ConstraintLayout,因为在 RecyclerView 之后您还想显示按钮,所以您需要在 NestedScrollView 中添加 ConstraintLayout

<?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>

【讨论】:

  • 感谢您的重播。我更改了相对布局中的所有布局。我解决了我的问题。但我无法在约束布局中实现。
  • 在 ConstraintLayout 中,更改 android:layout_height="0dp" 并添加这两行 - app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent" 以实现相同的效果。
  • 感谢您的重播。在我使用受约束时,recyclerview items.xml 中没有正确定位。
  • 给我一点时间来更新我对上述问题的解决方案的答案。
  • 我已经更新了答案,现在使用ConstraintLayout在recyclerView之后显示顶部的搜索视图和底部的按钮,希望这会有所帮助。
【解决方案4】:
    items.xml
    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout 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:text="fdfsfsdfsd"
            android:layout_margin="5dp"/>

                <Button
                    android:id="@+id/btnAdd"
                    android:layout_width="wrap_content"
                    android:layout_below="@+id/tvName"
                    android:layout_height="wrap_content"
                    android:text="Add Items"/>
                <Button
                    android:layout_below="@+id/tvName"
                    android:id="@+id/btnMinus"
                    android:layout_alignParentEnd="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add Items"/>        
        </RelativeLayout>

activity_main.xml

    <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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多