在我的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>
答案 0 :(得分:1)
RecyclerView
文件中的activity_main.xml
元素,删除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
之后您还想显示按钮,所以您需要在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>
答案 1 :(得分:1)
我没有添加评论的特权,因此无法在此处发布评论。不好意思
您的问题不太清楚,您是否希望RecyclerView
占据整个屏幕(即parent
)。如果是这样的话,我猜如果使用LinearLayout
在android:layout_weight
中添加属性RecyclerView
可以解决问题。
这是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
如果我错了,请纠正我。
答案 2 :(得分:0)
在您要在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);
答案 3 :(得分:0)
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>