Android关于RecyclerView的显示问题

__椎名真白 2018-03-08 04:17:30
嗨。大家好。又是我。
刚想测试一下界面显示。结果RecyclerView的显示并不是按照预期的显示。

刚进来看着一点问题没有。向下翻就开始了。每一条都隔非常大的距离。目测是我定义的一个LinearLayout的高度。
接着往回滚动也会变那么大。想不明白为什么会这样。
这是我定义的界面布局。都贴出来吧,万一是有其他控件影响呢,麻烦大佬看一下。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<!-- 内容区 -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:layout_marginBottom="61dp">

<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="搜索单词" />

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</android.support.v7.widget.RecyclerView>

</LinearLayout>

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true">

<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />

<Button
android:id="@+id/word_btn_hide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/button_selector"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/review_btn_hide"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/word_btn_back"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@id/guideline"
app:layout_constraintRight_toRightOf="parent"
style="?android:attr/borderlessButtonStyle" />

<Button
android:id="@+id/word_btn_back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/button_selector"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/review_btn_back"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/guideline"
style="?android:attr/borderlessButtonStyle" />

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

</android.support.design.widget.CoordinatorLayout>



这是我定义的TestBean
public class TestBean {

private String name;

public TestBean(String name){
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

这是我定义的适配器
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder>{

private List<TestBean> mList;

static class ViewHolder extends RecyclerView.ViewHolder{

TextView name;

public ViewHolder(View view){
super(view);
name = (TextView)view.findViewById(R.id.test_name);
}
}

public TestAdapter(List<TestBean> strList){
mList = strList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.string_item,parent,false);
ViewHolder holder = new ViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

TestBean name = mList.get(position);
holder.name.setText(name.getName());

}

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

这是我在活动中的使用
public class WordActivity extends BaseActivity {

Toolbar toolbar;
ActionBar actionBar;

private List<TestBean> nameList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word);

toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar); //使用Toolbar

actionBar = getSupportActionBar(); //获取ActionBar实例,具体实现由Toolbar完成
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true); //显示导航按钮设置为true
}

//返回按钮点击事件
Button button_back = (Button) findViewById(R.id.word_btn_back);
button_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WordActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});

//RecyclerView测试区
initNames();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
TestAdapter adapter = new TestAdapter(nameList);
recyclerView.setAdapter(adapter);

}

private void initNames() {

for(int i = 0;i < 40;i++){
TestBean testBean = new TestBean("---TestData--- " + i);
nameList.add(testBean);
}

}

//加载toolbar文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//加载toolbar布局
getMenuInflater().inflate(R.menu.word_activity_toolbar, menu);
return true;
}

//为按钮设置点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.translate_bar_add:
Toast.makeText(this, "添加到单词本\n功能未完成", Toast.LENGTH_SHORT).show();
break;

case android.R.id.home:
//前往主界面
Intent intent = new Intent(WordActivity.this,MainActivity.class);
startActivity(intent);
finish();
break;

default:
}
return true;
}

}


贴的东西有点多。关于RecyclerView的使用方面的都贴出来了。麻烦大佬多看两眼。我也不知道这个问题出在哪里了。
十分感谢!!!!
...全文
427 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2018-03-08
  • 打赏
  • 举报
回复
R.layout.string_item布局的高度不要设置为match_parent
__椎名真白 2018-03-08
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
R.layout.string_item布局的高度不要设置为match_parent
卧!!槽!! 我找了好久的活动布局。一直找不到问题在哪。原来是在子项这里。感谢感谢。十分感谢!

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧